Magento 2 - get special price of product by product id or sku












4















I need to get the special price of a product by product Id or Sku. Let's say I have an existing product with Id '21' and Sku 'test-21'.



This information is provided in my custom block class, how can I get the special price of this product in mentioned custom class?










share|improve this question





























    4















    I need to get the special price of a product by product Id or Sku. Let's say I have an existing product with Id '21' and Sku 'test-21'.



    This information is provided in my custom block class, how can I get the special price of this product in mentioned custom class?










    share|improve this question



























      4












      4








      4








      I need to get the special price of a product by product Id or Sku. Let's say I have an existing product with Id '21' and Sku 'test-21'.



      This information is provided in my custom block class, how can I get the special price of this product in mentioned custom class?










      share|improve this question
















      I need to get the special price of a product by product Id or Sku. Let's say I have an existing product with Id '21' and Sku 'test-21'.



      This information is provided in my custom block class, how can I get the special price of this product in mentioned custom class?







      magento2 magento-2.1 product sku special-price






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 13 '17 at 4:52









      Prince Patel

      13.3k54776




      13.3k54776










      asked Sep 11 '17 at 23:25









      shahir hajirshahir hajir

      4461929




      4461929






















          3 Answers
          3






          active

          oldest

          votes


















          5














          Use MagentoCatalogModelProductRepository class to get special price of product



          protected $_productRepository;

          public function __construct(
          ...
          MagentoCatalogModelProductRepository $productRepository,
          ...
          ) {
          $this->_productRepository = $productRepository;
          }

          public function getSpecialPriceById($id)
          {
          //$id = '21'; //Product ID
          $product = $this->_productRepository->getById($id);
          $product->getSpecialPrice();
          return $specialPrice;
          }

          public function getSpecialPriceBySku($sku)
          {
          //$sku = 'test-21'; //Product Sku
          $product = $this->_productRepository->get($sku);
          $product->getSpecialPrice();
          return $specialPrice;
          }


          Now you can get special price by



          $id = '21';
          $sku = 'test-21';
          $this->getSpecialPriceById($id);
          $this->getSpecialPriceBySku($sku);





          share|improve this answer


























          • How to get special price of configurable product?

            – Chirag Patel
            Jan 11 at 13:49



















          2














          use Magento/Catalog/Model/ProductFactory to load product, and get price information from the model



          Code is like:



          $specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');


          some reference about how to get product model in your custom module



          use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/



          use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/



          Normally dependency injection is recommended.






          share|improve this answer
























          • I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

            – shahir hajir
            Sep 12 '17 at 20:49











          • Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

            – Tomi
            Sep 13 '17 at 2:12











          • $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

            – Tomi
            Sep 13 '17 at 2:13



















          0














          Here I am sharing the script to get the configurable product with special price.



          magento2 SQL Query get special price for the configurable product with associated products:-



          SELECT e.sku,e.type_id, d1.value as price, d2.value as special_price from catalog_product_entity e LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
          AND d1.store_id = 0
          AND d1.attribute_id =75
          LEFT JOIN catalog_product_entity_decimal d2 ON e.entity_id = d2.entity_id
          AND d2.store_id = 0
          AND d2.attribute_id =76




          share























            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%2f192828%2fmagento-2-get-special-price-of-product-by-product-id-or-sku%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









            5














            Use MagentoCatalogModelProductRepository class to get special price of product



            protected $_productRepository;

            public function __construct(
            ...
            MagentoCatalogModelProductRepository $productRepository,
            ...
            ) {
            $this->_productRepository = $productRepository;
            }

            public function getSpecialPriceById($id)
            {
            //$id = '21'; //Product ID
            $product = $this->_productRepository->getById($id);
            $product->getSpecialPrice();
            return $specialPrice;
            }

            public function getSpecialPriceBySku($sku)
            {
            //$sku = 'test-21'; //Product Sku
            $product = $this->_productRepository->get($sku);
            $product->getSpecialPrice();
            return $specialPrice;
            }


            Now you can get special price by



            $id = '21';
            $sku = 'test-21';
            $this->getSpecialPriceById($id);
            $this->getSpecialPriceBySku($sku);





            share|improve this answer


























            • How to get special price of configurable product?

              – Chirag Patel
              Jan 11 at 13:49
















            5














            Use MagentoCatalogModelProductRepository class to get special price of product



            protected $_productRepository;

            public function __construct(
            ...
            MagentoCatalogModelProductRepository $productRepository,
            ...
            ) {
            $this->_productRepository = $productRepository;
            }

            public function getSpecialPriceById($id)
            {
            //$id = '21'; //Product ID
            $product = $this->_productRepository->getById($id);
            $product->getSpecialPrice();
            return $specialPrice;
            }

            public function getSpecialPriceBySku($sku)
            {
            //$sku = 'test-21'; //Product Sku
            $product = $this->_productRepository->get($sku);
            $product->getSpecialPrice();
            return $specialPrice;
            }


            Now you can get special price by



            $id = '21';
            $sku = 'test-21';
            $this->getSpecialPriceById($id);
            $this->getSpecialPriceBySku($sku);





            share|improve this answer


























            • How to get special price of configurable product?

              – Chirag Patel
              Jan 11 at 13:49














            5












            5








            5







            Use MagentoCatalogModelProductRepository class to get special price of product



            protected $_productRepository;

            public function __construct(
            ...
            MagentoCatalogModelProductRepository $productRepository,
            ...
            ) {
            $this->_productRepository = $productRepository;
            }

            public function getSpecialPriceById($id)
            {
            //$id = '21'; //Product ID
            $product = $this->_productRepository->getById($id);
            $product->getSpecialPrice();
            return $specialPrice;
            }

            public function getSpecialPriceBySku($sku)
            {
            //$sku = 'test-21'; //Product Sku
            $product = $this->_productRepository->get($sku);
            $product->getSpecialPrice();
            return $specialPrice;
            }


            Now you can get special price by



            $id = '21';
            $sku = 'test-21';
            $this->getSpecialPriceById($id);
            $this->getSpecialPriceBySku($sku);





            share|improve this answer















            Use MagentoCatalogModelProductRepository class to get special price of product



            protected $_productRepository;

            public function __construct(
            ...
            MagentoCatalogModelProductRepository $productRepository,
            ...
            ) {
            $this->_productRepository = $productRepository;
            }

            public function getSpecialPriceById($id)
            {
            //$id = '21'; //Product ID
            $product = $this->_productRepository->getById($id);
            $product->getSpecialPrice();
            return $specialPrice;
            }

            public function getSpecialPriceBySku($sku)
            {
            //$sku = 'test-21'; //Product Sku
            $product = $this->_productRepository->get($sku);
            $product->getSpecialPrice();
            return $specialPrice;
            }


            Now you can get special price by



            $id = '21';
            $sku = 'test-21';
            $this->getSpecialPriceById($id);
            $this->getSpecialPriceBySku($sku);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 12 '17 at 5:58

























            answered Sep 12 '17 at 5:52









            Prince PatelPrince Patel

            13.3k54776




            13.3k54776













            • How to get special price of configurable product?

              – Chirag Patel
              Jan 11 at 13:49



















            • How to get special price of configurable product?

              – Chirag Patel
              Jan 11 at 13:49

















            How to get special price of configurable product?

            – Chirag Patel
            Jan 11 at 13:49





            How to get special price of configurable product?

            – Chirag Patel
            Jan 11 at 13:49













            2














            use Magento/Catalog/Model/ProductFactory to load product, and get price information from the model



            Code is like:



            $specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');


            some reference about how to get product model in your custom module



            use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/



            use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/



            Normally dependency injection is recommended.






            share|improve this answer
























            • I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

              – shahir hajir
              Sep 12 '17 at 20:49











            • Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

              – Tomi
              Sep 13 '17 at 2:12











            • $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

              – Tomi
              Sep 13 '17 at 2:13
















            2














            use Magento/Catalog/Model/ProductFactory to load product, and get price information from the model



            Code is like:



            $specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');


            some reference about how to get product model in your custom module



            use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/



            use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/



            Normally dependency injection is recommended.






            share|improve this answer
























            • I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

              – shahir hajir
              Sep 12 '17 at 20:49











            • Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

              – Tomi
              Sep 13 '17 at 2:12











            • $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

              – Tomi
              Sep 13 '17 at 2:13














            2












            2








            2







            use Magento/Catalog/Model/ProductFactory to load product, and get price information from the model



            Code is like:



            $specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');


            some reference about how to get product model in your custom module



            use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/



            use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/



            Normally dependency injection is recommended.






            share|improve this answer













            use Magento/Catalog/Model/ProductFactory to load product, and get price information from the model



            Code is like:



            $specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');


            some reference about how to get product model in your custom module



            use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/



            use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/



            Normally dependency injection is recommended.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 12 '17 at 1:54









            TomiTomi

            1506




            1506













            • I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

              – shahir hajir
              Sep 12 '17 at 20:49











            • Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

              – Tomi
              Sep 13 '17 at 2:12











            • $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

              – Tomi
              Sep 13 '17 at 2:13



















            • I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

              – shahir hajir
              Sep 12 '17 at 20:49











            • Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

              – Tomi
              Sep 13 '17 at 2:12











            • $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

              – Tomi
              Sep 13 '17 at 2:13

















            I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

            – shahir hajir
            Sep 12 '17 at 20:49





            I am using Magento 2.1.7 and there is no class named ProductFactory under 'Magento/Catalog/Model/ProductFactory'. Can not find this class at 'vendormagentomodule-catalogModel'

            – shahir hajir
            Sep 12 '17 at 20:49













            Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

            – Tomi
            Sep 13 '17 at 2:12





            Factory class are automatically created by magento, so if you have a model like /vendor/magento/module_catalog/Model/Product, then you can call ProductFactory directly without creating actual class file.

            – Tomi
            Sep 13 '17 at 2:12













            $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

            – Tomi
            Sep 13 '17 at 2:13





            $this->productFactory()->create() will return the model Magento/Catalog/Model/Product

            – Tomi
            Sep 13 '17 at 2:13











            0














            Here I am sharing the script to get the configurable product with special price.



            magento2 SQL Query get special price for the configurable product with associated products:-



            SELECT e.sku,e.type_id, d1.value as price, d2.value as special_price from catalog_product_entity e LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
            AND d1.store_id = 0
            AND d1.attribute_id =75
            LEFT JOIN catalog_product_entity_decimal d2 ON e.entity_id = d2.entity_id
            AND d2.store_id = 0
            AND d2.attribute_id =76




            share




























              0














              Here I am sharing the script to get the configurable product with special price.



              magento2 SQL Query get special price for the configurable product with associated products:-



              SELECT e.sku,e.type_id, d1.value as price, d2.value as special_price from catalog_product_entity e LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
              AND d1.store_id = 0
              AND d1.attribute_id =75
              LEFT JOIN catalog_product_entity_decimal d2 ON e.entity_id = d2.entity_id
              AND d2.store_id = 0
              AND d2.attribute_id =76




              share


























                0












                0








                0







                Here I am sharing the script to get the configurable product with special price.



                magento2 SQL Query get special price for the configurable product with associated products:-



                SELECT e.sku,e.type_id, d1.value as price, d2.value as special_price from catalog_product_entity e LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
                AND d1.store_id = 0
                AND d1.attribute_id =75
                LEFT JOIN catalog_product_entity_decimal d2 ON e.entity_id = d2.entity_id
                AND d2.store_id = 0
                AND d2.attribute_id =76




                share













                Here I am sharing the script to get the configurable product with special price.



                magento2 SQL Query get special price for the configurable product with associated products:-



                SELECT e.sku,e.type_id, d1.value as price, d2.value as special_price from catalog_product_entity e LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
                AND d1.store_id = 0
                AND d1.attribute_id =75
                LEFT JOIN catalog_product_entity_decimal d2 ON e.entity_id = d2.entity_id
                AND d2.store_id = 0
                AND d2.attribute_id =76





                share











                share


                share










                answered 2 mins ago









                Saurabh-Magento2-DeveloperSaurabh-Magento2-Developer

                31




                31






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f192828%2fmagento-2-get-special-price-of-product-by-product-id-or-sku%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

                    Polycentropodidae

                    Magento 2 Error message: Invalid state change requested

                    Paulmy