Magento 2 conditionally hide quantity box
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
magento2 magento2.3
New contributor
add a comment |
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
magento2 magento2.3
New contributor
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50
add a comment |
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
magento2 magento2.3
New contributor
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
magento2 magento2.3
magento2 magento2.3
New contributor
New contributor
New contributor
asked Jan 21 at 16:49
Alex SmithAlex Smith
11
11
New contributor
New contributor
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50
add a comment |
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50
add a comment |
1 Answer
1
active
oldest
votes
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!
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute setyes
forused in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Alex Smith is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258609%2fmagento-2-conditionally-hide-quantity-box%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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!
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute setyes
forused in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
add a comment |
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!
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute setyes
forused in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
add a comment |
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!
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!
answered Jan 22 at 3:16
PawanPawan
1,6502515
1,6502515
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute setyes
forused in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
add a comment |
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute setyes
forused in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
No, that doesnt work either. With the first line i get NULL still and the second line i get bool(false).
– Alex Smith
Jan 22 at 9:42
Does your attribute set
yes
for used in product listnig
– Pawan
Jan 22 at 11:41
Does your attribute set
yes
for used in product listnig
– Pawan
Jan 22 at 11:41
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Yes, "used in product listing" is set to yes.
– Alex Smith
Jan 22 at 11:46
Have you added any value in admin for this product ?
– Pawan
Jan 22 at 11:54
Have you added any value in admin for this product ?
– Pawan
Jan 22 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
Jan 22 at 12:09
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
Jan 22 at 12:09
add a comment |
Alex Smith is a new contributor. Be nice, and check out our Code of Conduct.
Alex Smith is a new contributor. Be nice, and check out our Code of Conduct.
Alex Smith is a new contributor. Be nice, and check out our Code of Conduct.
Alex Smith is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258609%2fmagento-2-conditionally-hide-quantity-box%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is code of your attribute ?
– Pawan
Jan 21 at 17:04
Attribute code is quantity
– Alex Smith
Jan 21 at 20:50