0

How to get wishlist product details on wishlist_product_add_after event in magento2. Please suggest me.

1 Answer 1

3

Follow below steps to get current product details on wishlist_product_add_after event in magento2

Step 1 : Add event in etc/event.xml file.

<event name="wishlist_product_add_after">
    <observer instance="Vendor\ModuleName\Observer\Customer\Custom" name="wishlist_product_add_after_custom"/>
</event>

Step 2 : Create Custom.php event Observer file on the location Vendor/ModuleName/Observer/Customer/Custom.php and add below code on this file

namespace Vendor\ModuleName\Observer\Customer;

class Custom implements \Magento\Framework\Event\ObserverInterface
{
    protected $type;

    protected $productFactory;


    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $type
    ) {
        $this->productFactory = $productFactory;
        $this->type = $type;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     *
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $items = $observer->getItems();
        foreach ($items as $key => $item) {
            $wishlist_item = $item->getBuyRequest();

            $supper = $wishlist_item->getSuperAttribute();
            $product = $this->productFactory->create()->load($wishlist_item->getProduct());
            $product->getSku();

            if ($product->getTypeId()=="configurable") {
                if (!empty($supper)) {
                    $simple = $this->type->getProductByAttributes($supper,$product);
                    $simple->getSku();
                }
            }
            $item->getQty();
        }
    }
}
1
  • 1
    Thanks a lot! For me it worked. @saneer-ladani Commented Jan 23, 2019 at 10:24

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.