1

We have created one module through which we can create upload images & from product detail page we can get the list of those images in a grid & whatever images we select those will be seen in front in product page.

This module works fine in Magento 2.0.x but not working in magento 2.1.x the problem is here :-

We have one observer which is being called when we save the product & in that we get the product id & the selected images ids....now the issue is we do not get images ids in 2.1.x

Event is : catalog_product_save_after

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="sticker" instance="Els\Sk\Observer\Adminhtml\Product" />
    </event>
</config>

Here is our Observer Code :-

<?php 
namespace Els\Sk\Observer\Adminhtml;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class Product implements ObserverInterface
{
    protected $resourceConnection;
    protected $eavAttributeFactory;
    protected $request;
    protected $_resources;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        \Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory
    ) {
       $this->request = $request;
       $this->resourceConnection = $resourceConnection;
       $this->eavAttributeFactory = $eavAttributeFactory;
    }


    /**
     * Saving product images data.
     *
     * @param EventObserver $observer
     * @return $this
     */
    public function execute(EventObserver $observer)
    {
        $product = $observer->getEvent()->getProduct();

        $product_id = $product->getId();

        /* We are not getting this in 2.1.x so this is the real issue */
        $images_ids = $this->request->getPost("images_id");

        $this->imageInsert($images_ids , $product_id);
    }
?>

Can any one please help with this?

Thank you.

  • can you please add full class so we can see dependency – Murtuza Zabuawala Aug 10 '17 at 14:16
  • Are you sure this Observer was call after you save the product ? – Toan Tam Aug 10 '17 at 17:45
  • Thank you Murtuza for looking into this :) Please see the updated question, we have added full definition of observer & also included observer event. – KA9 Aug 11 '17 at 5:58
  • Thank you Toan for looking into this :) Please see the updated question, we have added full definition of observer & also included observer event & yes it is being called after product save. – KA9 Aug 11 '17 at 5:58
1

Try below code.

protected $_request;

public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    \Psr\Log\LoggerInterface $logger
) { 
    $this->_request = $request;
    $this->_logger = $logger;
}

inside your execute function use below code to get post data.

$this->_request->getPost();

clear cache.

  • Hi Abhishek, we tried this but no luck :( Any other solution please? – KA9 Aug 12 '17 at 7:28

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.