Skip to content

Commit 8065274

Browse files
committed
fix notices when product has no category, refactoring, fixes adrianspeyer#18
1 parent 90445b8 commit 8065274

File tree

1 file changed

+24
-19
lines changed
  • app/code/community/PiwikMage/PiwikAnalytics/Block

1 file changed

+24
-19
lines changed

app/code/community/PiwikMage/PiwikAnalytics/Block/Piwik.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ protected function _getOrdersTrackingCode()
4545
foreach ($order->getAllVisibleItems() as $item) {
4646

4747
//get category name
48-
$product_id = $item->product_id;
49-
$_product = Mage::getModel('catalog/product')->load($product_id);
50-
$cats = $_product->getCategoryIds();
51-
$category_id = $cats[0]; // just grab the first id
52-
$category = Mage::getModel('catalog/category')->load($category_id);
53-
$category_name = $category->getName();
48+
$productId = $item->product_id;
49+
$product = Mage::getModel('catalog/product')->load($productId);
50+
$categoryName = '';
51+
$categoryIds = $product->getCategoryIds();
52+
if (!empty($categoryIds)) {
53+
$categoryId = $categoryIds[0];
54+
$category = Mage::getModel('catalog/category')->load($categoryId);
55+
$categoryName = $category->getName();
56+
}
5457

5558

5659
if ($item->getQtyOrdered()) {
@@ -61,7 +64,7 @@ protected function _getOrdersTrackingCode()
6164
$result[] = sprintf("_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]);",
6265
$this->jsQuoteEscape($item->getSku()),
6366
$this->jsQuoteEscape($item->getName()),
64-
$category_name,
67+
$categoryName,
6568
$item->getBasePrice(),
6669
$qty
6770
);
@@ -101,12 +104,13 @@ protected function _getEcommerceCartUpdate()
101104
//get category name
102105
$productId = $cartItem->product_id;
103106
$product = Mage::getModel('catalog/product')->load($productId);
104-
$cats = $product->getCategoryIds();
105-
if (isset($cats)) {
106-
$categoryId = $cats[0];
107-
} // just grab the first id
108-
$category = Mage::getModel('catalog/category')->load($categoryId);
109-
$categoryName = $category->getName();
107+
$categoryName = '';
108+
$categoryIds = $product->getCategoryIds();
109+
if (!empty($categoryIds)) {
110+
$categoryId = $categoryIds[0];
111+
$category = Mage::getModel('catalog/category')->load($categoryId);
112+
$categoryName = $category->getName();
113+
}
110114
$productName = $cartItem->getName();
111115
$productName = str_replace('"', "", $productName);
112116

@@ -142,12 +146,13 @@ protected function _getProductPageview()
142146

143147
$productId = $currentProduct->getId();
144148
$product = Mage::getModel('catalog/product')->load($productId);
145-
$cats = $product->getCategoryIds();
146-
//$category_id = $cats[0]; grabs first category
147-
// $category_id = below fix when no catgeories
148-
if (isset($cats[0])) {$categoryId = $cats[0];} else {$categoryId = null;}
149-
$category = Mage::getModel('catalog/category')->load($categoryId);
150-
$categoryName = $category->getName();
149+
$categoryName = '';
150+
$categoryIds = $product->getCategoryIds();
151+
if (!empty($categoryIds)) {
152+
$categoryId = $categoryIds[0];
153+
$category = Mage::getModel('catalog/category')->load($categoryId);
154+
$categoryName = $category->getName();
155+
}
151156
$productName = $currentProduct->getName();
152157

153158
echo "_paq.push(['setEcommerceView', " . json_encode($currentProduct->getSku()) . ", " . json_encode($productName) . ", " . json_encode($categoryName) . ", " . $currentProduct->getPrice() . " ]);";

0 commit comments

Comments
 (0)