2

I am able 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"

1

3 Answers 3

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.         
1

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

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?

Your Answer

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

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