0

I have a custom cron to update/import bulk products using a csv file. All the products have tier pricing and different attribute sets assigned.

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;

/*
* @var ProductRepositoryInterface
*/
protected $productRepository;

/*
* @var ProductInterfaceFactory
*/
protected $productInterface;


$product = $this->productRepository->get($sku, ['edit_mode' => true]);

// code to set values in products

$this->productRepository->save($product);

This works fine until we get the different attribute set because $this->productRepository is the singleton object and saves the attribute set info of the first product it saves. Due to this for the product with new attribute set, can't match with old attribute set info and as a result tier price data is not fetched in the product object beforeSave method.

// File: vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php
/**
     * Check if attribute in specified set
     *
     * @param int|int[] $setId
     * @return bool
     */
    public function isInSet($setId)
    {
        if (!$this->hasAttributeSetInfo()) {
            return true;
        }

        if (is_array($setId) && count(array_intersect($setId, array_keys($this->getAttributeSetInfo())))) {
            return true;
        }

// fails here because $this->getAttributeSetInfo() has old attribute set info
        if (!is_array($setId) && array_key_exists($setId, $this->getAttributeSetInfo())) {
            return true;
        }

        return false;
    }

Above method will return false and as the result the product update script will break with below error.

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4949-0-0-1.0000-0' for key 'UNQ_E8AB433B9ACB00343ABB312AD2FAB087' in /var/www/html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91
Stack trace:
#0 /var/www/html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): PDOStatement->execute(Array)
#1 /var/www/html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(107): Magento\Framework\DB\Statement\Pdo\Mysql->Magento\Framework\DB\Statement\Pdo\{closure}()
#2 /var/www/html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(92): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute(Object(Closure))
#3 /var/www/html/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array)
#4 /var/www/html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#5 /var/www/html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_A in /var/www/html/vendor/magento/framework/EntityManager/Operation/Update.php on line 121

Can anyone help me on how to get new object of $this->productRepository for each product?

1 Answer 1

0

The message means you are doing another insert with the same combination of columns that are part of the UNQ_E8AB433B9ACB00343ABB312AD2FAB087, which seems to be defined as UNIQUE. If it is so, it doesn't allow to enter the same combination (it seems like it consists of two fields) twice. If you are inserting records, make sure you are picking a brand new record id or that the combination of the record id and the other column is unique.

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.