I want to pass custom params( product id and currency) with product page url dynamically. Example url:


Need help


Use below code to Pass parameter

$currency = 'GBP';
$parameter = ['id' => $id, 'currency' => $currency];
$resultRedirect->setPath('frontname/path/controllername', ['params' => $parameter]);
  • In which file this code should be place? – Shoaib Saleem Jun 25 at 7:10
  • Where you want add redirect code . like block or controller file – Shorabh Jun 25 at 7:13
  • I am new in magento 2, don't know where should be add this code to get example url. – Shoaib Saleem Jun 25 at 7:14
  • I think you want add parameter on product details page . I am write ? – Shorabh Jun 25 at 7:23
  • Yes.ID and currency(both) params on product detail page and just currency on all others pages. – Shoaib Saleem Jun 25 at 7:28

As you want to add id and currency then you have to work on Magento\Catalog\Model\Product\Url::getUrl()

Create Before plugin on the method getUrl and add currency add one of an index of parameter $params.

Declare the Plugin from di.xml and it code is below

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product\Url">
        <plugin disabled="false" name="StackExchange_MagentoAddress_Plugin_Magento_Catalog_Model_Product_Url" 
                        sortOrder="10" type="StackExchange\Magento\Plugin\ProductUrl"/>

Plugin Class

namespace StackExchange\Magento\Plugin;

class ProductUrl 

     * @var \Magento\Store\Model\StoreManagerInterface
    private $storeManager;

    public function __construct(
     \Magento\Store\Model\StoreManagerInterface $storeManager       

        $this->storeManager = $storeManager;
    public function beforeGetUrl(
            \Magento\Catalog\Model\Product\Url $subject,
            \Magento\Catalog\Model\Product $product,
            $params = []
    ) {

        if(empty($params) || (!empty($params) && !array_key_exists('_query', $params))){

            if(!array_key_exists('_query', $params)){
                $params['_query'] = [];
            if(!array_key_exists('currency', $params['_query'])){
                 $params['_query']['currency'] =$this->storeManager->getStore()->getCurrentCurrency()->getCode();


        return [
  • I tried but not getting any result. – Shoaib Saleem Jun 25 at 8:13
  • hmm i have checked that , seems it is not working. I will check again and will post answer – Amit Bera Jun 25 at 8:14
  • @shoaib-saleem,abve solution is worked – Amit Bera Jun 25 at 8:23
  • How to pass params in getUrl()?Can you describe a proper way?I am not good in Mag2. – Shoaib Saleem Jun 25 at 8:25
  • using before plugin, i have changes the parameter of this function – Amit Bera Jun 25 at 8:41

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.