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();


    return $this;


  • Not sure how that's possible but have you considered using local storage instead? Jun 26, 2019 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, 2019 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. Jun 27, 2019 at 18:24
  • No I don't want that either. The data must come from the cookies.
    – Cypher909
    Jun 27, 2019 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.