我也尝试了这个答案对
。将以下内容添加到您的管理脚本:
add_filter( 'product_type_selector' , array( $this, 'wpa_120215_add_product_type' ) );
function wpa_120215_add_product_type( $types ){
$types[ 'your_type' ] = __( 'Your Product Type' );
return $types;
}
class WC_Product_Your_Type extends WC_Product{
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
public function __construct( $product ) {
$this->product_type = 'your_type';
parent::__construct( $product );
}
}
要在前端显示自定义模板,添加了以下内容:
define( 'YOUR_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
add_action('woocommerce_your_type_add_to_cart', array($this, 'add_to_cart'),30);
public function add_to_cart() {
wc_get_template( 'single-product/add-to-cart/your_type.php',$args = array(), $template_path = '', YOUR_TEMPLATE_PATH);
}
评论答案...
your-plugin/
includes/
admin/
在管理文件夹中:类 - 您的类型 - admin.php
<?php
class Your_Type_Admin {
public function __construct() {
add_filter( 'product_type_selector' , array( $this, 'product_type_selector_one'));
}
public function product_type_selector_one( $types ) {
$types[ 'your_type' ] = __( 'Your Type product' );
return $types;
}}
new Your_Type_Admin();
然后在您的插件上包含此文件__ Construct File
。
或使用此代码:
// #1 Add New Product Type to Select Dropdown
add_filter( 'product_type_selector', 'wpsaad_add_custom_product_type' );
function wpsaad_add_custom_product_type( $types ){
$types[ 'custom' ] = 'Custom product';
return $types;
}
// --------------------------
// #2 Add New Product Type Class
add_action( 'init', 'wpsaad_create_custom_product_type' );
function wpsaad_create_custom_product_type(){
class WC_Product_Custom extends WC_Product {
public function get_type() {
return 'custom';
}
}
}
// --------------------------
// #3 Load New Product Type Class
add_filter( 'woocommerce_product_class', 'wpsaad_woocommerce_product_class', 10, 2 );
function wpsaad_woocommerce_product_class( $classname, $product_type ) {
if ( $product_type == 'custom' ) {
$classname = 'WC_Product_Custom';
}
return $classname;
}