0

I would like to hide the built in quantity box based on whether i have set a custom attribute for quantity. Some products that dont have a custom attribute for quantity will need to still display the quantity box. I'd like this for everywhere the quantity box is shown, but most importantly and where i am currently trying to get it to work is the product page. For example, one of the products i will have is business cards. I will have a price per each variant (material, number of sides, quantity, lamination etc) which i can create using the custom attributes and this part works fine.

I cannot work out how to do the checking for the custom attribute called quantity. I have tried various things and when i do a var_dump of the variable i just get "NULL" and im not sure where i am going wrong. I am using Magento 2.3 and i am editing the addtocart.phtml template file, which i was hoping to just add an if statement to not show the qty div if it returned true.

The code i have last tried is:

<?php $myquantity = $_product->getCustomAttribute('quantity'); ?>

and when i do a var_dump i just get NULL. The full addtocart.phtml code is with the only changes are the line above and the var_dump line:

<?php $_product = $block->getProduct(); ?>
<?php $myquantity = $_product->getCustomAttribute('quantity'); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
        <?php var_dump($myquantity); ?>
            <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; ?>
        <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>

I am only at the checkign phase so far, i didnt see the point in trying to write the if statement when i can't return the right information first. Of course, if there is another/better way i can do this i would appreciate the help. I am not much of a developer so i was hoping for a simple resolution.

Thanks

2
  • What is code of your attribute ?
    – Pawan
    Commented Jan 21, 2019 at 17:04
  • Attribute code is quantity
    – Alex Smith
    Commented Jan 21, 2019 at 20:50

3 Answers 3

0

If issue is related to get attribute value, You can use:

<?php $myquantity = $_product->getQuantity(); ?>

----------OR----------

<?php $myquantity = $_product->getAttributeText('quantity'); ?> 

once you have value of $myquantity, you can put your logic.

Hope above will help!

Update I

You can create a Yes/No type attribute instead of drop-down. Default value will be No. for product you want to hide add to cart change value to Yes.

<?php echo $_product->getAttributeText('quantity'); ?>

Will give you value Yes Or No, based on value you can put your logic!

6
  • No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
    – Alex Smith
    Commented Jan 22, 2019 at 9:42
  • Does your attribute set yes for used in product listnig
    – Pawan
    Commented Jan 22, 2019 at 11:41
  • Yes, "used in product listing" is set to yes.
    – Alex Smith
    Commented Jan 22, 2019 at 11:46
  • Have you added any value in admin for this product ?
    – Pawan
    Commented Jan 22, 2019 at 11:54
  • yes, it is a dropdown with 2 values. I want to check if this dropdown exists when the page is loaded, before any value is selected. I guess i want a boolean answer so i can do an if statement if the attribute is true or false
    – Alex Smith
    Commented Jan 22, 2019 at 12:09
0

I was just working on a similar task. You should just need to add "getValue()"

<?php $myquantity = $_product->getCustomAttribute('quantity')->getValue(); ?>

0

i have magento 2.3.7, what do I need to change in the code to make dropdown attribute "quantity_active" yes/no to hide quantity? My code in addtocart.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()) :?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()) :?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       min="0"
                       value="<?= $block->getProductDefaultQty() * 1 ?>"
                       title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtmlAttr(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
                    class="action primary tocart"
                    id="product-addtocart-button" disabled>
                <span><?= $block->escapeHtml($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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.