Home > database >  Can't get max quantity (woocommerce)
Can't get max quantity (woocommerce)

Time:10-22

Circumstances:
I created a product and added stock quantity: 80.
I installed Min/Max Quantities plugin and added min:1 & max: 20 for that product.
(Quantity varies from product to product)

Problem:
I need to echo max quantity of 20 on a shop page, but this code displays 80 which is stock quantity.

echo $product->get_max_purchase_quantity();

Would you please let me know how to get the max quantity?

functions.php:

add_action('woocommerce_after_shop_loop_item', 'QTY');
function QTY()
{
    global $product;    
    ?>
        <div class="shopAddToCart">
        <button  value="-" class="minus"  >-</button>
        <input 
        type="text"
        disabled="disabled"
        autocomplete="off"
        size="2"
        value="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['QTY'] : 0;
    ?>"
        id="count"
        data-product-id= "<?php echo $product->get_id() ?>"
        data-in-cart="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['in_cart'] : 0;
    ?>"
        data-in-cart-qty="<?php echo (Check_if_product_in_cart($product->get_id())) ? Check_if_product_in_cart($product->get_id())['QTY'] : 0;
    ?>"
        class="quantity  qty"
        max_value = <?php echo $product->get_max_purchase_quantity(); ?>
        min_value = <?php echo $product->get_min_purchase_quantity(); ?>
        >
        
        <button type="button" value=" " class="plus"  > </button>
        </div>
                              <?php
}

CodePudding user response:

The maximum_allowed_quantity is stored in product meta so you can retrieve it by using the get_post_meta function. check below code.

add_action('woocommerce_after_shop_loop_item', 'QTY');
function QTY(){
    global $product;
    if( get_post_meta( $product->get_id(), 'maximum_allowed_quantity', true ) > 0 ){
        echo '<div>Max quantity: '.get_post_meta( $product->get_id(), 'maximum_allowed_quantity', true ).'</div>';
    }
}

Tested and works

enter image description here

enter image description here

  • Related