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

2024-03-17

我在 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');

这会在产品数据下拉列表中显示产品类型,如下所示,

我的问题是

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


你需要一个 JS 小技巧来显示价格和库存选项卡,即你 需要添加类show_if_{your_custom_product_type}在你的情况下 将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');

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

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

这就是库存选项卡的外观

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 WooCommerce 中启用自定义产品类型的价格和库存 的相关文章

随机推荐