我想添加一个自定义stock_status woocommerce 3.0在wordpress中。添加一个股票期权给Woocommerce 3.0
最终目标是在产品编辑页面上添加第三个库存选项“保留”,并在产品页面上显示该库存状态。
以前我能在这里使用的方法: Add stock option in woocommerce,它确实工作要额外股票期权增加了产品管理,但它看起来像Woocommerce 3.0也有一些是压倒一切的实际产品网页上我的设置。它也在产品页面上工作,直到我升级到3.0。
我的functions.php:
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select(array('id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __('Stock status', 'woocommerce'), 'options' => array(
'instock' => __('In stock', 'woocommerce'),
'outofstock' => __('Out of stock', 'woocommerce'),
'onhold' => __('On Hold', 'woocommerce'), // The new option !!!
), 'desc_tip' => true, 'description' => __('Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce')));}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status($product_id) {
update_post_meta($product_id, '_stock_status', wc_clean($_POST['_stock_status']));
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woocommerce_get_custom_availability($data, $product) {
switch($product->get_stock_status) {
case 'onhold':
$data = array('availability' => __('On Hold', 'woocommerce'), 'class' => 'on-hold');
break;
case 'instock':
$data = array('availability' => __('In stock', 'woocommerce'), 'class' => 'in-stock');
break;
case 'outofstock':
$data = array('availability' => __('Out of stock', 'woocommerce'), 'class' => 'out-of-stock');
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 3, 2);
我可以在数据库中看到,stock_status正确设置为“挂起”,我的自定义状态,当我选择它的管理,但这并未在实际产品页面上应用。
为了验证,我修改了price.php简单地输出库存状态:
<p class="price">
<?php
$stockamount = $product->get_stock_quantity();
$price = $product->get_price_html();
$stockstatus = $product->get_stock_status();
$pricelabelone = "Out of Stock";
$pricelabeltwo = "On Hold";
echo $stockstatus;
?>
</p>
然而,即使我设置产品,以“挂起”(这是节约),该产品页面始终输出“库存”(测试产品: http://aegis-staging.byethost7.com/dgh/product/santa-cruz-h13-custom-42-cocobolo-and-moon-spruce/)。
我错过了什么?
工作就像一个魅力,谢谢! – Kevin