3

I am able to add tier prices using csv

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

but it is taking "value_type" as Fixed, I want to dynamically give the value for "value_type"

2
  • rakeshjesadiya.com/… check the article for more details. Jan 20, 2020 at 10:59
  • I'm also looking for a solution to update tier price without call save() method. So, I used @embed0 suggested solution but exception accured: > The data was invalid. Verify the data and try again. in /vendor/magento/module-catalog/Model/Product/TierPriceManagement.php:99 Can you please specified the expected result for $productTierPrice argument of add() method?
    – Samuele
    Sep 22, 2021 at 11:46

3 Answers 3

2

I used Rakesh Jesadiya article information and done some debugging to create more "elegant" solution for percentage_value tier price type. Here is my code:

use Magento\Catalog\Api\Data\ProductTierPriceInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface;

class ProductPriceListsResolver
{
    /**
     * @var ScopedProductTierPriceManagementInterface
     */
    private $tierPriceManagement;
    /**
     * @var ProductTierPriceInterface
     */
    private $productTierPriceFactory;

    public function __construct(
        ScopedProductTierPriceManagementInterface $tierPriceManagement,
        ProductTierPriceInterfaceFactory $productTierPriceFactory
    ) {

        $this->tierPriceManagement = $tierPriceManagement;
        $this->productTierPriceFactory = $productTierPriceFactory;
    }

    public function resolve()
    {
        $sku = 'sku_83519'; //Product sku
        $customerGroupId = 6; //premium group id
        $price = 50.00;
        $qty = 1.00;

        /** @var ProductTierPriceInterface $productTierPrice */
        $productTierPrice = $this->productTierPriceFactory->create();
        $productTierPrice->setCustomerGroupId($customerGroupId)
            ->setQty($qty)
            ->setValue(20.00);

        $extensionAttributes = $productTierPrice->getExtensionAttributes();
        $extensionAttributes->setPercentageValue(20.00);
        $productTierPrice->setExtensionAttributes($extensionAttributes);
        try {
            $this->tierPriceManagement->add($sku, $productTierPrice);
        } catch (\Exception $e) {
            throw new $e;
        }
    }
}
1

I done the same by doing

$product_obj = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($prod_id);
    $tierPrices[] = array(
                                 'website_id'  => 0,
                                 'cust_group'  => 1,
                                 'price_qty'   => $qty,
                                 'price'       => $price,
                                 'percentage_value'  => $percentage);
                          $product_obj->setTierPrice($tierPrices);
                        $product_obj->save(); 
Here in the above array   `percentage_value` is given for the price range having discount type.         
0

@embed0's solution is largely correct, however may result in the issue @Samuele experienced "The data was invalid. Verify the data and try again.". Our solution on this was to floatval() both the qty and price values.

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