Magento2 保存产品参数到订单表里面
Magento2 保存产品参数到订单表里面
我在Magento2.3.4 中 顾客点广告到达网站落地页的链接:https://www.aaa.com/pregnancy-announcement-dog-bandana?utm_source=Facebook&utm_medium=jack&utm_campaign=AAA&utm_content=201245785436&fbclid=fdggjghjtf5461231
如果顾客下单了,需要把utm_source,utm_medium,utm_campaign,utm_content这四个参数存到sale_orders表里。
我该怎么操作呢
admin 已回答
在 Magento 2.3.4 中,您可以通过以下几个步骤来捕获 URL 中的 UTM 参数并将其保存到 `sales_order` 表中:
步骤 1:获取 UTM 参数
在用户到达网站时,您可以使用前端代码(JavaScript)或后端代码(PHP)来获取这些 UTM 参数。
使用 JavaScript:
var urlParams = new URLSearchParams(window.location.search); var utm_source = urlParams.get('utm_source'); var utm_medium = urlParams.get('utm_medium'); var utm_campaign = urlParams.get('utm_campaign'); var utm_content = urlParams.get('utm_content');
使用 PHP:
$utm_source = $this->getRequest()->getParam('utm_source'); $utm_medium = $this->getRequest()->getParam('utm_medium'); $utm_campaign = $this->getRequest()->getParam('utm_campaign'); $utm_content = $this->getRequest()->getParam('utm_content');
步骤 2:保存 UTM 参数
一旦用户下单,您需要在订单保存事件中捕获这些参数。
您可以创建一个自定义模块并使用事件观察者来实现这一点。
在您的自定义模块中,创建或编辑 `etc/events.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_model_service_quote_submit_before"> <observer name="save_utm_to_order" instance="Vendor\Module\Observer\SaveUtmToOrder" /> </event> </config>
然后,创建观察者文件 `Observer/SaveUtmToOrder.php`:
namespace Vendor\Module\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class SaveUtmToOrder implements ObserverInterface { public function execute(Observer $observer) { $order = $observer->getEvent()->getOrder(); $quote = $observer->getEvent()->getQuote(); // 获取 UTM 参数(这里您需要根据实际情况来获取这些参数) $utm_source = /* 获取 utm_source */; $utm_medium = /* 获取 utm_medium */; $utm_campaign = /* 获取 utm_campaign */; $utm_content = /* 获取 utm_content */; // 保存到订单 $order->setData('utm_source', $utm_source); $order->setData('utm_medium', $utm_medium); $order->setData('utm_campaign', $utm_campaign); $order->setData('utm_content', $utm_content); } }
今生缘 编辑评论
在这个方法里面,我就是不知道该怎么去获取呢/ 获取 UTM 参数()
$utm_source = /* 获取 utm_source */;
$utm_medium = /* 获取 utm_medium */;
$utm_campaign = /* 获取 utm_campaign */;
$utm_content = /* 获取 utm_content */;
感觉cookle session 都不好使用呢