Skip to content

Commit bccc52a

Browse files
committed
Fixes to get cart items to account for checkout and confirmation pages.
1 parent b5751d2 commit bccc52a

File tree

1 file changed

+66
-43
lines changed
  • app/code/local/Smashmetrics/Rekko/Block

1 file changed

+66
-43
lines changed

app/code/local/Smashmetrics/Rekko/Block/Rekko.php

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,45 @@ public function getOrderDetails()
4747
return $orderDet;
4848
}
4949

50-
if ($module == 'checkout') {
51-
//This is an order confirmation page.
52-
if ($controller == 'onepage' && $action == 'success') {
53-
Mage::log("Getting order details on confirmation page.");
54-
$lastOrderId = Mage::getSingleton('checkout/session')
55-
->getLastRealOrderId();
56-
$orderId = Mage::getModel('sales/order')
57-
->loadByIncrementId($lastOrderId)
58-
->getEntityId();
59-
$order = Mage::getModel('sales/order')->load($orderId);
60-
$_totalData = $order->getData();
61-
62-
$orderDet['grand_total'] = $_totalData['base_grand_total'];
63-
$orderDet['coupon_code'] = $_totalData['coupon_code'];
64-
$orderDet['discount'] = abs($_totalData['base_discount_amount']);
65-
$orderDet['orderid'] = $_totalData['increment_id'];
66-
$orderDet['shipping_total'] = $_totalData['base_shipping_amount'];
67-
68-
// TODO: quote_id is used by phtml to determine whether there was a purchase or not.
69-
// this is bad and should be replaced by a proper object.
70-
$orderDet['quote_id'] = 'successpage';
71-
if ($action == 'success') {
72-
$orderDet['orderid'] = $_totalData['increment_id'];
73-
}
74-
} else {
75-
//Anywhere else in the checkout process.
76-
Mage::log("Getting order details on the checkout process (before confirmation page)");
77-
78-
$cart = Mage::getSingleton('checkout/session');
79-
$quote_id = $cart->getQuoteId();
80-
$item_quote = Mage::getModel('sales/quote')->load($quote_id);
81-
82-
$orderDet['grand_total'] = $item_quote->grand_total;
83-
$orderDet['coupon_code'] = $item_quote->coupon_code;
84-
$orderDet['discount'] = $item_quote->subtotal - $item_quote->subtotal_with_discount;
85-
$orderDet['quote_id'] = $quote_id;
86-
$orderDet['orderid'] = '';
87-
$orderDet['shipping_total'] = '';
50+
//If this is an order confirmation page.
51+
if ($controller == 'onepage' && $action == 'success') {
52+
Mage::log("Getting order details on confirmation page.");
53+
$lastOrderId = Mage::getSingleton('checkout/session')
54+
->getLastRealOrderId();
55+
$orderId = Mage::getModel('sales/order')
56+
->loadByIncrementId($lastOrderId)
57+
->getEntityId();
58+
$order = Mage::getModel('sales/order')->load($orderId);
59+
$_totalData = $order->getData();
60+
61+
$orderDet['grand_total'] = $_totalData['base_grand_total'];
62+
$orderDet['coupon_code'] = $_totalData['coupon_code'];
63+
$orderDet['discount'] = abs($_totalData['base_discount_amount']);
64+
$orderDet['orderid'] = $_totalData['increment_id'];
65+
$orderDet['shipping_total'] = $_totalData['base_shipping_amount'];
66+
67+
// TODO: quote_id is used by phtml to determine whether there was a purchase or not.
68+
// this is bad and should be replaced by a proper object.
69+
$orderDet['quote_id'] = 'successpage';
70+
if ($action == 'success') {
71+
$orderDet['orderid'] = $_totalData['increment_id'];
8872
}
73+
} else {
74+
//Anywhere else in the checkout process.
75+
Mage::log("Getting order details on the checkout process (before confirmation page)");
76+
77+
$cart = Mage::getSingleton('checkout/session');
78+
$quote_id = $cart->getQuoteId();
79+
$item_quote = Mage::getModel('sales/quote')->load($quote_id);
80+
81+
$orderDet['grand_total'] = $item_quote->grand_total;
82+
$orderDet['coupon_code'] = $item_quote->coupon_code;
83+
$orderDet['discount'] = $item_quote->subtotal - $item_quote->subtotal_with_discount;
84+
$orderDet['quote_id'] = $quote_id;
85+
$orderDet['orderid'] = '';
86+
$orderDet['shipping_total'] = '';
8987
}
88+
9089
return $orderDet;
9190

9291
}
@@ -137,15 +136,22 @@ public function getCartItems()
137136
{
138137
$request = $this->getRequest();
139138
$module = $request->getModuleName();
139+
$controller = $request->getControllerName();
140+
$action = $request->getActionName();
140141
$cart = Mage::getSingleton('checkout/session');
141142

142143
$product = array();
143144

144145
//Only tag on checkout pages.
145-
if ($module == 'checkout') {
146-
Mage::log("Getting cart items for a checkout");
146+
if ($module != 'checkout') {
147+
Mage::log("Not in a cart module. No need to tag order details");
148+
return $product;
149+
}
150+
151+
//If this is an order confirmation page.
152+
if ($controller == 'onepage' && $action == 'success') {
153+
Mage::log("Getting cart items on confirmation page.");
147154
$orderId = $cart->getLastOrderId();
148-
$lastOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
149155
$order = Mage::getModel('sales/order')->load($orderId);
150156
$ordered_items = $order->getAllItems();
151157
$i = 0;
@@ -165,10 +171,27 @@ public function getCartItems()
165171

166172
$i++;
167173
}
168-
return $product;
174+
} else {
175+
//We must be still in the checkout process.
176+
Mage::log("Getting cart items on the checkout process (before confirmation page)");
177+
$i = 0;
178+
foreach ($cart->getQuote()->getAllItems() as $item) {
179+
$product[$i]['productId'] = $item->getProductId();
180+
$product[$i]['sku'] = $item->getSku();
181+
$product[$i]['name'] = $item->getName();
182+
$product[$i]['qty'] = $item->getQty();
183+
$product[$i]['price'] = $item->getPrice();
184+
$catIds = $item->getProduct()->getCategoryIds();
185+
$category = array();
186+
foreach ($catIds AS $cid) {
187+
$category[] = Mage::getModel('catalog/category')->load($cid)->getName();
188+
}
189+
$product[$i]['category'] = implode(',', $category);
190+
$i++;
191+
}
169192
}
170193

171-
Mage::log("Not in a cart module. No need to tag cart items");
194+
return $product;
172195
}
173196

174197
}

0 commit comments

Comments
 (0)