Skip to content

Commit 6dabe3d

Browse files
committed
Improved Basket Collection
1 parent 050a483 commit 6dabe3d

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

code/controllers/BasketController.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class Clerk_Clerk_BasketController extends Mage_Core_Controller_Front_Action{
4+
5+
public function basketAction()
6+
{
7+
8+
if (Mage::helper('clerk')->getSetting('clerk/general/collect_baskets', Mage::app()->getStore()->getId()) == '1') {
9+
foreach (Mage::app()->getWebsites() as $website) {
10+
foreach ($website->getGroups() as $group) {
11+
$stores = $group->getStores();
12+
foreach ($stores as $store) {
13+
$cart_products = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
14+
$cart_product_ids = array();
15+
foreach ($cart_products as $product) {
16+
if (!in_array($product->getProduct()->getId(), $cart_product_ids)) {
17+
$cart_product_ids[] = $product->getProduct()->getId();
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
25+
echo implode(',',$cart_product_ids);
26+
27+
}
28+
29+
}

design/frontend/template/tracking.phtml

+98
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
55
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
66
$currentCurrencySymbol = Mage::app()->getLocale()->currency($currentCurrencyCode)->getSymbol();
77
$rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, $currentCurrencyCode);
8+
9+
if (Mage::helper('clerk')->getSetting('clerk/general/collect_baskets', Mage::app()->getStore()->getId()) == '1') {
10+
11+
$cart_products = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
12+
$cart_product_ids = array();
13+
foreach ($cart_products as $product) {
14+
if (!in_array($product->getProduct()->getId(), $cart_product_ids)) {
15+
$cart_product_ids[] = $product->getProduct()->getId();
16+
}
17+
}
18+
}
19+
20+
821
?>
922
<script type="text/javascript">
1023
(function(w,d){
@@ -38,4 +51,89 @@ $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCod
3851
formkey: '<?php echo $this->getFormKey(); ?>'
3952
}
4053
});
54+
55+
<?php if(Mage::helper('clerk')->getSetting('clerk/general/collect_baskets', Mage::app()->getStore()->getId()) == '1') : ?>
56+
57+
var clerk_productids = '<?php echo implode(',',$cart_product_ids); ?>'.split(",") ;
58+
clerk_productids = clerk_productids.map(Number);
59+
var clerk_last_productids = [];
60+
if( localStorage.getItem('clerk_productids') !== null ){
61+
clerk_last_productids = localStorage.getItem('clerk_productids').split(",");
62+
clerk_last_productids = clerk_last_productids.map(Number);
63+
}
64+
clerk_productids = clerk_productids.sort((a, b) => a - b);
65+
clerk_last_productids = clerk_last_productids.sort((a, b) => a - b);
66+
if(JSON.stringify(clerk_productids) == JSON.stringify(clerk_last_productids)){
67+
// if equal - do nothing
68+
}else{
69+
// if not equal send cart to clerk
70+
if(JSON.stringify(clerk_productids) === "[0]" ){
71+
Clerk('cart', 'set', []);
72+
}else{
73+
Clerk('cart', 'set', clerk_productids);
74+
}
75+
}
76+
localStorage.setItem("clerk_productids", clerk_productids);
77+
78+
let open = XMLHttpRequest.prototype.open;
79+
XMLHttpRequest.prototype.open = function() {
80+
this.addEventListener("load", function(){
81+
82+
if( this.responseURL.includes("/cart")){
83+
84+
data = "form_key='<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>'";
85+
86+
const request = new XMLHttpRequest();
87+
88+
request.addEventListener('load', function () {
89+
90+
if( this.responseURL.includes("/basket")){
91+
92+
if (this.readyState === 4 && this.status === 200) {
93+
94+
var response = this.responseText.replace('[', '').replace(']', '');
95+
var clerk_productids = [];
96+
clerk_productids = response.split(",")
97+
clerk_productids = clerk_productids.map(Number);
98+
99+
var clerk_last_productids = [];
100+
if( localStorage.getItem('clerk_productids') !== null ){
101+
clerk_last_productids = localStorage.getItem('clerk_productids').split(",");
102+
clerk_last_productids = clerk_last_productids.map(Number);
103+
}
104+
//sort
105+
clerk_productids = clerk_productids.sort((a, b) => a - b);
106+
clerk_last_productids = clerk_last_productids.sort((a, b) => a - b);
107+
// compare
108+
if(JSON.stringify(clerk_productids) == JSON.stringify(clerk_last_productids)){
109+
// if equal - do nothing
110+
// console.log('equal: ', clerk_productids, clerk_last_productids)
111+
}else{
112+
// if not equal send cart to clerk
113+
// console.log('not equal: ', clerk_productids, clerk_last_productids)
114+
if(JSON.stringify(clerk_productids) === "[0]" ){
115+
Clerk('cart', 'set', []);
116+
}else{
117+
Clerk('cart', 'set', clerk_productids);
118+
}
119+
120+
}
121+
// save for next compare
122+
localStorage.setItem("clerk_productids", clerk_productids);
123+
}
124+
}
125+
126+
});
127+
128+
request.open('POST', '<?php echo Mage::getUrl('clerk/basket/basket'); ?>', true);
129+
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
130+
request.send(data);
131+
132+
}
133+
}, false);
134+
open.apply(this, arguments);
135+
};
136+
137+
<?php endif; ?>
138+
41139
</script>

0 commit comments

Comments
 (0)