1

I have created 1 custom attribute (current_user_id) and saving value when product is saved. I have usecatalog_product_save_before event that save value for my custom attribute(current_user_id). code is

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $current_users=$this->authSession->getUser();
        $current_admin_id=$current_users->getData('user_id');
        $_product->setCurrentUserId($current_admin_id) ;
    }

It work perfect for add single product.

But now I want to add bulk products How this possible for import bulk product ?

I think we make custom attribute column in csv file and use which event that fire when import after check data. and foreach for setting value in custom attribute ?
Please Guide me I want to save custom attribute value in bulk products Thanks

1

AFAIK catalog_product_save_before event is not executing on the bulk import process this event is executing only when you save the product from backend.

The correct event would be catalog_product_import_bunch_save_after for the bulk import process

And from your question, you want to set the current admin user Id in current_user_id attribute so I think this is the perfect event for the hook.

So what you need to do create events.xml

<event name="catalog_product_import_bunch_save_after">
        <observer name="some_admin_user" instance="Example\Sample\Observer\AssignAdminId"/>
    </event>

Now you need to create observer AssignAdminId.php

In this observer, you can easily get the imported products and you can update product attributes by using mass action method 'updateAttributes' method

<?php

namespace Example\Sample\Observer;

use Magento\Catalog\Model\Product\Action;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AssignAdminId implements ObserverInterface
{

    private $import;

    /**
     * @var Action
     */
    private $massAction;

    /**
     * AssignAdminId constructor.
     * @param Action $massAction
     */
    public function __construct(
        Action $massAction
    )
    {
        $this->massAction = $massAction;
    }

    public function execute(Observer $observer)
    {
        $this->import = $observer->getEvent()->getAdapter();
        if ($products = $observer->getEvent()->getBunch()) {
            $array_product = [1,2,3,4,5]; //find imported product ID here
            $adminId = 1 ;// find current admin Id here
            $this->massAction->updateAttributes($array_product, array('current_user_id' => $adminId), 0);
        }
    }
}
  • well practic dear keyur Shah. let me try it . – HaFiz Umer Apr 17 at 12:56
  • but how can get imported ID product – HaFiz Umer Apr 17 at 12:56
  • I am not sure but you can find all products in $products object so please try to debug how you can get imported Ids but as per I know it should be easy – Keyur Shah Apr 17 at 12:57
  • events.xml will be in etc\events.xml or etc\adminhtml\events.xml ? – HaFiz Umer Apr 17 at 13:04
  • 1
    please check log, di:compile, cache:clean and try, how can I guide you without checking, – Keyur Shah Apr 17 at 13:35
1

Simply create the csv file with two fields "sku" & "custom_attribute" & add your values to that files as you want.

  • Go to the below location

Admin > SYSTEM > Data Transfer > Import

  • From the drop-down select product.
  • In Import behavior select Add/Update.
  • Chose your CSV file in Select File to Import.
  • Click on the check data for checking the data. (If facing any issue with the sheet try to solve it & try again)
  • Click on the Import Button.

You are done with the attributes value import for all the products.


Update

Your csv File will looks like this.
sku | Custom_Id
abc | 25
xyz | 25

You need to pass the sku & custom_attribute as header column & in every row it will have the sku & custom attribute values.

  • just 2 feilds ? and other products attributes ? and i want to save current login admin id as a value of custom_attribute then how can give value in csv file ? it should add on run time. may fire any event etc . . – HaFiz Umer Apr 17 at 11:39
  • as the operation is Add/Update so if the product is already present there in the magento it will simply update your existing product with that particular attribute value & if it is not there it will create a new product (In general case when import is done with all the other attribute values also) but in your case it will show error of product not exist. – ABHISHEK TRIPATHI Apr 17 at 11:44
  • Since this is a manual import process so there is no any default event is going to run with this import, it will simply add those values to your custom attributes filed which you have/will passed/pass through the csv file. – ABHISHEK TRIPATHI Apr 17 at 11:46
  • For getting the particular admin users Id you can simply get it from SYSTEM > Permission > All Users get the id from there & pass that value into your csv & import it, It will simply add that id to all the products Into your custom attribute field. Which you have passed in your csv file. – ABHISHEK TRIPATHI Apr 17 at 11:49
  • i actually i make multi vendor site . as you say each vendor manually give own id in csv file . – HaFiz Umer Apr 17 at 11:51

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.