Add stock option in Woocommerce 3.0

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

The ultimate goal is to add a third inventory option on the product edit page, "On." and display the stock status on the product page.

Previously, I could use this method here: Add a stock option in woocommerce , which works to add more stock options to the product admin, but it looks like there is something with Woocommerce 3.0 that moved my settings to the actual product page. He also worked on the product page until I updated it to 3.0.

My .php functions:

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

I can see in the db that stock_status is setting correctly to "On Hold", my custom status when I select it in admin, but that doesn't apply on the real products page.

To test, I changed my price.php to just 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>

      

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

What am I missing?

+3


source to share


1 answer


This solution can help you solve your problem. Use get_post_meta () instead of get_stock_status () to get the stock status.

Step through the "woocommerce_get_availability" code as 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);

      

+1


source







All Articles