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.
Do share the solution with the fellow developers via social media.
Thanks.