Site icon MageComp Blog

How To Pass Parameter To URL In Magento 2

How To Pass Parameter To URL In Magento 2

Hello Magento Folks?,

How are you all? Today in this article I will guide you through How To Pass Parameter To URL In Magento 2. Have you checked the previously placed article? Have a look at How to Install Magento SUPEE 11346 [With or Without SSH]. Let’s Dive In?.

Introduction: 

Nowadays, social media marketing is a very effective method to sell your store products on social media platforms like Facebook, Instagram, and many more platforms. It is very beneficial to grow your business and the customers also get a good customer experience. When any customer checks the product and willingly wants to purchase it then for that they click on that particular link and should be directly redirected to your Magento 2 store, and after that, the product must be added to the cart for placing the order. Mainly, Magento 2 requires recognizing the specific product which is added to the cart, and for that, you will need to pass the parameter to the URL. 

Steps to How To Pass Parameter To URL In Magento 2:

Apply the given below method for passing a parameter through the URL of any specific product.

<?php
namespace Vendor\Extension\Helper;


class Data extends AbstractHelper
{
    protected $urlBuilder;
    
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\UrlInterface $urlBuilder
    )
    {
        $this->urlBuilder = $urlBuilder;
        parent::__construct($context);
    }

    public function GetParamsUrl()
    {
        $queryParams = [
            'param_1' => value1, // value for parameter
            'param_2' => value2
        ];
        
       return $this->urlBuilder->getUrl('route/controller/action', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);
    }
}

That’s It.

Important Note: For phtml file, use:

$block->getUrl(‘route/controller/action’,[‘param’=>’value’])

Conclusion:

Hence, after the implementation of the above steps, you can easily Pass Parameter To URL In Magento 2 and increase your business growth and customer experience. Make sure you don’t forget to share the article with your developer friends and if any errors then I would love to hear in the comment section below.

Happy Coding?

Exit mobile version