Wordpress/WooCommerce自定义产品类型


Wordpress / WooCommerce Custom Product type

我为Woo commerce制作了一个自定义产品类型,唯一的问题是让它发挥作用。

我为产品类型添加了一个过滤器,它显示在下拉框中,但当我单击它时,它除了SKU之外没有其他框。

有没有我丢失的文件的某个位置?因为我目前刚刚把它和其他产品类型文件一起抛出。

感谢您为提供的任何帮助

添加产品类后,还必须操作产品设置页面上"常规"选项卡的元框。价格框在那里,它最初只是隐藏的,除非产品类型是"简单"或"外部"

有几种方法可以做到这一点。一种(不建议)是直接编辑html-variation-admin.php文件,并将"show_if_customproduct"添加到价格组div.

<div class="options_group pricing show_if_simple show_if_external">

变为:

<div class="options_group pricing show_if_simple show_if_external show_if_customproduct">

另一个(更好的)方法是通过调用woomocommerce_product_options_general_product_data操作来添加一些javascript:

add_action('wocommerce_product_options_general_product_data','owType');

function showType(){
  echo "<script>jQuery('.show_if_simple').addClass('show_if_customproduct');</script>";
}

这将打开通用部分中"customproduct"框的所有"简单"产品框。

或者,您的操作可以使用新框回显一个新的div,将div类设置为"show_if_customproduct",并从html_variations文件中的group-pricediv复制初始内容。

您还可能需要为页面添加模板,至少要获得"添加到购物车"按钮。查看模板/单个产品以获得想法。

首先,您需要为自定义产品类型创建一个类。假设它是CustomProduct

class WC_Product_CustomProduct extends WC_Product{
    public function __construct( $product ) {
        $this->product_type = 'CustomProduct';
        parent::__construct( $product );
    }
}

并且您将把这个类放在includes/文件夹下。之后,您需要激活此产品类型;

add_filter( 'product_type_selector', 'add_custom_product_type' );
function add_custom_product_type( $types ){
    $types[ 'CustomProduct' ] = __( 'Custom Product' );
    return $types;
}

将其添加到functions.php