How to get bundle product price by product id ? Magento2
-
20-03-2021 - |
Question
In the code below, I get the product price by identifier, but only the first product price is returned for different product id. Can help me ?
public function getBundlePriceByid($product_id){
$_store = 1;
$this->emulation->startEnvironmentEmulation($_store, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
$prices = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getValue();
$this->emulation->stopEnvironmentEmulation();
return $prices;
}
// 1 = 250$
// 7 = 260$
// 8 = 270$
$prIds= array(1,7,8);
foreach($prIds as $prId){
echo $this->getBundlePriceByid($prId);
}
returns only 250$
This code I call in Cron Job
Solution
Please try this:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
if ($product->getTypeId() == 'bundle') {
$regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
$specialPrice = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
}
Here product_id
is your product Id.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange