我正在使用WooCommerce,我想创建一种自定义产品类型,但我没有得到WAY

问题描述 投票:0回答:2
我在那里需要另一种自定义产品类型。有什么方法可以创建自定义产品类型?

我也尝试了这个答案对

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

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

php wordpress woocommerce
2个回答
6
投票

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

0
投票
您可以在几秒钟内使用此WordPress插件来轻松地做到这一点,称为WooCommerce(附加组件,数据,选项,预订和约会)

或使用此代码:

// #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; }
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.