0

I have created an observer that saves cookie data in the sales_order table on the sales_order_save_before event. I have tested that saving to the database in the custom fields works fine but I'm having inconsistent results with retrieving the actual cookie value. Sometimes it returns the cookie value and sometimes it returns nothing or what seems to be a cached value although I'm not sure how that's possible.

namespace Tss\AddDataToOrder\Observer\Sales;

class OrderSaveBefore implements \Magento\Framework\Event\ObserverInterface
{

protected $_cookieManager;

public function __construct(
    \Magento\Framework\HTTP\Header $httpHeader,
    \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
) {
    $this->httpHeader = $httpHeader;
    $this->_cookieManger = $cookieManager;
}

/**
 * Execute observer
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    /** @var \Magento\Sales\Model\Order $order */
    $order = $observer->getEvent()->getOrder();

    $order->setUtmCampaign($this->_cookieManger->getCookie('_uc_utm_campaign'));
    $order->setUtmContent($this->_cookieManger->getCookie('_uc_utm_content'));
    $order->setUtmMedium($this->_cookieManger->getCookie('_uc_utm_medium'));
    $order->setUtmSource($this->_cookieManger->getCookie('_uc_utm_source'));
    $order->setUtmTerm($this->_cookieManger->getCookie('_uc_utm_term'));
    $order->setUtmAdGroupId($this->_cookieManger->getCookie('_uc_adgroupid'));
    $order->setUtmCampaignId($this->_cookieManger->getCookie('_uc_campaignid'));
    $order->setUtmCreative($this->_cookieManger->getCookie('_uc_creative'));
    $order->setUtmDevice($this->_cookieManger->getCookie('_uc_device'));
    $order->setUtmKeyword($this->_cookieManger->getCookie('_uc_keyword'));
    $order->setUtmMatchType($this->_cookieManger->getCookie('_uc_matchtype'));
    $order->setUtmInitialReferrer($this->_cookieManger->getCookie('_uc_referrer'));
    $order->setUtmLastReferrer($this->_cookieManger->getCookie('_uc_last_referrer'));
    $order->setUtmInitialLandingPage($this->_cookieManger->getCookie('_uc_initial_landing_page'));
    $order->setUtmVisits($this->_cookieManger->getCookie('_uc_visits'));
    $order->setUserAgentString($this->httpHeader->getHttpUserAgent());

    return $this;
}

}

  • Not sure how that's possible but have you considered using local storage instead? – Dominic Xigen Jun 26 at 21:46
  • @DominicXigen I would have to post the data with javascript which I could also do with the cookies so no reason to use local storage. The issue here is I want to get the data on the server side using PHP. – Cypher909 Jun 27 at 18:00
  • As using cookies logic I assumed you wanted some form of frontend manipulation. I think you would be better using session logic. – Dominic Xigen Jun 27 at 18:24
  • No I don't want that either. The data must come from the cookies. – Cypher909 Jun 27 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.