【问题标题】:Magento programmatically get discount giving coupon code and productsMagento 以编程方式获得折扣,提供优惠券代码和产品
【发布时间】:2013-04-03 23:03:26
【问题描述】:

我正在尝试将系统与 Magento 集成,我想要一种将优惠券代码、当前用户和购物车发送到 Magento 并检索相应折扣(如果适用)的方法,因此我不必复制所有优惠券验证背后的逻辑。

我真的很感激。

我设法做到了以下几点。

    $customerId = 1;
    $couponCode = "TESTCOUPON";
    $json = "{
                \"cart\":[{
                    \"listProduct\":[{
                        \"idReferenceProduct\":15,
                        \"quantity\":1
                    }]
                }]
            }";
    $jsonDecoded = json_decode($json);
    $products = $jsonDecoded->cart[0]->listProduct;

    // *********************************************************

    $customerObj = Mage::getModel('customer/customer')->load($customerId);
    $storeId = $customerObj->getStoreId();
    $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
    $storeObj = $quoteObj->getStore()->load($storeId);
    $quoteObj->setStore($storeObj); 

    foreach ($products as $singleProduct) {

        $productObj =  Mage::getModel('catalog/product');
        $productObj->load($singleProduct->idReferenceProduct);
        echo $productObj->getName();
        echo $productObj->getPrice();

        try{
            $quoteItem = $quoteObj->addProduct($productObj);
            $quoteItem->setPrice($productObj->getPrice());
            $quoteItem->setQty($singleProduct->quantity);
            $quoteItem->setQuote($quoteObj);                                    
            $quoteObj->addItem($quoteItem);

        } catch (exception $e) {
            echo "error creating quote item ";
        }

        $singleProduct->quantity);
    }

    try{
        $quoteObj->setCouponCode($couponCode); 
    }
    catch(exception $e){
        return "error setting coupon";
    }

    $quoteObj->collectTotals();

    var_dump($quoteObj->toArray());

还有输出:

{
["customer_id"] = > "1" 
["customer_prefix"] = > NULL
["customer_firstname"] = > "xxxxxx" 
["customer_middlename"] = > NULL
["customer_lastname"] = > "xxxxxxx" 
["customer_suffix"] = > NULL
["customer_email"] = > "xxxxxxxxxx@gmail.com" 
["customer_dob"] = > "1981-03-06 00:00:00" 
["customer_taxvat"] = > NULL
["customer_gender"] = > "1" 
["customer_group_id"] = > "1" 
["customer_tax_class_id"] = > "3" 
["store_id"] = > "1" 
["coupon_code"] = > "TESTCOUPON"
["subtotal"] = > float(872.06)
["base_subtotal"] = > float(872.06)
["subtotal_with_discount"] = > float(830.92)
["base_subtotal_with_discount"] = > float(830.92)
["grand_total"] = > float(929.64)
["base_grand_total"] = > float(929.64)
["applied_rule_ids"] = > "1" 
["virtual_items_qty"] = > int(0)
["taxes_for_items"] = > {
    [""] = > {
        [0] = > {
            ["rates"] = > {
                [0] = > {
                    ["code"] = > "IVA" 
                    ["title"] = > "IVA" 
                    ["percent"] = > float(12)
                    ["position"] = > "1" 
                    ["priority"] = > "1" 
                    ["rule_id"] = > "1"
                }
            }["percent"] = > float(12)
            ["id"] = > "IVA"
        }
    }
}["items_count"] = > int(2)
["items_qty"] = > float(2)
["trigger_recollect"] = > int(0)
["can_apply_msrp"] = > bool(false)
["totals_collected_flag"] = > bool(true)
}

优惠券折扣应该是 5% 的折扣。

由于某种原因,价格不正确。该产品价格为 516.00,输出状态的小计为 872.06。也只有一项,输出状态为 2 项。我做错了吗?

【问题讨论】:

  • 您能详细说明您的要求吗?其实我没明白你的意思。
  • 简而言之,我需要创建一个接收优惠券代码、购物车和客户并返回折扣金额的方法

标签: magento coupon


【解决方案1】:

愚蠢的我,似乎我以错误的方式将产品添加到报价单中。这就是它现在的工作方式:

        $quoteItem = Mage::getModel('sales/quote_item');
        $quoteItem->setProduct($productObj);
        $quoteItem->setPrice($productObj->getPrice());
        $quoteItem->setQty($singleProduct->quantity);
        $quoteItem->setQuote($quoteObj);                                    
        $quoteObj->addItem($quoteItem);

【讨论】:

  • 我也有这种情况,我想出了一个和你类似的实现。我的脚本还返回报价的 grand_total。然而,这种技术存在一个问题。我有仅对 1 个“每位客户使用”有效的优惠券代码。因此,当我运行我的脚本时,优惠券已用完,将不再可供特定用户使用。我的应用程序首先调用上述脚本来工作。向用户显示总计,然后调用另一个实际下订单的脚本。你有什么想法可以解决这个问题吗?
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多