{"id":693,"date":"2019-11-27T09:24:07","date_gmt":"2019-11-27T09:24:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/11\/27\/pass-parameter-to-url-in-magento-2\/"},"modified":"2025-06-05T09:43:40","modified_gmt":"2025-06-05T04:13:40","slug":"pass-parameter-to-url-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/pass-parameter-to-url-in-magento-2\/","title":{"rendered":"Learn The Best Way to Pass Parameter to URL in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Online multi-channel retailing can be interpreted as an approach to sale online store products on additional platforms like Instagram, Facebook, relevant marketplaces, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Magento 2 stores going for this approach needs to offer an excellent shopping experience. For example, a visitor checks your product on a Facebook shop and decides to buy it. On clicking it, he should be redirected to your store, and the product must be added to the cart for checkout.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, Magento 2 needs to identify the product that is to be added to the cart. And to do so, you need to pass parameter to URL in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the below method to identify the particular product by passing a parameter through URL:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Pass Parameter To URL In Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Framework\\UrlInterface;\n\nclass Data extends AbstractHelper\n{\n    protected $urlBuilder;\n    \n    public function __construct(\n        Context $context,\n        UrlInterface $urlBuilder\n    )\n    {\n        $this->urlBuilder = $urlBuilder;\n        parent::__construct($context);\n    }\n\n    public function GetParamUrl()\n    {\n        $queryParams = [\n            'param_1' => value1, \/\/ value for parameter\n            'param_2' => value2\n        ];\n        \n       return $this->urlBuilder->getUrl('route\/controller\/action', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);\n      \/\/Ex: $this->urlBuilder->getUrl('custom\/addcart\/index', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> For phtml file, you can use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$block->getUrl('route\/controller\/action',['param'=>'value'])<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But if you want to avoid implementing the above code for selling Magento 2 store products on Facebook, simply opt for <a href=\"https:\/\/meetanshi.com\/magento-2-facebook-shop-integration.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Facebook Shop Integration<\/a>&nbsp;extension that will do the entire job for you!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The extension synchronizes the Magento 2 store products with the Facebook shop to sell on Facebook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow developers via social media. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Online multi-channel retailing can be interpreted as an approach to sale online store products on additional platforms like Instagram, Facebook, relevant marketplaces, etc. For Magento&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":16405,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/693\/revisions\/16405"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}