Magento 2 - get special price of product by product id or sku
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
add a comment |
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
add a comment |
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
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
magento2 magento-2.1 product sku special-price
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
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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);
How to get special price of configurable product?
– Chirag Patel
Jan 11 at 13:49
add a comment |
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.
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
add a comment |
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
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%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
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);
How to get special price of configurable product?
– Chirag Patel
Jan 11 at 13:49
add a comment |
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);
How to get special price of configurable product?
– Chirag Patel
Jan 11 at 13:49
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 2 mins ago
Saurabh-Magento2-DeveloperSaurabh-Magento2-Developer
31
31
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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