Frage

I am working on CSV import to add custom attribute data for Products. I have created a custom attribute as "slide_link". I am unable to add only SKU and slide_link in CSV for adding data of attribute in the product.

Please provide a solution for above.

War es hilfreich?

Lösung

Please analyse below demo its working to update price and qty using sku ( specific SKU ) via CSV, I hope its helpful to you.. ( its just idea how to work in magento2 )

<?php
    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';

    $params = $_SERVER;
    $bootstrap = Bootstrap::create(BP, $params);

    $obj = $bootstrap->getObjectManager();

    $state = $obj->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $productRepository = $obj->get('Magento\Catalog\Model\ProductRepository');
    $stockRegistry = $obj->get('Magento\CatalogInventory\Api\StockRegistryInterface');

    $csv = "csvfilename.csv";
    if (!empty($argv) && sizeof($argv) > 1) {
        $csv = $argv[1];
    }
    if (($handle = fopen($csv, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 2000, "\t")) !== FALSE) {
            $num = count($data);
            if ($num < 1) {
                continue;
            }
            $sku = trim($data[0]);
            if ($num < 2) {
                echo "Skipping: " . $sku . " not enough fields\n";
                continue;
            }
            $qty = trim($data[1]);
            $price = trim($data[2]);

            try {
                $product = $productRepository->get($sku);
            } catch (\Exception $e) {
                echo "Error:  Invalid SKU, ".$sku."\n";
                continue;
            }

            if ($product->getPrice() != $price) {
                $product->setPrice($price);
                $product->save();
            }

            try {
                $stockItem = $stockRegistry->getStockItemBySku($sku);
            } catch (\Exception $e) {
                echo "Error:  Invalid stock SKU, ".$sku."\n";
                continue;
            }

            if ($stockItem->getQty() != $qty) {
                $stockItem->setQty($qty);
                if ($qty > 0) {
                    $stockItem->setIsInStock(1);
                }
                $stockRegistry->updateStockItemBySku($sku, $stockItem);
            }
        }
        fclose($handle);
    }

Andere Tipps

I am not sure why stock info is present in code of previous answer. There are easier, and surely faster, ways to update attributes values (but depending by the attribute type it could need some extra code)

Using \Magento\Catalog\Model\ResourceModel\Product\Action & \Magento\Catalog\Model\Product\Attribute\Repository

You can just do

$product = $this->_productRepository->get($sku);
$this->_action->updateAttributes(
    array($product->getId()),
    array('YOUR_ATTRIBUTE_CODE' => $YOUR_ATTRIBUTE_VALUE),
    1
);

That last parameter is store_id, in this example: 1

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top