0

We are working heavily and daily with the Magento 2 CSV import. We use something like this

"sku","qty"
"12345","100"

In this case, the qty for 12345 is set to be 100.

The next day I would like to upload something like this:

"sku","qty"
"12345","5"

The problem now is that 12345 will have 5 qty, and not 105 or ar at least 5 + (100 - sold qty).

Is there any way I can increase the quantity with the upload file?

Thanks

2 Answers 2

0

if you don't mind using third-party tools, you can use the Store Manager software and i think there must be an expression formula to add qty the way you need at the step of import

0

I was also stucked in same situation, after checking the core code got two solutions.

You have to make changes as according to your need.

My Module Name is Conceptive_Commerce, You have to replace that name with your module name

Method 1 - using plugin

app\code\Conceptive\Commerce\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ImportExport\Model\ResourceModel\Import\Data">
        <plugin name="rewrite_source_import_method" type="Conceptive\Commerce\Plugin\ImportData" sortOrder="10" disabled="false"  />
    </type>
</config>

app\code\Conceptive\Commerce\Plugin\ImportData.php

<?php

namespace Conceptive\Commerce\Plugin;

class ImportData
{
    protected $request;
    protected $sourceinventorys;

    public function __construct(
        \Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku $sourceinventorys,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->request = $request;
        $this->sourceinventorys = $sourceinventorys;
    }

    public function aftergetNextBunch(\Magento\ImportExport\Model\ResourceModel\Import\Data $subject, $result)
    {
        if (is_array($result)) {
            $params = $this->request->getParams();
            if ($params['entity'] == "stock_sources" && $params['behavior'] == "append") {
                foreach ($result as $key => $data) {
                    $sourceCode = $data['source_code'];
                    $sku = $data['sku'];
                    $stockSources = $this->sourceinventorys->execute($sku);
                    foreach ($stockSources as $stockSource) {
                        if ($stockSource['source_code'] == $sourceCode) {
                            $qty = $stockSource['quantity'];
                            if ($qty) {
                                $result[$key]['quantity'] =  $data['quantity'] + $qty;
                            }
                        }
                    }
                }
            }
        }
        return $result;
    }
}

Method 2 using prefrence

app\code\Conceptive\Commerce\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\ImportExport\Model\ResourceModel\Import\Data" type="Conceptive\Commerce\Rewrite\ImportData" />
</config>

app\code\Conceptive\Commerce\Rewrite\ImportData.php

<?php

namespace Conceptive\Commerce\Rewrite;

class ImportData extends \Magento\ImportExport\Model\ResourceModel\Import\Data
{
    /**
     * Get next bunch of validated rows.
     *
     * @return array|null
     */
    public function getNextBunch()
    {
        if (null === $this->_iterator) {
            $this->_iterator = $this->getIterator();
            $this->_iterator->rewind();
        }
        $dataRow = null;
        if ($this->_iterator->valid()) {
            $encodedData = $this->_iterator->current();
            if (array_key_exists(0, $encodedData) && $encodedData[0]) {
                $dataRow = $this->jsonHelper->jsonDecode($encodedData[0]);
                // custom code for increasing the quantity
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $params = $objectManager->get('Magento\Framework\App\Request\Http')->getParams();
                if ($params['entity'] == "stock_sources" && $params['behavior'] == "append") {
                    foreach ($dataRow as $key=>$data) {
                        $sourceCode = $data['source_code'];
                        $sku = $data['sku'];
                        $sourceinventorys = $objectManager->get('\Magento\InventoryCatalogAdminUi\Model\GetSourceItemsDataBySku');
                        $stockSources = $sourceinventorys->execute($sku);
                        foreach($stockSources as $stockSource) {
                            if ($stockSource['source_code'] == $sourceCode) {
                                $qty = $stockSource['quantity'];
                                if ($qty) {
                                    $dataRow[$key]['quantity'] =  $data['quantity'] + $qty;
                                }
                            }
                        }
                    }
                }
                // custom code end

                $this->_iterator->next();
            }
        }
        if (!$dataRow) {
            $this->_iterator = null;
        }
        return $dataRow;
    }
}

After adding files run the below commands

  1. bin/mgento setup:di:compile
  2. bin/magento cache:clean
  3. bin/magento cache:flush

Note : If not worked than try it by running upgrade and deploy command as well

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.