Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Jul 15, 2023
2 parents 54f8074 + a508ae5 commit 49951c1
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 116 deletions.
2 changes: 1 addition & 1 deletion app/code/core/Mage/Checkout/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public function ajaxUpdateAction()
if ($qty == 0) {
$cart->removeItem($id);
} else {
$quoteItem->setQty($qty)->save();
$quoteItem->setQty($qty);
}
$this->_getCart()->save();

Expand Down
15 changes: 15 additions & 0 deletions app/code/core/Mage/Customer/Model/Resource/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,19 @@ public function getPasswordTimestamp($customerId)
}
return $value;
}

/**
* Get email by customer ID.
*
* @param int $customerId
* @return string|false
*/
public function getEmail($customerId)
{
$select = $this->_getReadAdapter()->select()
->from($this->getEntityTable(), 'email')
->where('entity_id = ?', $customerId);

return $this->_getReadAdapter()->fetchOne($select);
}
}
4 changes: 2 additions & 2 deletions app/code/core/Mage/Eav/Model/Attribute/Data/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class Mage_Eav_Model_Attribute_Data_Date extends Mage_Eav_Model_Attribute_Data_A
* Extract data from request and return value
*
* @param Zend_Controller_Request_Http $request
* @return array|string
* @return array|string|false
*/
public function extractValue(Zend_Controller_Request_Http $request)
{
$value = $this->_getRequestValue($request);
return $this->_applyInputFilter($value);
return $value ? $this->_applyInputFilter($value) : false;
}

/**
Expand Down
114 changes: 55 additions & 59 deletions app/code/core/Mage/GoogleAnalytics/Block/Ga.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,67 +131,57 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$request = $this->getRequest();
$moduleName = $request->getModuleName();
$controllerName = $request->getControllerName();
$helper = Mage::helper('googleanalytics');

/**
* This event signifies that an item was removed from a cart.
*
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#remove_from_cart
*/
$removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart();
$removedProducts = Mage::getSingleton('core/session')->getRemovedProductsForAnalytics();
if ($removedProducts) {
foreach ($removedProducts as $removedProduct) {
$_removedProduct = Mage::getModel('catalog/product')->load($removedProduct);
$eventData = [];
$eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2, '.', '');
$eventData['value'] = $helper->formatPrice($removedProduct['price'] * $removedProduct['qty']);
$eventData['items'] = [];
$_item = [
'item_id' => $_removedProduct->getSku(),
'item_name' => $_removedProduct->getName(),
'price' => number_format($_removedProduct->getFinalPrice(), 2, '.', ''),
'item_id' => $removedProduct['sku'],
'item_name' => $removedProduct['name'],
'price' => $helper->formatPrice($removedProduct['price']),
'quantity' => (int) $removedProduct['qty'],
'item_brand' => $removedProduct['manufacturer'],
'item_category' => $removedProduct['category'],
];
if ($_removedProduct->getAttributeText('manufacturer')) {
$_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer');
}
$itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_removedProduct);
if ($itemCategory) {
$_item['item_category'] = $itemCategory;
}
array_push($eventData['items'], $_item);
$eventData['items'][] = $_item;
$result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
}
Mage::getSingleton('core/session')->unsRemovedProductsCart();
Mage::getSingleton('core/session')->unsRemovedProductsForAnalytics();
}

/**
* This event signifies that an item was added to a cart for purchase.
*
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#add_to_cart
*/
$addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart();
$addedProducts = Mage::getSingleton('core/session')->getAddedProductsForAnalytics();
if ($addedProducts) {
foreach ($addedProducts as $addedProduct) {
$_addedProduct = Mage::getModel('catalog/product')->load($addedProduct);
foreach ($addedProducts as $_addedProduct) {
$eventData = [];
$eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2, '.', '');
$eventData['value'] = $helper->formatPrice($_addedProduct['price'] * $_addedProduct['qty']);
$eventData['items'] = [];
$_item = [
'item_id' => $_addedProduct->getSku(),
'item_name' => $_addedProduct->getName(),
'price' => number_format($_addedProduct->getFinalPrice(), 2, '.', ''),
'item_id' => $_addedProduct['sku'],
'item_name' => $_addedProduct['name'],
'price' => $helper->formatPrice($_addedProduct['price']),
'quantity' => (int) $_addedProduct['qty'],
'item_brand' => $_addedProduct['manufacturer'],
'item_category' => $_addedProduct['category'],
];
if ($_addedProduct->getAttributeText('manufacturer')) {
$_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer');
}

$itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_addedProduct);
if ($itemCategory) {
$_item['item_category'] = $itemCategory;
}
array_push($eventData['items'], $_item);
$eventData['items'][] = $_item;
$result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
Mage::getSingleton('core/session')->unsAddedProductsCart();
Mage::getSingleton('core/session')->unsAddedProductsForAnalytics();
}
}

Expand All @@ -205,14 +195,14 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$category = Mage::registry('current_category') ? Mage::registry('current_category')->getName() : false;
$eventData = [];
$eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$eventData['value'] = number_format($productViewed->getFinalPrice(), 2, '.', '');
$eventData['value'] = $helper->formatPrice($productViewed->getFinalPrice());
$eventData['items'] = [];
$_item = [
'item_id' => $productViewed->getSku(),
'item_name' => $productViewed->getName(),
'list_name' => 'Product Detail Page',
'item_category' => $category,
'price' => number_format($productViewed->getFinalPrice(), 2, '.', ''),
'price' => $helper->formatPrice($productViewed->getFinalPrice()),
];
if ($productViewed->getAttributeText('manufacturer')) {
$_item['item_brand'] = $productViewed->getAttributeText('manufacturer');
Expand Down Expand Up @@ -251,7 +241,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
'item_id' => $productViewed->getSku(),
'index' => $index,
'item_name' => $productViewed->getName(),
'price' => number_format($productViewed->getFinalPrice(), 2, '.', ''),
'price' => $helper->formatPrice($productViewed->getFinalPrice()),
];
if ($productViewed->getAttributeText('manufacturer')) {
$_item['item_brand'] = $productViewed->getAttributeText('manufacturer');
Expand All @@ -263,7 +253,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$index++;
$eventData['value'] += $productViewed->getFinalPrice();
}
$eventData['value'] = number_format($eventData['value'], 2, '.', '');
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
}

