gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:56:03 26 4
gpt4 key购买 nike

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

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

enter image description here

我的问题是

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

最佳答案

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 文件中。
代码已经过测试并且可以工作。

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

enter image description here

这就是库存标签的样子

enter image description here

希望这对您有所帮助!

关于php - 如何在 WooCommerce 中为自定义产品类型启用价格和库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com