如何在WooCommerce中为自定义产品类型启用价格和库存 [英] How to enable price and inventory for custom product type in WooCommerce

查看:209
本文介绍了如何在WooCommerce中为自定义产品类型启用价格和库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a custom product type in my WooCommerce application

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

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

This shows product type in product data dropdown like as follows,

我的问题是

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

The new product don't have an option for adding inventory and prices, How can I enable these options?

推荐答案

您需要一个小的JS技巧来显示价格和库存"标签,即您 需要在您的情况下添加类show_if_{your_custom_product_type} 将为show_if_variable_bulk.

You need a small JS trick to show Price and Inventory tab, i.e. you need to add class show_if_{your_custom_product_type} in your case it will be 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文件中.
代码已经过测试并且可以正常工作.

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin PHP files.
Code is tested and works.

这是您的常规标签的外观:

This is how your general tab will look:

这是库存标签的外观

希望这会有所帮助!

这篇关于如何在WooCommerce中为自定义产品类型启用价格和库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