0

I want to access product url from product collection after product save event in magento 2. i have try to some code but its return url with admin code and key.

i want to clean url no need to key and other paramenter in url.

event file

$product = $observer->getEvent()->getProduct();
echo "<pre>"; print_r($product->getProductUrl());

Get Following output :

admin/catalog/product/view/id/36/s/aim-analog-watch/key/7a1aeddd7ca27d6fc9daf52478db967c889d95d6d74350e6009ed44e06779fa5/

I Need to clean url

http://localhost/magento121new/aim-analog-watch.html

enter image description here

2
  • 1
    Please add your code and problem with the code to the question. Feb 21, 2017 at 8:39
  • Please see my updated question
    – Ajay Patel
    Feb 21, 2017 at 8:46

3 Answers 3

1

Save after is admin event so you can't get direct product url use below code

$id=$product->getId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

//Load product by product id
$productData = $objectManager->create('Magento\Catalog\Model\ProductRepository')->getById($id);

echo $p_url=$product->getUrlModel()->getUrl($productData);
5
1

Create events.xml at app/code/Vendor/Module/etc/adminhtml/events.xml as below

<?xml version="1.0"?>
<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="custom_event" instance="Vendor\Modulename\Model\Observer" />
    </event>   
</config>

Create Observer.php at app/code/Vendor/Module/Model/Observer.php as below

<?php
namespace Vendor\Modulename\Model;

use Magento\Framework\Event\ObserverInterface;

class Observer implements ObserverInterface {

    protected $catalogProductHelper;

    public function __construct(
        \Magento\Catalog\Helper\Product $catalogProductHelper
    ) {
        $this->catalogProductHelper = $catalogProductHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $product = $observer->getProduct();
       return $this->catalogProductHelper->getProductUrl($observer->getProduct()->getId());
    }
}
6
0

you can get the Product information after catalog_product_save_after Observer

<?php

namespace [VendorName]\[ModuleName]\Observer\Backend;


class AfterProductSaveObserver implements \Magento\Framework\Event\ObserverInterface{


/**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;


    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
    )
    {

        $this->_storeManager = $storeManagerInterface;
    }

    /**
     * Product after save event handler
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        /** @var $product \Magento\Catalog\Model\Product */
        $product = $observer->getEvent()->getProduct();
        $urlKey = $this->_storeManager->getStore(1)->getBaseUrl() . $product->getUrlKey();// it will return the product url

    }

}
5
  • I have used this but need product url on this event
    – Ajay Patel
    Feb 21, 2017 at 9:13
  • @AjayPatel see my updated answer.
    – Bojjaiah
    Feb 21, 2017 at 9:31
  • Its return url key how to concatenate url suffix
    – Ajay Patel
    Feb 21, 2017 at 9:32
  • @AjayPatel see my updated answer
    – Bojjaiah
    Feb 21, 2017 at 9:38
  • i have ask about suffix not prefix like .html or other
    – Ajay Patel
    Feb 21, 2017 at 9:39

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.