Home > Mobile >  Woocommerce product counts in single product page
Woocommerce product counts in single product page

Time:11-26

I am currently struggling with woo-commerce and the availability status on product pages. I am trying to display various texts (translated) on the product pages, whether the product is available and how many we have, then whether it is unavailable, whether it is available to backorder, and what I can't do is display the message that the product is in stock with disabled managing stock. There are unlimited products. But it still shows me only "available places".

There is my function.

add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
function product_stock_quantity_single( $availability, $_product ) {
  global $product;
  $stock = $product->get_stock_quantity();
    if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
    if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );

 return $availability;
}

I've tried many different combinations and frankly, it doesn't work for me. Thanks for all the advice and help. I tried what was in the topic which was merged with mine. It's a different topic about different things I want to achieve. I need a product with disabled managing stock but still available (something like infinite stock)

CodePudding user response:

You're currently returning $availability outside of the function. Give this a try:

add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
function product_stock_quantity_single( $availability, $_product ) {
  global $product;
  $stock = $product->get_stock_quantity();
    if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
    if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );
    
    return $availability;
}

CodePudding user response:

Check out this answer and add the fourth condition I added. Hope this helps you want to achieve.

 add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
    function product_stock_quantity_single( $availability, $_product ) {
      global $product;
      $stock = $product->get_stock_quantity();
        if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
        if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
        if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );
        if ( $_product->is_in_stock() && !$stock ) $availability['availability'] = __('Stock disabled text', 'woocommerce');
        
        return $availability;
    }
  • Related