Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetConversionValueObserver.php
Go to the documentation of this file.
1 <?php
9 
11 
13 {
17  protected $_helper;
18 
22  protected $_collection;
23 
31  public function __construct(
32  \Magento\GoogleAdwords\Helper\Data $helper,
33  \Magento\Framework\Registry $registry,
34  \Magento\Sales\Model\ResourceModel\Order\Collection $collection
35  ) {
36  $this->_helper = $helper;
37  $this->_collection = $collection;
38  $this->_registry = $registry;
39  }
40 
47  public function execute(\Magento\Framework\Event\Observer $observer)
48  {
49  if (!($this->_helper->isGoogleAdwordsActive() && $this->_helper->isDynamicConversionValue())) {
50  return $this;
51  }
52  $orderIds = $observer->getEvent()->getOrderIds();
53  if (!$orderIds || !is_array($orderIds)) {
54  return $this;
55  }
56  $this->_collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
57 
58  $conversionValue = 0;
59  $conversionCurrency = false;
60  $sendOrderCurrency = $this->_helper->hasSendConversionValueCurrency();
61  foreach ($this->_collection as $order) {
63  $conversionValue += $sendOrderCurrency ? $order->getGrandTotal() : $order->getBaseGrandTotal();
64  $conversionCurrency = $sendOrderCurrency ? $order->getOrderCurrencyCode() : false;
65  }
66  $this->_registry->register(
67  \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
68  $conversionCurrency
69  );
70  $this->_registry->register(
71  \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,
72  $conversionValue
73  );
74  return $this;
75  }
76 }
__construct(\Magento\GoogleAdwords\Helper\Data $helper, \Magento\Framework\Registry $registry, \Magento\Sales\Model\ResourceModel\Order\Collection $collection)
$helper
Definition: iframe.phtml:13
$order
Definition: order.php:55
$this _collection
Definition: coupons.php:7
$orderIds
Definition: results.phtml:9