以下是我的做法,以便在客户尝试订购超过可用库存水平时发送谷歌分析跟踪事件。
第一个副本:app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
收件人:app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
这样您就不会修改核心文件。
在app/code/local/Mage/CatalogInventory/Model/Stock/Item.php添加这个函数
public function notifyOutOfStock($productId){
$session = Mage::getSingleton('checkout/session');
//Initialise as empty array, or use existing session data
$outOfStockItems = array();
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
}
try {
$product = Mage::getModel('catalog/product')->load($productId);
$sku = $product->getSKu();
if($sku){
//Add the current sku to our out of stock items (if not already there)
if(! isset($outOfStockItems[$sku]) ) {
$outOfStockItems[$sku] = 0;
}
}
} catch (Exception $e){
//Log your error
}
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
在同一个文件中还有另一个名为 checkQuoteItemQty 的函数。
在该函数中,您需要在设置每个错误消息之后和返回语句之前使用 $this->notifyOutOfStock($this->getProductId()); 调用新函数。
所以:
public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)
{
....
if ($this->getMinSaleQty() && ($qty) < $this->getMinSaleQty()) {
$result->setHasError(true)
->setMessage(
$_helper->__('The minimum quantity allowed for purchase is %s.', $this->getMinSaleQty() * 1)
)
->setQuoteMessage($_helper->__('Some of the products cannot be ordered in requested quantity.'))
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
}
.....
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
.....
这样做是将您的产品 sku 添加到结帐会话中的数组中。
这意味着您可以在页面加载显示“库存不足”通知后立即访问模板文件中的该信息。
因此,您可以在其中一个模板文件中添加一些代码来呈现必要的 JavaScript。
我选择了 header.phtml,因为它会在每个页面上加载。 (用户可以在购物车页面以及产品查看页面中将商品数量添加到购物车)。
app/design/frontend/CUSTOMNAME/default/template/page/html/header.phtml
在代码底部的某处添加:
<!-- GA tracking for out of stock items -->
<script>
try {
<?php
$session = Mage::getSingleton('checkout/session');
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
foreach($outOfStockItems as $sku=>$value) {
if($value==0){
//Render the GA tracking code
echo "_gaq.push(['_trackEvent', 'AddToCart', 'ProductQtyNotAvailable', '".$sku."']); \r\n";
//Set it to 1 so we know not to track it again this session
$outOfStockItems[$sku] = 1;
}
}
//Update the main session
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
?>
}
catch(err) {
//console.log(err.message);
}
</script>
可以确认这很有效,并且在我看来比电子邮件或 RSS 提要更好,因为您可以将其与其他分析一起分析。