0

I've added on System > Data Transfer > Import another option in order to populate the table salesrule_coupon and i got on my csv the following:

rule_id,code,usage_limit,usage_per_customer,times_used,created_at,type
1,'R56Y2CU5FX8L',0,0,0,'2017-09-05 11:19:21',1
1,'705E77MMDK3E',0,0,0,'2017-11-22 22:09:57',1

On etc/import.xml:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
    <entity name="salesrule_coupon" label="Cupones" model="<Vendor>\<Module>\Model\Coupon" behaviorModel="Magento\ImportExport\Model\Source\Import\Behavior\Basic" />
</config>

then on Model Coupon:

<?php
/**
 * Created by PhpStorm.
 * User: oscar
 * Date: 22-01-19
 * Time: 11:07 AM
 */

namespace <Vendor>\<Module>\Model;


use <Vendor>\<Module>\Model\Coupon\RowValidatorInterface as ValidatorInterface;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;

class Coupon extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
    const ID = 'coupon_id';
    const RULE_ID = 'rule_id';
    const CODE = 'code';
    const USAGE_LIMIT = 'usage_limit';
    const USAGE_PER_CUSTOMER = 'usage_per_customer';
    const TIMES_USED = 'times_used';
    const CREATED_AT = 'created_at';
    const TYPE = 'type';

    const TABLE_Entity = 'salesrule_coupon';
    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = [
        ValidatorInterface::ERROR_CODE_IS_EMPTY => 'Code is empty',
    ];
    protected $_permanentAttributes = [self::CODE];
    /**
     * If we should check column names
     *
     * @var bool
     */
    protected $needColumnCheck = true;
    /**
     * Valid column names
     *
     * @array
     */
    protected $validColumnNames = [
        self::RULE_ID,
        self::CODE,
        self::USAGE_LIMIT,
        self::USAGE_PER_CUSTOMER,
        self::TIMES_USED,
        self::CREATED_AT,
        self::TYPE
    ];
    /**
     * Need to log in import history
     *
     * @var bool
     */
    protected $logInHistory = true;
    protected $_validators = [];
    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_connection;
    protected $_resource;
    /**
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    public function __construct(
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\ImportExport\Helper\Data $importExportData,
        \Magento\ImportExport\Model\ResourceModel\Import\Data $importData,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
        \Magento\Framework\Stdlib\StringUtils $string,
        ProcessingErrorAggregatorInterface $errorAggregator
    ) {
        $this->jsonHelper = $jsonHelper;
        $this->_importExportData = $importExportData;
        $this->_resourceHelper = $resourceHelper;
        $this->_dataSourceModel = $importData;
        $this->_resource = $resource;
        $this->_connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
        $this->errorAggregator = $errorAggregator;
    }
    public function getValidColumnNames()
    {
        return $this->validColumnNames;
    }
    /**
     * Entity type code getter.
     *
     * @return string
     */
    public function getEntityTypeCode()
    {
        echo "entro2";
        return 'salesrule_coupon';
    }
    /**
     * Row validation.
     *
     * @param array $rowData
     * @param int $rowNum
     * @return bool
     */
    public function validateRow(array $rowData, $rowNum)
    {
        $title = false;
        if (isset($this->_validatedRows[$rowNum])) {
            return !$this->getErrorAggregator()->isRowInvalid($rowNum);
        }
        $this->_validatedRows[$rowNum] = true;
        return !$this->getErrorAggregator()->isRowInvalid($rowNum);
    }
    /**
     * Create Advanced message data from raw data.
     *
     * @throws \Exception
     * @return bool Result of operation.
     */
    protected function _importData()
    {
        $this->saveEntity();
        return true;
    }
    /**
     * Save Message
     *
     * @return $this
     */
    public function saveEntity()
    {
        $this->saveAndReplaceEntity();
        return $this;
    }
    /**
     * Save and replace data message
     *
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    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, $rowNum);
                    continue;
                }
                if ($this->getErrorAggregator()->hasToBeTerminated()) {
                    $this->getErrorAggregator()->addRowToSkip($rowNum);
                    continue;
                }
                $rowCode= $rowData[self::CODE];
                $listTitle[] = $rowCode;
                $entityList[$rowCode][] = [
                    //self::ID => $rowData[self::ID],
                    self::RULE_ID => $rowData[self::RULE_ID],
                    self::CODE => $rowData[self::CODE],
                    self::USAGE_LIMIT => $rowData[self::USAGE_LIMIT],
                    self::USAGE_PER_CUSTOMER => $rowData[self::USAGE_PER_CUSTOMER],
                    self::TIMES_USED => $rowData[self::TIMES_USED],
                    self::CREATED_AT => $rowData[self::CREATED_AT],
                    self::TYPE => $rowData[self::TYPE],
                ];
            }
            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;
    }
    /**
     * Save message to customtable.
     *
     * @param array $priceData
     * @param string $table
     * @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::RULE_ID,
                    self::CODE,
                    self::USAGE_LIMIT,
                    self::USAGE_PER_CUSTOMER,
                    self::TIMES_USED,
                    self::CREATED_AT,
                    self::TYPE
                ]);
            }
        }
        return $this;
    }
}

And then when i click on Check Data returns the following: Notice: Undefined index: sku in ...vendor/magento/module-import-export/Model/Import/Entity/AbstractEntity.php on line 411

Please help! :( THanks

2 Answers 2

3

i got it solved i just changed the

\Magento\ImportExport\Model\Import\Entity\AbstractEntity

for

\Magento\ImportExport\Model\Import\AbstractEntity

Thanks! :)

1
  • this resolved the error, but after import is clicked facing this error,systemException Additional data: Error in data structure: entity values are mixed Sep 20, 2019 at 10:51
0

that solution didn't work for me, however i had to create a class that extends from: \Magento\ImportExport\Model\Import\Entity\AbstractEntity and then override the method _saveValidatedBunches , there is a line of code (411):

$skuSet[$rowData['sku']] = true;

so, I replaced that line by:

if (array_key_exists('sku', $rowData)) {
    $skuSet[$rowData['sku']] = true;
}

I got that solution from the official git issue https://github.com/magento/magento2/blob/2.2.10/app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

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.