我正在使用Woocommerce,我想创建一个自定义产品类型,但我没有找到方法
在Woocommerce中,有四种产品类型,如
Simple product
variable product
grouped product and
External/Affiliate product
我需要另一种自定义产品类型。有没有办法创建自定义产品类型?
答案 0 :(得分:6)
我也在尝试this answer helped。
将以下内容添加到管理脚本中:
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);
}
答案 1 :(得分:0)
评论答案......
your-plugin/
includes/
admin/
在admin文件夹中:class-your-type-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文件
中