Question

i need to set additional attribute column pragmatically using code in magento2 with import export module model file

my product custom attribute is "pre_order_qty"

if ($this->validateRow($rowData, $source->key())) {

// add row to bunch for save

$rowData = $this->_prepareRowForDb($rowData);

$pre_order_qty = $rowData['qty'];

$rowData['additional_attributes'] = 'pre_order_qty='.$pre_order_qty;

i need to set that "qty" value in "pre_order_qty"

here i share the screen shoot can any one help me how can set this

enter image description here

Was it helpful?

Solution

add this additional attribute code before

$rowData = $this->_prepareRowForDb($rowData);

file path

/vendor/magento/module-import-export/Model/Import/Entity/AbstractEntity.php

Function

protected function _saveValidatedBunches()
    {
        $source = $this->_getSource();
        $currentDataSize = 0;
        $bunchRows = [];
        $startNewBunch = false;
        $nextRowBackup = [];
        $maxDataSize = $this->_resourceHelper->getMaxDataSize();
        $bunchSize = $this->_importExportData->getBunchSize();
        $skuSet = [];

        $source->rewind();
        $this->_dataSourceModel->cleanBunches();

        while ($source->valid() || $bunchRows) {
            if ($startNewBunch || !$source->valid()) {
                $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);

                $bunchRows = $nextRowBackup;
                $currentDataSize = strlen($this->getSerializer()->serialize($bunchRows));
                $startNewBunch = false;
                $nextRowBackup = [];
            }
            if ($source->valid()) {
                try {
                    $rowData = $source->current();
                    if (array_key_exists('sku', $rowData)) {
                        $skuSet[$rowData['sku']] = true;
                    }
                } catch (\InvalidArgumentException $e) {
                    $this->addRowError($e->getMessage(), $this->_processedRowsCount);
                    $this->_processedRowsCount++;
                    $source->next();
                    continue;
                }

                $this->_processedRowsCount++;

                if ($this->validateRow($rowData, $source->key())) {
                    // add row to bunch for save

                    $pre_order_qty =  $rowData['qty'];
                    if($rowData['additional_attributes'])
                    {
                        if($rowData['additional_attributes'] != '')
                        {
                            $rowData['additional_attributes'] = $rowData['additional_attributes'].',preorder_custom_qty='.$pre_order_qty;
                        }
                        else
                        {
                           $rowData['additional_attributes'] = 'preorder_custom_qty='.$pre_order_qty; 
                        }
                    }
                    else
                    {
                        $rowData['additional_attributes'] = 'preorder_custom_qty='.$pre_order_qty; 
                    }
                    

                    $rowData = $this->_prepareRowForDb($rowData);

                    


                    $rowSize = strlen($this->jsonHelper->jsonEncode($rowData));

                    $isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize;

                    if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) {
                        $startNewBunch = true;
                        $nextRowBackup = [$source->key() => $rowData];
                    } else {
                        $bunchRows[$source->key()] = $rowData;
                        $currentDataSize += $rowSize;
                    }
                }
                $source->next();
            }
        }
        $this->_processedEntitiesCount = (count($skuSet)) ? : $this->_processedRowsCount;
                   
                   
   
      
        return $this;
    }

the main code is

 $pre_order_qty =  $rowData['qty'];
            if($rowData['additional_attributes'])
            {
                if($rowData['additional_attributes'] != '')
                {
                    $rowData['additional_attributes'] = $rowData['additional_attributes'].',preorder_custom_qty='.$pre_order_qty;
                }
                else
                {
                   $rowData['additional_attributes'] = 'preorder_custom_qty='.$pre_order_qty; 
                }
            }
            else
            {
                $rowData['additional_attributes'] = 'preorder_custom_qty='.$pre_order_qty; 
            }
            

            $rowData = $this->_prepareRowForDb($rowData);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top