Learn The Best Way to Pass Parameter to URL in Magento 2
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 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.
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.
Implement the below method to identify the particular product by passing a parameter through URL:
Method To Pass Parameter To URL In Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace Vendor\Module\Helper; use Magento\Framework\App\Helper\Context; use Magento\Framework\UrlInterface; class Data extends AbstractHelper { protected $urlBuilder; public function __construct( Context $context, UrlInterface $urlBuilder ) { $this->urlBuilder = $urlBuilder; parent::__construct($context); } public function GetParamUrl() { $queryParams = [ 'param_1' => value1, // value for parameter 'param_2' => value2 ]; return $this->urlBuilder->getUrl('route/controller/action', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]); //Ex: $this->urlBuilder->getUrl('custom/addcart/index', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]); } } |
1 |
$block->getUrl('route/controller/action',['param'=>'value']) |
I would be happy to help with any doubts on the topic mentioned in the Comments section below.
But if you want to avoid implementing the above code for selling Magento 2 store products on Facebook, simply opt for Magento 2 Facebook Shop Integration extension that will do the entire job for you!
The extension synchronizes the Magento 2 store products with the Facebook shop to sell on Facebook.
Do share the solution with fellow developers via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Use jQuery Library In Magento 2
How To Create Input Tag With Disabled Attribute In Magento system.xml
Next