Capturing URL Parameters in Magento: A Comprehensive Guide
Magento is a powerful eCommerce platform that allows you to capture various parameters from URLs and use them within your store operations. In this blog post, we will guide you through the process of capturing the resid parameter from URLs and saving it along with the order information.
Understanding the URL Parameters in Magento
In Magento, URLs can take several forms depending on the configuration of your store. The base URL structure is typically:
https://<your-domain>/index.php/<store-view-name>?<parameter>=<value>
Where:
<your-domain>is your domain name<store-view-name>is the code for the store view (optional, if you have multiple views)<parameter>and<value>are the key-value pairs of parameters in the query string
In your case, you want to capture the resid parameter from URLs like:
http://localhost.com/index.php/?resid=anyvalue
or
http://localhost.com/index.php/storeviewname/?resid=anyvalue
Capturing the URL Parameter
To capture the resid parameter, you need to use Magento’s core functionality. Here are the steps:
- Create a new module in your project using the Magento CLI command:
./bin/magento setup:module:create --name=YourCompany_CustomOrder
Replace YourCompany_CustomOrder with the desired name for your custom module.
- Navigate to the newly created module directory:
cd app/code/YourCompany/CustomOrder
- Open the
Registration.phpfile in theetcfolder and add the following content:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'YourCompany_CustomOrder',
__DIR__
);
- Create a new observer to capture the parameter when an order is placed. In your module directory, create a folder structure like this:
app/code/YourCompany/CustomOrder/etc
app/code/YourCompany/CustomOrder/etc/event_types.xml
- Add the following content to
event_types.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc event.xsd">
<event name="sales_order_place_after">
<observer instance="YourCompany\CustomOrder\Observer\SaveOrderResid" />
</event>
</config>
- Create the observer class
SaveOrderResidin the following folder structure:
app/code/YourCompany/CustomOrder/etc
app/code/YourCompany/CustomOrder/Observer
app/code/YourCompany/CustomOrder/Observer/SaveOrderResid.php
- Add the following content to
SaveOrderResid.php:
<?php
namespace YourCompany\CustomOrder\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Model\OrderFactory;
class SaveOrderResid implements ObserverInterface
{
private $orderFactory;
public function __construct(OrderFactory $orderFactory)
{
$this->orderFactory = $orderFactory;
}
public function execute(Observer $observer): void
{
/** @var \Magento\Sales\Model\Order $order */
$order = $observer->getEvent()->getOrder();
$resid = $order->getData('resid'); // Retrieve the resid value from the order data
if ($resid) {
$residValue = $this->processResidValue($resid);
$order->setResidValue($residValue); // Save the processed resid value to a custom attribute
$order->save();
}
}
private function processResidValue(string $resid): string
{
// Add your logic here to process the received resid value
return $resid;
}
}
- To create a custom attribute
resid_valuefor saving the processedresidvalue, you can use the Magento Admin interface or write an installer script.
Displaying the Captured Parameter in the Sales Order Grid
To display the captured resid value in the sales order grid, follow these steps:
- Create a new column to the sales order grid by adding an XML element in your module’s layout file:
app/code/YourCompany/CustomOrder/view/adminhtml/layout/sales_order_index.xml
- Add the following content to
sales_order_index.xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="sales_order_grid">
<columns>
<column name="resid_value">
<argument name="data" xsi:type="array">
<item name="header_text" xsi:type="string" translate="true">Resid Value</item>
<item name="sortable" xsi:type="boolean">false</item>
<item name="index_field" xsi:type="string">resid_value</item>
</argument>
</column>
</columns>
</referenceBlock>
</body>
</page>
This will add a new column named Resid Value to the sales order grid, displaying the processed resid value.
Conclusion
Capturing URL parameters in Magento allows you to enhance your store’s functionality by integrating external data with customer orders. By following this comprehensive guide, you can successfully capture the resid parameter from URLs and save it along with the order information.
Remember to test your implementation thoroughly and consult the official Magento documentation for more advanced customizations and best practices.
For further reading on Magento development and eCommerce solutions, visit our blog at Laravel Company.
Happy coding!