首页 文章

Magento以编程方式获得折扣优惠券代码和产品

提问于
浏览
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项 . 难道我做错了什么?

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);
    

相关问题