如何在 WooCommerce 中为自定义产品类型启用价格和库存

Shi*_*jin 5 php wordpress plugins woocommerce hook-woocommerce

我在 WooCommerce 应用程序中创建了自定义产品类型

function register_variable_bulk_product_type() {

    class WC_Product_Variable_bulk extends WC_Product_Simple {

        public function __construct($product) {

            $this->product_type = 'variable_bulk';
            parent::__construct($product);
        }

    }

}

add_action('init', 'register_variable_bulk_product_type');

function add_variable_bulk_product($types) {

    $types['variable_bulk'] = __('Variable Bulk');

    return $types;
}

add_filter('product_type_selector', 'add_variable_bulk_product');
Run Code Online (Sandbox Code Playgroud)

这在产品数据下拉菜单中显示了产品类型,如下所示,

在此处输入图片说明

我的问题是

新产品没有添加库存和价格的选项,如何启用这些选项?

Rau*_*pta 11

show_if_{your_custom_product_type}您需要一个 JS 小技巧来显示“价格”和“库存”选项卡,即您需要在您的情况下添加类show_if_variable_bulk

这是工作代码:

function wh_variable_bulk_admin_custom_js() {

    if ('product' != get_post_type()) :
        return;
    endif;
    ?>
    <script type='text/javascript'>
        jQuery(document).ready(function () {
            //for Price tab
            jQuery('.product_data_tabs .general_tab').addClass('show_if_variable_bulk').show();
            jQuery('#general_product_data .pricing').addClass('show_if_variable_bulk').show();
            //for Inventory tab
            jQuery('.inventory_options').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_variable_bulk').show();
        });
    </script>
    <?php

}

add_action('admin_footer', 'wh_variable_bulk_admin_custom_js');
Run Code Online (Sandbox Code Playgroud)

代码位于functions.php您的活动子主题(或主题)的文件中。或者也可以在任何插件 PHP 文件中。
代码经过测试并且可以工作。

这是您的常规选项卡的外观:

在此输入图像描述

这就是库存选项卡的外观

在此输入图像描述

希望这可以帮助!