Skip to content

Commit

Permalink
Merge pull request #6 from alexkoepke/master
Browse files Browse the repository at this point in the history
Add discountCode, discountPrice, and fix composer.json
  • Loading branch information
srenon authored Sep 29, 2016
2 parents 17dad36 + 369c19d commit 825ada4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
58 changes: 30 additions & 28 deletions app/code/local/MagePal/GoogleTagManager/Block/Tm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* See COPYING.txt for license details.
*/
class MagePal_GoogleTagManager_Block_Tm extends Mage_Core_Block_Template
{
{

/**
* Google Tag Manager Helper
*
Expand All @@ -27,39 +27,39 @@ class MagePal_GoogleTagManager_Block_Tm extends Mage_Core_Block_Template
* @var MagePal_TagManager_Model_DataLayer
*/
protected $_dataLayerModel = null;


protected $_customVariables = array();

protected $_orderCollection = null;


public function __construct() {

$this->_cookieHelper = Mage::helper('core/cookie');
$this->_gtmHelper = Mage::helper('googletagmanager');
$this->_dataLayerModel = Mage::getModel('googletagmanager/dataLayer');

$this->addVariable('ecommerce', array('currencyCode' => Mage::app()->getStore()->getCurrentCurrencyCode()));
}

/**
* Render information about specified orders and their items
*
*
* @return void|string
*/
protected function getOrdersTrackingCode()
{
$collection = $this->getOrderCollection();

if(!$collection){
return;
}

$result = array();

foreach ($collection as $order) {

foreach ($order->getAllVisibleItems() as $item) {
$product[] = array(
'sku' => $item->getSku(),
Expand All @@ -68,19 +68,21 @@ protected function getOrdersTrackingCode()
'quantity' => $item->getQtyOrdered()
);
}

$transaction = array(
'transactionId' => $order->getIncrementId(),
'transactionAffiliation' => Mage::app()->getStore()->getFrontendName(),
'transactionTotal' => $order->getBaseGrandTotal(),
'transactionShipping' => $order->getBaseShippingAmount(),
'discountCode' => $order->getCouponCode(),
'discountPrice' => $order->getDiscountAmount(),
'transactionProducts' => $product
);


$result[] = sprintf("dataLayer.push(%s);", json_encode($transaction));
}

return implode("\n", $result) . "\n";
}

Expand All @@ -103,47 +105,47 @@ protected function _toHtml() {
* @return json
*/
public function getGtmTrackingCode() {

Mage::dispatchEvent('magepal_data_layer', array('data_layer' => $this));

$result = array();
$result[] = sprintf("dataLayer.push(%s);\n", json_encode($this->_dataLayerModel->getVariables()));

if(!empty($this->_customVariables) && is_array($this->_customVariables)){

foreach($this->_customVariables as $custom){
$result[] = sprintf("dataLayer.push(%s);\n", json_encode($custom));
}
}

return implode("\n", $result) . "\n";
}

public function getQuote(){
return $this->_dataLayerModel->getQuote();
}

public function addVariable($name, $value) {
$this->_dataLayerModel->addVariable($name, $value);

return $this;
}

public function addCustomVariable($name, $value = null) {
if(is_array($name)){
$this->_customVariables[] = $name;
}
else{
$this->_customVariables[] = array($name => $value);
}

return $this;
}

public function formatPrice($price){
return $this->_dataLayerModel->formatPrice($price);
}

public function getOrderCollection(){
$orderIds = $this->getOrderIds();
if (empty($orderIds) || !is_array($orderIds)) {
Expand All @@ -155,8 +157,8 @@ public function getOrderCollection(){
->addFieldToFilter('entity_id', array('in' => $orderIds))
;
}

return $this->_orderCollection;
}

}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
"require": {
"php": ">=5.3.0"
},
"extra": {
"auto-append-gitignore": true
}
}

0 comments on commit 825ada4

Please sign in to comment.