【问题标题】:Magento AJAX cart quantity update. No items in cartMagento AJAX 购物车数量更新。购物车中没有商品
【发布时间】:2013-12-19 15:11:24
【问题描述】:

我正在尝试使用通过 AJAX 调用访问的 PHP 脚本来更新位于标题中的 Magento“迷你购物车”。

购物车有“递增”和“递减”按钮。单击后,PHP 脚本将运行,并保存和更新购物车中特定产品的数量。

现在,问题是当我运行这个 PHP 脚本时,购物车中似乎没有商品。我在主页上和脚本运行时都查看了会话 ID,它们似乎都不同。这就是为什么我认为脚本似乎认为我的购物车中没有任何物品。

我相信我为获取会话 ID 和购物车项目而编写的代码是正确的。有谁知道我在这里可能做错了什么?

代码如下所示。我知道这可能会写得更好,但现在我只想看到一些有用的东西!。

header.phtml 中的 HTML(这是在获取篮子中所有项目的循环内 - 在这里可以正常工作):

<button id="decrement" onclick="updateCart(<?php echo $product->getId(); ?>);" class="decrement">-</button>
<input type="text" class="quantity" name="quantity" id="quantity" value="<?php echo $product->getQty(); ?>">
<button id="increment" onclick="updateCart(<?php echo $product->getId(); ?>);" class="increment">+</button>

AJAX:

function updateCart(productId, qty) {
            qty = jQuery('#quantity').val()
            jQuery.ajax({
                type: "POST",
                dataType: "HTML",
                data: { productId : productId, qty : qty },
                url: "scripts/get_cart_script.php?productId=" + productId + "&qty=" + qty,
                success: function (data) {
                    alert("Success");

                },
                error: function(data){
                    alert("Failure");
                }
            });
        }

PHP:

require_once  $_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php';

Mage::app();

// Get session
Mage::getSingleton('core/session', array('name'=>'frontend'));

// Check for a product id
if(isset($_REQUEST['productId']))
{

    // Product ID and Quantity
    $pid = $_REQUEST['productId'];
    $qnt = $_REQUEST['qty'];

    $cart = Mage::getSingleton('checkout/cart');
    $items = $cart->getItems(); 

    foreach ($items as $item) :
        if($pid == $item->getId()) :
            echo $item->getQty();
            $item->setQty($qnt); 
            $cart->save();
        endif;
    endforeach;     
};

【问题讨论】:

    标签: php jquery ajax magento


    【解决方案1】:

    您正在使用 2 个参数(即产品 ID 和数量)调用 ajax 更新函数。您在增量和减量按钮上调用了一个名为 update 的函数,只有一个名为 productid 的参数。

    你可以做一件事。更改 ajax 更新函数参数。只使用一个称为产品 ID 的参数。

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多