0

How to fetch the current product id and add in this function?

Vendor\CatalogImportExport\Model\Import\Product.php

class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
   const PRODUCTID = 'product_id';
   const TABLE_Entity = 'temp_tgs';
   const COL_ENTITY_ID = 'entity_id';

   protected $validColumnNames = [
    self::SKU,
    self::NAME,
];

 protected $_permanentAttributes = [self::SKU];


public function getEntityTypeCode()
{
    return 'my_import';
}


protected function saveAndReplaceEntity()
{
    $behavior = $this->getBehavior();
    $listTitle = [];
    while ($bunch = $this->_dataSourceModel->getNextBunch()) {
        $entityList = [];
        foreach ($bunch as $rowNum => $rowData) {
            if (!$this->validateRow($rowData, $rowNum)) {
                $this->addRowError(ValidatorInterface::ERROR_PRODUCTID_IS_EMPTY, $rowNum);
                continue;
            }
            if ($this->getErrorAggregator()->hasToBeTerminated()) {
                $this->getErrorAggregator()->addRowToSkip($rowNum);
                continue;
            }

            $rowTtile= $rowData[self::PRODUCTID];
            $listTitle[] = $rowTtile;
            $entityList[$rowTtile][] = [
              self::PRODUCTID => $rowData[self::PRODUCTID],
              //self::SKU => $rowData[self::SKU],
            ];
        }
        if (\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE == $behavior) {
            if ($listTitle) {
                if ($this->deleteEntityFinish(array_unique(  $listTitle), self::TABLE_Entity)) {
                    $this->saveEntityFinish($entityList, self::TABLE_Entity);
                }
            }
        } elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $behavior) {
            $this->saveEntityFinish($entityList, self::TABLE_Entity);
        }
    }
    return $this;
}

protected function saveEntityFinish(array $entityData, $table)
{
    if ($entityData) {
        $tableName = $this->_connection->getTableName($table);
        $entityIn = [];
        foreach ($entityData as $id => $entityRows) {
                foreach ($entityRows as $row) {
                    $entityIn[] = $row;
                }
        }
        if ($entityIn) {
            $this->_connection->insertOnDuplicate($tableName, $entityIn,[
            self::SKU,
            self::COL_ENTITY_ID,   //trying this, but not saving
            //$this->_connection->lastInsertId(),
            ]);
        }
    }
    return $this; 

}
  • Are you in the product page? – Phoenix128_RiccardoT Aug 30 '16 at 10:11
  • Im in Import page..i want to insert the product id into my custom table during csv import...But default magento2 csv import file doesnt have product_id it has sku...So i need to get the product ids during import and save them...My custom table has only 1 columm (product_id) – Sushivam Aug 30 '16 at 10:14
  • Pls attach the filename – Phoenix128_RiccardoT Aug 30 '16 at 10:16
  • Please find the updated code along with saveEntityFinish() – Sushivam Aug 30 '16 at 10:23
  • 1
    You cannot do in that way because the entity is not yet created. Try $entityIn[self::PRODUCTID] – Phoenix128_RiccardoT Aug 30 '16 at 10:43
1

Try the following:

$myNewProductId = $this->_connection->lastInsertId();

Please try it before putting on production.

| improve this answer | |
  • Can u tell where in saveEntityFinish has this to be added? Cause there is one more _connection->getTableName – Sushivam Aug 30 '16 at 11:08
  • Instead of yout $productid=$product->getEntityId(); – Phoenix128_RiccardoT Aug 30 '16 at 11:09
  • This is what im getting systemException Additional data: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list', query was: INSERT INTO temp_tgs (sku) VALUES (?), (?), (?), (?) ON DUPLICATE KEY UPDATE sku = VALUES(sku), 0 = VALUES(0) – Sushivam Aug 30 '16 at 11:15
  • hey @Riccardo..i have updated the entire code here : magento.stackexchange.com/questions/133777/… im getting undefined index error for the entity_id column? – Sushivam Aug 30 '16 at 12:09
  • Any other approach @RiccardoT from the above updated answer!!!..it says undefined 'entity_id' – Sushivam Aug 30 '16 at 13:34

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.