向 Woocommerce 3.0 添加股票期权

问题描述:

我正在尝试在 wordpress 中将自定义 stock_status 添加到 woocommerce 3.0.

I am trying to add in a custom stock_status to woocommerce 3.0 in wordpress.

最终目标是在产品编辑页面上添加第三个库存选项暂停",并在产品页面上显示该库存状态.

The end goal is to add a 3rd inventory option on the product edit page, "On Hold", and display that stock status on the product page.

以前我可以在这里使用该方法:在woocommerce中添加股票选项,这确实可以向产品管理员添加额外的股票选项,但看起来在 Woocommerce 3.0 中,实际产品页面上的设置覆盖了我的设置.在我升级到 3.0 之前,它也在产品页面上工作.

Previously I was able to use the method here: Add stock option in woocommerce, which does work to add extra stock options to the product admin, but it looks like with Woocommerce 3.0 there is something over-riding my settings on the actual product page. It was also working on the products page until I upgraded to 3.0.

我的functions.php:

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

https://pastebin.com/EFtBVY9h

我可以在数据库中看到 stock_status 正确设置为暂停",这是我的自定义状态,当我在管理员中选择它时,但这并未应用于实际产品页面.

I can see in the DB that the stock_status is properly setting to "On Hold", my custom status, when I select it on the admin, but this is not being applied on the actual products page.

为了验证,我修改了我的 price.php 以简单地输出库存状态:

To verify, I modified my price.php to simply output the stock status:

<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/).

However, even though I set products to "On Hold" (and this is saving), the product page is always outputting "In Stock" (test product: http://aegis-staging.byethost7.com/dgh/product/santa-cruz-h13-custom-42-cocobolo-and-moon-spruce/).

我错过了什么?

此解决方案可能会帮助您解决问题.使用 "get_post_meta()" 而不是使用 get_stock_status() 获取库存状态.

This solution may help you to solve your problem. Use "get_post_meta()" instead of using get_stock_status() to get stock status.

通过woocommerce_get_availability"钩子代码,如下面的代码

Go through the "woocommerce_get_availability" hook code like the below code

function woocommerce_get_custom_availability( $data, $product ) {
$stock_status = get_post_meta($product->id , '_stock_status' , true );
switch( $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);