1

I have below snippet which I am using to update tier prices programaticaly:

$tierPricenew[0] = array('website_id' => 2,'cust_group' => $groupId, 'price_qty' => 1 , 'price' => floatval($price) );
$productObj->setTierPrice($tierPricenew);
$prices = $productObj->getTierPrice();
$productObj->unsetData('media_gallery');
$productObj->save();

This however takes very long with lots of products and so want to just update the tier_price attribute alone as suggested elsewhere on stack exchange to quicken the whole process rather than saving the whole product object.

I have tried below methods saveAttribute:

$tierPricenew[0] = array('website_id' => 2,'cust_group' => $groupId, 'price_qty' => 1 , 'price' => floatval($price) );
$productObj->setTierPrice($tierPricenew);
$prices = $productObj->getTierPrice();
$productObj->setData('tier_price', $prices);
$this->productResource->saveAttribute($productObj, 'tier_price');

And this method updateAttributes:

$tierPricenew[0] = array('website_id' => 2,'cust_group' => $groupId, 'price_qty' => 1 , 'price' => floatval($price) );
$productObj->setTierPrice($tierPricenew);
$prices = $productObj->getTierPrice();
$this->productResourceModel->updateAttributes([$productObj->getId()], ['tier_price' => $prices]);

None of the above however seem to work for me. No errors are thrown however tier prices are not set. They work with other attributes like name etc but not tier_prices. Is there a way to update these quickly with methods like above?

I have looked into the Magento\Catalog\Api\ProductTierPriceManagementInterface class and tried with the remove method here however looking at this just seems to do very similar to my first method and saves the complete product object.

$this->tierPrice->remove($product->getSku(), $groupId, '1');

Import seems to do this so fast so feel there must be a quicker method to use here.

1 Answer 1

2

I think this should work for you.

 <?php 
namespace Test\Sample\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Catalog\Api\ProductTierPriceManagementInterface;

class Index extends Action
{

    protected $tierPrice;

    public function __construct(Context $context, ProductTierPriceManagementInterface $tier)
    {
        $this->tierPrice = $tier;
        parent::__construct($context);
    }

     public function execute()
    {
        $sku = 'test-sku'; //Product sku
        $customerGroupId = 2; //Wholsaler group id
        $price = 200;
        $qty = 55;

        $this->tierPrice->add($sku,$customerGroupId,$price, $qty);
    }
}

I found the answer at this location Magento 2: How to set Tier Pricing Programatically Group Wise

1
  • 1
    This isn't fast. Takes 1 sec to update 1 price atleast in my env. Which is quite slow. Nov 25, 2021 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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