0

我正在尝试将系统与 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 项。难道我做错了什么?

4

1 回答 1

0

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

        $quoteItem = Mage::getModel('sales/quote_item');
        $quoteItem->setProduct($productObj);
        $quoteItem->setPrice($productObj->getPrice());
        $quoteItem->setQty($singleProduct->quantity);
        $quoteItem->setQuote($quoteObj);                                    
        $quoteObj->addItem($quoteItem);
于 2013-04-12T16:38:56.907 回答