我最终得到了以下代码,这不是最有效的,但是这个移动应用解决方案列表中的产品永远不会超过 5 个......
将代码添加到:
$array = Mage::helper('paypal_catalog')->getProducts($category->getId());
$this->setCurrenCategoryKey($category->getId());
并做到了:
$array = Mage::helper('paypal_catalog')->getProducts($category->getId());
foreach ($array['items'] as &$productitem) {
$productId = $productitem['id'];
$product = Mage::getModel('catalog/product')->load($productId);
if(!is_null($product)){
$productitem['type'] = $product->getTypeId();
//Get all options for product
$options = array();
$options = $product->getOptions();
if (count($options) > 0){
$productitem['hasoptions'] = '1';
}
else
{
$productitem['hasoptions'] = '0';
}
$addTocartUrl = $this->helper('checkout/cart')->getAddUrl($product);
$productitem['addTocartUrl'] = $addTocartUrl;
}
else{
$productitem['type'] = 'simple';
$productitem['hasoptions'] = '0';
}
}
$this->setCurrenCategoryKey($category->getId());
然后我可以使用以下内容更改我的footer.phtml:
<!-- Product List Template -->
<div id="productListTemplate">
<li onclick="checkQuickAdd(jQuery(this),'%url%');">
<div class="product-image">%image%</div>
<div class="product-content">
<div class="product-title">%title%</div>
<div class="product-description"><span class="counter" style="font-weight:bold;font-size:large;"></span> %description%</div>
<div class="product-price">$%price%</div>
<div class="quickadd"><a href="#" class="quickaddlink">+</a></div>
</div>
<div class="product-id" style="display:none;">%id%</div>
<div class="product-hasoptions" style="display:none">%hasoptions%</div>
</li>
</div>
这允许稍后在 js 函数中查询值:
function checkQuickAdd(listitem,url)
{
var hasOptions = jQuery(listitem).find(".product-hasoptions").text();
var productId = jQuery(listitem).find(".product-id").text();
var productTitle = jQuery(listitem).find(".product-title").text();
if (hasOptions == '0'){
try {
jQuery(".ui-loader").css("display","block");
var cartUrl = "/index.php/test/jsoncheckout/cart/add/product/" + productId;
var cartno = jQuery(jQuery('.ui-page-active .menu-bar-item.itemright>div')[0]).attr('id').replace("cartCount","");
var cartCount = jQuery(jQuery('.ui-page-active .menu-bar-item.itemright>div')[0]).text();
jQuery.ajax( {
url : cartUrl,
type: "POST",
dataType : 'json',
contentType: "application/json; charset=utf-8",
async: true,
cache: false,
success : function(data) {
//alert("added");
var counter = 1;
var strCounter = jQuery(listitem).find(".counter").text();
if (strCounter != "") { counter = parseInt(strCounter) + 1; }
if (cartCount != "") {
cartCount = parseInt(cartCount) + 1;
}
else {
cartCount = 1;
}
jQuery(listitem).find(".counter").text(counter);
// update cart number
updateCartCount(cartCount, cartno);
jQuery(".ui-loader").css("display","none");
alert("Added " + productTitle);
},
fail: function (msg) {
jQuery(listitem).find(".product-description").text(msg.d);
jQuery(".ui-loader").css("display","none");
}
});
jQuery(".ui-loader").css("display","none");
}
catch (e) {
alert(e);
jQuery(".ui-loader").css("display","none");
}
}
else{
forwardURL(url);
}
}
这一定是我很久以来写的效率最低的代码了,但是完成这个的时间非常短。
我很想以更有效的方式做到这一点。
任何建议,将不胜感激!