x-wp/wc-product-customizer
v1.0.2
2024-09-22 04:43 UTC
Requires
- php: >=8.0
- x-wp/helper-functions: ^1.4
Requires (Dev)
Suggests
- automattic/jetpack-autoloader: Allow for better interoperability with other plugins that use this package.
This package is auto-updated.
Last update: 2024-09-22 04:44:07 UTC
README
这是一个库,允许您轻松地自定义 WooCommerce 中的产品类型、选项和选项卡。
安装
composer require x-wp/wc-product-customizer
使用
要使用自定义器 - 您需要扩展基本自定义器类。
自定义产品类型
<?php class My_Customizer extends \XWC\Product\Customizer_Base { public function custom_product_types( array $types ): array { $types['simple'] = array( 'class' => My_Simple_Product::class, ); $types['composite'] = array( 'class' => Composite_Product::class, // Product classname to use - Mandatory for custom product types 'name' => 'Composite product', // Name to be displayed in product type selectors 'extends' => array( 'simple' ,'virtual' ), // Base product type to extend. Used to show hide / option tabs groups 'tabs' => array( array( 'id' => 'composite', // Tab ID. Used in HTML generation, and as an action suffix 'label' => 'Structure', // Tab label. 'icon' => 'woo:\f307', // Tab icon. Unicode strings will use dashicons, strings prefixed with woo: will use woocommerce icomoon font. 'prio' => 33, // Tab priority. ) ) ); return $types; } }
这将创建一个名为 composite
的新产品类型,将显示 simple
和 virtual
产品所有选项,并添加一个名为 结构
的新选项卡。
自定义产品选项
<?php class My_Customizer extends \XWC\Product\Customizer_Base { public function custom_product_options( array $options ): array { $options['premium'] = array( 'default' => false, // Default option value. 'desc' => 'Enable premium features', // Option description. 'for' => array( 'simple', 'variable' ), // Product types this option is available for. 'label' => 'Premium', // Option label. 'prop' => true, // Whether this option is a product property. 'tabs' => array(), // Custom tabs to show when this option is enabled. ); return $options; } }
在自定义选项卡中显示选项。
对于每个注册的选项卡,我们显示所有需要的面板数据。您可以通过提供的钩子添加自定义选项。
<?php class My_Custom_Product_Options { public function __construct() { add_action( 'xwc_product_options_composite', array( $this, 'display_options' ), 99, 0 ); } public function display_options(): void { global $product_object; echo '<div class="options_group">'; woocommerce_wp_select( array( 'id' => '_my_data', 'label' => 'My Data', 'default' => '', 'class' => 'wc-enhanced-select', 'options' => array( '' => 'Select a value', 'option1' => 'Option 1', 'option2' => 'Option 2', ), ) ); echo '</div>'; } }