2

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.

2

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);
    }
  • Can u tell me how can i add dynamic custom attribute for product – Parthavi Jul 29 at 10:09
  • Can you please explain more. Like dynamic or custom attribute – Anas Mansuri Jul 29 at 10:11
  • yes if i want to set multiple custom attribute by using setData() then how can i do ?Suppose i have 3 columns sku,custom_attribute1,custom_attribute2 then without specifying name of attribute can we save it dynamically ? – Parthavi Jul 29 at 10:13
  • Try with attribute code using foreach loop. – Anas Mansuri Jul 29 at 11:31
  • Can u let me know if i want to add category to product using above code how can i do it – Parthavi Jul 30 at 11:17
0

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

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.