Expand All @@ -273,32 +263,34 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart
*/
elseif ($moduleName == 'checkout' && $controllerName == 'cart') {
$productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
$productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$eventData = [];
$eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$eventData['value'] = 0.00;
$eventData['items'] = [];

foreach ($productCollection as $productInCart) {
$_product = Mage::getModel('catalog/product')->load($productInCart->getProductId());
if ($productInCart->getParentItem()) {
continue;
}
$_product = $productInCart->getProduct();
$_item = [
'item_id' => $_product->getSku(),
'item_name' => $_product->getName(),
'price' => number_format($_product->getFinalPrice(), 2, '.', ''),
'price' => $helper->formatPrice($_product->getFinalPrice()),
'quantity' => (int) $productInCart->getQty(),
];
if ($_product->getAttributeText('manufacturer')) {
$_item['item_brand'] = $_product->getAttributeText('manufacturer');
}

$itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product);
$itemCategory = $helper->getLastCategoryName($_product);
if ($itemCategory) {
$_item['item_category'] = $itemCategory;
}
array_push($eventData['items'], $_item);
$eventData['value'] += $_product->getFinalPrice();
$eventData['value'] += $_product->getFinalPrice() * $productInCart->getQty();
}
$eventData['value'] = number_format($eventData['value'], 2, '.', '');
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'view_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
}

Expand All @@ -308,32 +300,34 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#begin_checkout
*/
elseif ($moduleName == static::CHECKOUT_MODULE_NAME && $controllerName == static::CHECKOUT_CONTROLLER_NAME) {
$productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
$productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
if ($productCollection) {
$eventData = [];
$eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode();
$eventData['value'] = 0.00;
$eventData['items'] = [];
foreach ($productCollection as $productInCart) {
$_product = Mage::getModel('catalog/product')->load($productInCart->getProductId());
if ($productInCart->getParentItem()) {
continue;
}
$_product = $productInCart->getProduct();
$_item = [
'item_id' => $_product->getSku(),
'item_name' => $_product->getName(),
'price' => number_format($_product->getFinalPrice(), 2, '.', ''),
'price' => $helper->formatPrice($_product->getFinalPrice()),
'quantity' => (int) $productInCart->getQty(),
];
if ($_product->getAttributeText('manufacturer')) {
$_item['item_brand'] = $_product->getAttributeText('manufacturer');
}

$itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product);
$itemCategory = $helper->getLastCategoryName($_product);
if ($itemCategory) {
$_item['item_category'] = $itemCategory;
}
array_push($eventData['items'], $_item);
$eventData['value'] += $_product->getFinalPrice();
}
$eventData['value'] = number_format($eventData['value'], 2, '.', '');
$eventData['value'] = $helper->formatPrice($eventData['value']);
$result[] = "gtag('event', 'begin_checkout', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");";
}
}
Expand All @@ -352,28 +346,30 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
$orderData = [
'currency' => $order->getBaseCurrencyCode(),
'transaction_id' => $order->getIncrementId(),
'value' => number_format($order->getBaseGrandTotal(), 2, '.', ''),
'coupon' => strtoupper($order->getCouponCode()),
'shipping' => number_format($order->getBaseShippingAmount(), 2, '.', ''),
'tax' => number_format($order->getBaseTaxAmount(), 2, '.', ''),
'value' => $helper->formatPrice($order->getBaseGrandTotal()),
'coupon' => strtoupper((string)$order->getCouponCode()),
'shipping' => $helper->formatPrice($order->getBaseShippingAmount()),
'tax' => $helper->formatPrice($order->getBaseTaxAmount()),
'items' => []
];

/** @var Mage_Sales_Model_Order_Item $item */
foreach ($order->getAllVisibleItems() as $item) {
foreach ($order->getAllItems() as $item) {
if ($item->getParentItem()) {
continue;
}
$_product = $item->getProduct();
$_item = [
'item_id' => $item->getSku(),
'item_name' => $item->getName(),
'quantity' => (int) $item->getQtyOrdered(),
'price' => number_format($item->getBasePrice(), 2, '.', ''),
'discount' => number_format($item->getBaseDiscountAmount(), 2, '.', '')
'price' => $helper->formatPrice($item->getBasePrice()),
'discount' => $helper->formatPrice($item->getBaseDiscountAmount())
];
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
if ($_product->getAttributeText('manufacturer')) {
$_item['item_brand'] = $_product->getAttributeText('manufacturer');
}

$itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product);
$itemCategory = $helper->getLastCategoryName($_product);
if ($itemCategory) {
$_item['item_category'] = $itemCategory;
}
Expand Down
9 changes: 9 additions & 0 deletions app/code/core/Mage/GoogleAnalytics/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,13 @@ public function getLastCategoryName($product): string
}
return '';
}

/**
* @param int|float|string $price
* @return string
*/
public function formatPrice($price): string
{
return number_format($price, 2, '.', '');
}
}
Loading

0 comments on commit 49951c1

Please sign in to comment.