How to get Tier Price of product magento2?
To get the tier price for the customer groups, use getTierPrices() instead of getTierPrice(). Please see the example code below:
<?php
use \Magento\Framework\App\Bootstrap;
include('/www/magento2.1/app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productId = 1;
$product_obj = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
getAnyGroup($product_obj);
function getAnyGroup($product_obj) {
$tier_price = $product_obj->getTierPrices();
if(count($tier_price) > 0){
echo "price_qty\tprice\tCustomerGroupId\n";
foreach($tier_price as $price){
echo $price->getQty();
echo "\t";
echo $price->getValue();
echo "\t";
echo $price->getCustomerGroupId();
echo "\t";
echo "\n";
print_r($price->getData());
echo "\t";
echo "\n";
}
}
}
You can get the TierPrice as below.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product_obj = $objectManager->create('Magento\Catalog\Model\Product')->load(1);
$tier_price = $product_obj->getTierPrice();
if(count($tier_price) > 0){
foreach($tier_price as $pirces){
foreach ( array_reverse($pirces) as $k => $v ) {
if($k == "price"){
$tp = number_format($v, 2, '.', '');
echo $tp;
}
}
}
}
$product_obj
have a product data please check or debut it you can know. It will work 100%. see my log pic below.
Create constructor in your class and initiate the class object. From that Object you can do stuff.