Magento 2 plugin change price of products that have a custom attribute with
I'm trying to change the prices of products with a plugin. And it works, basically. The problem is this: i only need to change the price of products that have a specific attribute, not the price of all products. With a plugin, it's easy to add a fixed value to the price of a product, but i can't figure out how to check if the product has that custom attribute. this is the code I'm using:
namespace FrostmageWBTPDCPlugin;
class Product
{
public function afterGetPrice(MagentoCatalogModelProduct $subject, $result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wbtvalue = $objectManager->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('wbtpdc/modifier/variable');
return $result * $wbtvalue;
}
}
Now... It works for the value I added, but I need to add a check, sort of like this:
if ($subject->getData('has_wbt'))
{
return $result + ($wbtvalue * $subject->getData(weight));
}
Thing is that $subject is not the right variable to use (I wrote that just to exemplify what I need to do). If I try to use objectManager to create a $current_product variable, it always returns an exception for calling getData on null. If I use $subject, it always returns 0. So this is the problem: How can I change the price only of products that have 'has_wbt' attribute so that it shows on the frontend?
I mean, is there a way to have this plugin only act on products that have a specific attribute?
attributes price custom-attributes plugin magento2.3
add a comment |
I'm trying to change the prices of products with a plugin. And it works, basically. The problem is this: i only need to change the price of products that have a specific attribute, not the price of all products. With a plugin, it's easy to add a fixed value to the price of a product, but i can't figure out how to check if the product has that custom attribute. this is the code I'm using:
namespace FrostmageWBTPDCPlugin;
class Product
{
public function afterGetPrice(MagentoCatalogModelProduct $subject, $result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wbtvalue = $objectManager->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('wbtpdc/modifier/variable');
return $result * $wbtvalue;
}
}
Now... It works for the value I added, but I need to add a check, sort of like this:
if ($subject->getData('has_wbt'))
{
return $result + ($wbtvalue * $subject->getData(weight));
}
Thing is that $subject is not the right variable to use (I wrote that just to exemplify what I need to do). If I try to use objectManager to create a $current_product variable, it always returns an exception for calling getData on null. If I use $subject, it always returns 0. So this is the problem: How can I change the price only of products that have 'has_wbt' attribute so that it shows on the frontend?
I mean, is there a way to have this plugin only act on products that have a specific attribute?
attributes price custom-attributes plugin magento2.3
add a comment |
I'm trying to change the prices of products with a plugin. And it works, basically. The problem is this: i only need to change the price of products that have a specific attribute, not the price of all products. With a plugin, it's easy to add a fixed value to the price of a product, but i can't figure out how to check if the product has that custom attribute. this is the code I'm using:
namespace FrostmageWBTPDCPlugin;
class Product
{
public function afterGetPrice(MagentoCatalogModelProduct $subject, $result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wbtvalue = $objectManager->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('wbtpdc/modifier/variable');
return $result * $wbtvalue;
}
}
Now... It works for the value I added, but I need to add a check, sort of like this:
if ($subject->getData('has_wbt'))
{
return $result + ($wbtvalue * $subject->getData(weight));
}
Thing is that $subject is not the right variable to use (I wrote that just to exemplify what I need to do). If I try to use objectManager to create a $current_product variable, it always returns an exception for calling getData on null. If I use $subject, it always returns 0. So this is the problem: How can I change the price only of products that have 'has_wbt' attribute so that it shows on the frontend?
I mean, is there a way to have this plugin only act on products that have a specific attribute?
attributes price custom-attributes plugin magento2.3
I'm trying to change the prices of products with a plugin. And it works, basically. The problem is this: i only need to change the price of products that have a specific attribute, not the price of all products. With a plugin, it's easy to add a fixed value to the price of a product, but i can't figure out how to check if the product has that custom attribute. this is the code I'm using:
namespace FrostmageWBTPDCPlugin;
class Product
{
public function afterGetPrice(MagentoCatalogModelProduct $subject, $result)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wbtvalue = $objectManager->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('wbtpdc/modifier/variable');
return $result * $wbtvalue;
}
}
Now... It works for the value I added, but I need to add a check, sort of like this:
if ($subject->getData('has_wbt'))
{
return $result + ($wbtvalue * $subject->getData(weight));
}
Thing is that $subject is not the right variable to use (I wrote that just to exemplify what I need to do). If I try to use objectManager to create a $current_product variable, it always returns an exception for calling getData on null. If I use $subject, it always returns 0. So this is the problem: How can I change the price only of products that have 'has_wbt' attribute so that it shows on the frontend?
I mean, is there a way to have this plugin only act on products that have a specific attribute?
attributes price custom-attributes plugin magento2.3
attributes price custom-attributes plugin magento2.3
asked yesterday
Riccardo RonconiRiccardo Ronconi
153
153
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Below Code is working for me:
if($subject->hasData('color'))
{
return 10;
}
Now Go to the Store->Product Attribute->Edit Your Attribute->Storefront Properties->Used in Product Listing->Yes
Then save your attribute.
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
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%2f256991%2fmagento-2-plugin-change-price-of-products-that-have-a-custom-attribute-with%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
Below Code is working for me:
if($subject->hasData('color'))
{
return 10;
}
Now Go to the Store->Product Attribute->Edit Your Attribute->Storefront Properties->Used in Product Listing->Yes
Then save your attribute.
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
add a comment |
Below Code is working for me:
if($subject->hasData('color'))
{
return 10;
}
Now Go to the Store->Product Attribute->Edit Your Attribute->Storefront Properties->Used in Product Listing->Yes
Then save your attribute.
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
add a comment |
Below Code is working for me:
if($subject->hasData('color'))
{
return 10;
}
Now Go to the Store->Product Attribute->Edit Your Attribute->Storefront Properties->Used in Product Listing->Yes
Then save your attribute.
Below Code is working for me:
if($subject->hasData('color'))
{
return 10;
}
Now Go to the Store->Product Attribute->Edit Your Attribute->Storefront Properties->Used in Product Listing->Yes
Then save your attribute.
edited 15 hours ago
answered 20 hours ago
Dhairya ShahDhairya Shah
864
864
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
add a comment |
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
Looks like this checks if the attribute exists, and if it does it returns true for every product, even if they don't have said attribute set to true
– Riccardo Ronconi
16 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
@Riccardo Ronconi Please check my edited answer.
– Dhairya Shah
15 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
it worked perfectly, thank you! The problem was that, while my custom attribute was used in product listing, weight was not (it never came to my mind) so it always returned 0. You really helped an incredible lot, man, Thank You!
– Riccardo Ronconi
14 hours ago
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f256991%2fmagento-2-plugin-change-price-of-products-that-have-a-custom-attribute-with%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