php - 马根托 |我如何在自定义模块的购物车中添加产品?

标签 php rest magento

我目前正在开发 Magento 1.9 上的管理模块 Cart

我坚持要将产品添加到我的购物车(我尝试了很多不同的东西),这就是我请求您帮助的原因。

我的模块扩展了 magento 的其余 API,并且我已经管理了对我的购物车(产品数量)的更新,但现在我希望通过 POST 方法添加新产品。 当然,我以客户身份登录。我为这个角色定义了创建权限。 (我可以毫无问题地进行更新)

这是我的代码:

protected function _create(array $data){

    $store = $this->_getStore();
    Mage::app()->setCurrentStore($store->getId());

    $cart = Mage::getModel('checkout/cart');
    $cart->init();

    $productCollection = Mage::getModel('catalog/product')->load(4);


    // Add product to cart
    $cart->addProduct($productCollection,
        array(
            'product_id' => $productCollection->getId(),
            'qty' => '1'
        )
    );
    // Save cart
    $cart->save();

}

在这个简单的示例中,我尝试在数量 1 中添加产品 ID 4。 我的问题是我在日志中没有错误,一切似乎都过去了。但是当我拿到我的购物车时,没有要添加的产品...

作为返回,我有一个代码 200 OK

你有什么建议可以帮助我吗?

非常感谢您的帮助

问候

最佳答案

在遍历整个互联网后,我终于找到了解决方案;)

事实上,当你想在结账前到达购物车时,magento 使用“报价”的定义......对于 Magento 的初学者来说不容易理解...... 因此,为了方便那些像我一样遇到麻烦的人进行研究,这是我在购物车中添加新产品的代码(结帐前):

//$data['entity_id'] = The id of the product you want to add to the cart
//$data['qty'] = The quantity you want to specify

protected function _create(array $data)
{
    $store = $this->_getStore();
    Mage::app()->setCurrentStore($store->getId());

    // Get Customer's ID
    $customerID = $this->getApiUser()->getUserId();

    // Load quote by Customer
    $quote = Mage::getModel('sales/quote')
             ->loadByCustomer($customerID);

    $product = Mage::getModel('catalog/product')
                         // set the current store ID
                         ->setStoreId(Mage::app()->getStore()->getId())
                         // load the product object
                         ->load($data['entity_id']);

    // Add Product to Quote
    $quote->addProduct($product,$data['qty']);

    try {
        // Calculate the new Cart total and Save Quote
        $quote->collectTotals()->save();
    } catch (Mage_Core_Exception $e) {
        $this->_error($e->getMessage(),Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
    }

}

希望对大家有帮助

问候

嘉年华

关于php - 马根托 |我如何在自定义模块的购物车中添加产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25446804/

相关文章:

php - SQL Select where IN (array) 仅当找到数组中的第一项时才返回

php - Nginx 上的 Codeigniter 为表单生成 https 操作,但不是 Apache

java - 使用 REST 发送文件?

Magento:从产品集合中获取所有产品而忽略设置的限制?

php - 有没有可以处理 Magento 代码库的 PHP IDE?

php - 使用 PHP 查找谁在托管网站

当请求时,PHP 将 mysql 查询作为变量传递给第二个 php 脚本

php - 如何在http get请求中发送数组

java - 使用 jersey 和 spring 在 REST 中服务的空指针

Magento,可配置产品还是简单产品?什么对 SEO 最好?