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