在woocommerce中添加自定义产品类型

Sks*_*dip 2 php wordpress woocommerce

我正在使用Woocommerce,我想创建一个自定义产品类型,但我没有得到这样的方式

在Woocommerce中有四种产品类型

 Simple product
 variable product
 grouped product and
 External/Affiliate product
Run Code Online (Sandbox Code Playgroud)

我需要另一种定制产品类型.有没有办法创建自定义产品类型?

小智 6

我也试着帮助这个答案.

将以下内容添加到管理脚本中:

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;
}
Run Code Online (Sandbox Code Playgroud)

这将在管理仪表板的产品数据选择框中显示"您的产品类型".

接下来使用它创建一个文件来实际创建产品:

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 );
  }
}
Run Code Online (Sandbox Code Playgroud)

要在前端显示您的自定义模板,请添加以下内容:

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);

}
Run Code Online (Sandbox Code Playgroud)