cancel
Showing results for 
Search instead for 
Did you mean: 

Hiding quantity box conditionally

Hiding quantity box conditionally

Hi,

Is there a way to hide the quantity box conditionally? I have some products that i have created a quantity dropdown box in the configurable options and i want to hide the built-in qty box for products that have these quantity attributes, but keep it for others that do not.

I tried adding an if statement too the addtocart.phtml file and this seems to work, it also hides the add to cart button which i dont want and my if statement ends before the button code. What am i doing wrong? My if statment is:

<?php if (in_array ('quantity', $product->getCustomAttribute, FALSE)) : ?>

I have included my full sample code:

<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
	<?php if (in_array ('quantity', $product->getCustomAttribute, FALSE)) : ?>
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
		<?php endif; ?>
		<?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
            </button>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>

Thanks

3 REPLIES 3

Re: Hiding quantity box conditionally

Hi @alexsmith2709,

 

Everything looks fine! But make sure the following statement's return type/result.

in_array ('quantity', $product->getCustomAttribute, FALSE)

Should be either true or false.  

Prema M

Re: Hiding quantity box conditionally

It looks like this line is the problem. Whether it is TRUE or FALSE it doesnt show the quantity box. Does anyone know the correct way to check if there is a custom attribute called quantity that i could use to get this to work? 

Re: Hiding quantity box conditionally

I have been playing around with different code but i cant get anything to work.

My code at the moment to get the quantity custom attribute returns NULL.

It is  

$myquantity = $_product->getQuantity();

When i do a var_dump of $myquantity i get NULL. The product i am testing this on has a custom attribute called quantity. What am i doing wrong? I dont want any values from the quantity attribute, just check to see if an attribute called quantity is on this product, if i get a returns of TRUE then i can run an if statement