1

I have many landing pages for products and product categories. These landing pages are promoted on Facebook, and when users click on the address, they are directed to our website. The URL address contains many parameters. If a user places an order after coming from a landing page, I want to save these parameters in the order table. How can I do this? How can I retrieve the passed parameters in the order module? Note:Magento2.3.4

1 Answer 1

1

To save the parameters in order details you have to follow below steps :

1. You need to save the url params in your cookie.

2. Create the field in order table.

3. Set that cookie in the field created at 2 step.

.

- Create {VENDOR_MODULE_PATH}/view/frontend/layout/cms_index_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>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="test" template="Vendor_Module::cookie.phtml"></block>
        </referenceContainer>
    </body>
</page>

- Create your template file {VENDOR_MODULE_PATH}/view/frontend/template/cookie.phtml

<?php
    $currentUrl = $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
    if(str_contains($currentUrl,'?utm')){
        $refineurl = explode('?',$currentUrl);
        setcookie("urlinfo", $refineurl[1]);
    }    
?>

Replace ?utm with your parameter.

- Create {VENDOR_MODULE_PATH}/etc/db_schema.xml file in your module

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order">
       <column xsi:type="varchar" name="urlinfo" nullable="false" length="255" comment="Title"/>
    </table>
</schema>

- Create a event file {VENDOR_MODULE_PATH}/etc/event.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="vendor_module_sales_url" instance="Vendor\Module\Observer\UrlBuilder" />
    </event>
</config>

- Create Observer {VENDOR_MODULE_PATH}/Observer/UrlBuilder.php and save your data in sales order table using the below code

<?php


namespace Vendor\Module\Observer;


use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;


class UrlBuilder implements ObserverInterface
{
    protected $logger;


    public function __construct(LoggerInterface $logger,
    \Magento\Framework\UrlInterface $urlInterface)
    {
        $this->urlInterface = $urlInterface;
        $this->logger = $logger;
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $order = $observer->getEvent()->getOrder();
            if(isset($_COOKIE["urlinfo"])){
            $order->setData('urlinfo',$_COOKIE["urlinfo"]);
            $order->save();
            }
        } catch (\Exception $e) {
            $this->logger->info($e->getMessage());
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.