11

How to get tier price of the product in Magento 2 with custom code?

This is my code

 public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
         $collection->setOrder('created_at', 'DESC');
        // $collection->setPageSize(3); // fetching only 3 products
        return $collection;
    }

and my call function in .phtml

  $productCollection = $block->getProductCollection();
   $productCollection->setPageSize(2);
            foreach ($productCollection as $product) {

 echo $product->getName();
 print_r($product->getTierPrice());

echo getName ,getPrice,getSpecial price works

but getTierPrice is not working.

ex. I have VIP member card (customer_group) I want to show tier price VIP member card to public not set all group I want to show tier price VIP member card

6

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.

enter image description here

Create constructor in your class and initiate the class object. From that Object you can do stuff.

  • it blank page when i run your code not echo tier price – tttk Dec 1 '16 at 7:22
  • @tttk see my updated answer. – Bojjaiah Dec 1 '16 at 7:37
  • sorry sir,do you have full code to echo tier_price ? it here my code foreach($tier_price as $pirces){ echo $prirces["price"]; //get the each one } – tttk Dec 1 '16 at 7:43
  • @tttk now try my code. – Bojjaiah Dec 1 '16 at 7:46
  • what is array_reverse($tier) ? Variables $tier? – tttk Dec 1 '16 at 8:00
6

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";
        }
    }
}
  • Works for me in Obsever – Ankit Shah Jan 26 '17 at 8:51
  • how to get all product with tier price. I want to show all the product hv tier price in different page – Daniel_12 Sep 12 '18 at 8:24
  • @Daniel_12, can we get the product price using customer group id? – jafar pinjar Feb 27 at 13:35
0

code from Bojjaiah works but it shows the tier prices like this: "2,252,132,001,88". How can I put them like this: Buy 1 for 2,52 / Buy 2 for 2,13 / Buy 3 for 2,00 / Buy 4 for 1,88? Thank you!

This is the original code from Bojjaiah:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.