Magento 2 plugin change price of products that have a custom attribute with












0














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?










share|improve this question



























    0














    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?










    share|improve this question

























      0












      0








      0







      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Riccardo RonconiRiccardo Ronconi

      153




      153






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer























          • 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
















          1














          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.






          share|improve this answer























          • 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














          1












          1








          1






          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          1300-talet

          1300-talet

          Display a custom attribute below product name in the front-end Magento 1.9.3.8