Magento 2.2.3 How to check if attribute exist in current product?
I want to check if attribute exist in current product by attribute code.
For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.
So how can i filter is current value is not attribute?
attributes product-attribute frontend magento2.2.3
add a comment |
I want to check if attribute exist in current product by attribute code.
For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.
So how can i filter is current value is not attribute?
attributes product-attribute frontend magento2.2.3
I think you can try this$product->hasData('custom_attribute')
– kunj
1 hour ago
add a comment |
I want to check if attribute exist in current product by attribute code.
For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.
So how can i filter is current value is not attribute?
attributes product-attribute frontend magento2.2.3
I want to check if attribute exist in current product by attribute code.
For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.
So how can i filter is current value is not attribute?
attributes product-attribute frontend magento2.2.3
attributes product-attribute frontend magento2.2.3
edited 2 hours ago
Shoaib Munir
949421
949421
asked 2 hours ago
Chirag PatelChirag Patel
2,094320
2,094320
I think you can try this$product->hasData('custom_attribute')
– kunj
1 hour ago
add a comment |
I think you can try this$product->hasData('custom_attribute')
– kunj
1 hour ago
I think you can try this
$product->hasData('custom_attribute')
– kunj
1 hour ago
I think you can try this
$product->hasData('custom_attribute')
– kunj
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}
Try it and do let me know.
Peace :)
Tnxs for answer. but it is giving me errorArray to string conversion
any way i found other solution. +1 for your efforts. :)
– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
add a comment |
try this
$product->hasData('custom_attribute')
OR $product->hasCustomAttribute()
add a comment |
I found solution in below way as per my specific requirement.
public function getAttributeValue($product, $code){
$attribute = $product->getResource()->getAttribute('attribute_code');
if ($attribute)
{
if (in_array(
$product->getResource()->getAttribute($code)->getFrontendInput(),
['select', 'text']
)) {
$value = $product->getAttributeText($code);
} else {
$value = $product->getData($code);
}
} else {
$value = '';
}
return $value;
}
Where in above $product
is current product and $code
is attribute code.
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
});
}
});
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%2f261908%2fmagento-2-2-3-how-to-check-if-attribute-exist-in-current-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}
Try it and do let me know.
Peace :)
Tnxs for answer. but it is giving me errorArray to string conversion
any way i found other solution. +1 for your efforts. :)
– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
add a comment |
Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}
Try it and do let me know.
Peace :)
Tnxs for answer. but it is giving me errorArray to string conversion
any way i found other solution. +1 for your efforts. :)
– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
add a comment |
Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}
Try it and do let me know.
Peace :)
Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}
Try it and do let me know.
Peace :)
edited 2 hours ago
answered 2 hours ago
G PrathapG Prathap
330113
330113
Tnxs for answer. but it is giving me errorArray to string conversion
any way i found other solution. +1 for your efforts. :)
– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
add a comment |
Tnxs for answer. but it is giving me errorArray to string conversion
any way i found other solution. +1 for your efforts. :)
– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
Tnxs for answer. but it is giving me error
Array to string conversion
any way i found other solution. +1 for your efforts. :)– Chirag Patel
2 hours ago
Tnxs for answer. but it is giving me error
Array to string conversion
any way i found other solution. +1 for your efforts. :)– Chirag Patel
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.
– G Prathap
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
But why attribute return value is an array? how can i get in string?
– Chirag Patel
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
Updated, now check and let me know
– G Prathap
2 hours ago
add a comment |
try this
$product->hasData('custom_attribute')
OR $product->hasCustomAttribute()
add a comment |
try this
$product->hasData('custom_attribute')
OR $product->hasCustomAttribute()
add a comment |
try this
$product->hasData('custom_attribute')
OR $product->hasCustomAttribute()
try this
$product->hasData('custom_attribute')
OR $product->hasCustomAttribute()
answered 1 hour ago
kunjkunj
2,6072423
2,6072423
add a comment |
add a comment |
I found solution in below way as per my specific requirement.
public function getAttributeValue($product, $code){
$attribute = $product->getResource()->getAttribute('attribute_code');
if ($attribute)
{
if (in_array(
$product->getResource()->getAttribute($code)->getFrontendInput(),
['select', 'text']
)) {
$value = $product->getAttributeText($code);
} else {
$value = $product->getData($code);
}
} else {
$value = '';
}
return $value;
}
Where in above $product
is current product and $code
is attribute code.
add a comment |
I found solution in below way as per my specific requirement.
public function getAttributeValue($product, $code){
$attribute = $product->getResource()->getAttribute('attribute_code');
if ($attribute)
{
if (in_array(
$product->getResource()->getAttribute($code)->getFrontendInput(),
['select', 'text']
)) {
$value = $product->getAttributeText($code);
} else {
$value = $product->getData($code);
}
} else {
$value = '';
}
return $value;
}
Where in above $product
is current product and $code
is attribute code.
add a comment |
I found solution in below way as per my specific requirement.
public function getAttributeValue($product, $code){
$attribute = $product->getResource()->getAttribute('attribute_code');
if ($attribute)
{
if (in_array(
$product->getResource()->getAttribute($code)->getFrontendInput(),
['select', 'text']
)) {
$value = $product->getAttributeText($code);
} else {
$value = $product->getData($code);
}
} else {
$value = '';
}
return $value;
}
Where in above $product
is current product and $code
is attribute code.
I found solution in below way as per my specific requirement.
public function getAttributeValue($product, $code){
$attribute = $product->getResource()->getAttribute('attribute_code');
if ($attribute)
{
if (in_array(
$product->getResource()->getAttribute($code)->getFrontendInput(),
['select', 'text']
)) {
$value = $product->getAttributeText($code);
} else {
$value = $product->getData($code);
}
} else {
$value = '';
}
return $value;
}
Where in above $product
is current product and $code
is attribute code.
edited 24 mins ago
answered 43 mins ago
Chirag PatelChirag Patel
2,094320
2,094320
add a comment |
add a comment |
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%2f261908%2fmagento-2-2-3-how-to-check-if-attribute-exist-in-current-product%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
I think you can try this
$product->hasData('custom_attribute')
– kunj
1 hour ago