• Resolved wpshiran

    (@wpshiran)


    HI,

    im trying to display the stock amount and availability option in a product sidebar. I have used below code to display the quantity.

    <div class=”stock-num”><?php echo $product->get_stock_quantity(); ?></span> Available</p></div>

    What code I should use to hide the quantity and display “OUT OF STOCK” option on the sidebar, when I select out of stock at the backend.

    Thanks in advance and a big help.

    Cheers
    Shiran

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    <?php echo $product->get_availability(); ?>

    Thread Starter wpshiran

    (@wpshiran)

    Hey Mike,

    Thanks for the code. I inserted that code, and now it displays the word “Array”. Any idea why?

    And how do I hide “<?php echo $product->get_stock_quantity(); ?>” when I select out of stock?

    Thanks mate
    Shiran

    Plugin Contributor Mike Jolley

    (@mikejolley)

    That gives you the stock status.

    $availability      = $product->get_availability();
    $availability_html = empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>';
    
    echo $availability_html;

    That outputs a string.

    Compare $availability[‘class’] to see if its out of stock.

    Thread Starter wpshiran

    (@wpshiran)

    Thanks for the amazing quick replies Mike.

    I found this code that did the trick for me.

    <?php
        // Availability
        $availability = $product->get_availability();
        if ($availability['availability']) :
            echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] );
        endif;
    ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]

    Cheers!
    Shiran

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Stock Display’ is closed to new replies.