クラス
ItemPage
Item page HTML and JS
ソース ソース
ファイル: src/Views/ItemPage/ItemPage.php
class ItemPage { const SCRIPT_HANDLE = 'wcexwl-item'; /** * Master object * * @var Master */ protected $master; /** * Injects `Master` object * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param Master $master * @return void */ public function __construct(Master $master) { $this->master = $master; } /** * Registers hooks * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public function init() { (new ConditionalAssetsLoader())->loadItemPageAssets([$this, 'loadEssentialAssets']); add_action('wcex_sku_select_change_sku_success_js', [$this, 'updateCurrentSku'], 10, 1); add_filter('usces_filter_item_sku_button', [$this, 'addWishlistButton'], 10, 3); } /** * Loads CSS/JS for item page * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public function loadEssentialAssets() { /** * Return `false` with this hook to prevent item page CSS from being loaded * * @important * @param bool $flag Default: `true` */ if (apply_filters('wcexwl_filter_item_page_load_css', true)) { // load semantic-ui and common css Utils::loadCommonCss(); Semantic::loadButtonCss(); Semantic::loadLoaderCss(); // load css for page wp_enqueue_style( 'wcexwl-item-page', WCEXWL_PLUGIN_URL . '/src/Styles/item.css', [], WCEXWL_VERSION, false ); } // load JS client SDK $this->master->loadClientSdk(); /** * Fires after all item page common/default JS/CSS has been enqueued * * Use this hook to enqueue your own JS/CSS on the item page if necessary. * * @important */ do_action('wcexwl_item_page_on_load_assets'); $this->loadImplementationAssets(); } /** * Loads our JS * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ protected function loadImplementationAssets() { wp_enqueue_script( self::SCRIPT_HANDLE, WCEXWL_PLUGIN_URL . '/src/Views/ItemPage/item.js', [], WCEXWL_VERSION, true ); $assetsmap = include(WCEXWL_PLUGIN_DIR . '/dist/js/utils.asset.php'); wp_enqueue_script( 'wcexwl-utils', WCEXWL_PLUGIN_URL . '/dist/js/utils.js', array_merge($assetsmap['dependencies'], [self::SCRIPT_HANDLE]), $assetsmap['version'], true ); wp_localize_script(self::SCRIPT_HANDLE, 'wcexwl', $this->master->getScriptInjectionVariables()); Snackbar::load([self::SCRIPT_HANDLE]); } /** * Updates the value of the currently selected SKU when using `wcex_sku_select` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @return void */ public function updateCurrentSku($post_id) { ?> $('.wishlist-button').attr('onclick', "return wcexwl.item.addToWishlist(event, this, '<?php echo $post_id; ?>', '" + data['sku_enc'] + "')"); <?php } /** * Adds wishlist button next to "Add to Cart" button on item pages * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param string $skubutton * @return string */ public function addWishlistButton($skubutton) { global $usces; $display = Master::getFilterableValues(); $currentsku = urlencode($usces->itemsku['code']); $post_id = (int)get_the_ID(); ob_start(); ?> <div class="wcexwl item-page-buttons flex row-wrap ai-stretch jc-flex-end ml-05rem-children-not-first mt-05rem-children"> <?php echo $skubutton; ?> <div class="wcexwl flex row-wrap ai-stretch"> <input type="submit" value="<?php echo $display['addToWishlistButtonText'] ?>" class="wishlist-button <?php echo $display['btnsecondary']; ?>" onclick="return wcexwl.item.addToWishlist(event, this, <?php echo $post_id; ?>, '<?php echo $currentsku; ?>')" /> </div> </div> <?php $buttons = (string)ob_get_clean(); /** * Filters item page SKU and wishlist button HTML * * @important * @param string $buttons * @param string $skubutton Welcart 'Add to Cart' SKU button * @param int $post_id * @param string $currentsku */ $buttons = apply_filters('wcexwl_filter_item_page_buttons_html', $buttons, $skubutton, $post_id, $currentsku); return $buttons; } }
- __construct — Injects Master object
- addWishlistButton — Adds wishlist button next to "Add to Cart" button on item pages
- init — Registers hooks
- loadEssentialAssets — Loads CSS/JS for item page
- loadImplementationAssets — Loads our JS
- updateCurrentSku — Updates the value of the currently selected SKU when using wcex_sku_select