Question

I'm trying to integrate a system with Magento, I would like a way to send the coupon code, current user and cart to Magento and retrieve the according discount (if applies), so I don't have to replicate all the logic behind coupon validation.

I would really appreciate it.

I managed to do the following.

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

And the output:

{
["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)
}

The coupon discount is supposed to be a 5% discount.

For some reason the prices are not correct. That product price is 516.00 and the subtotal in the output states 872.06. Also there's only one item and the output states 2 items. Am I doing something wrong?

Was it helpful?

Solution

Silly me, it seems I was adding the products to the quote the wrong way. This is the way it works now:

        $quoteItem = Mage::getModel('sales/quote_item');
        $quoteItem->setProduct($productObj);
        $quoteItem->setPrice($productObj->getPrice());
        $quoteItem->setQty($singleProduct->quantity);
        $quoteItem->setQuote($quoteObj);                                    
        $quoteObj->addItem($quoteItem);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top