Magento – Add coupon to order programmatically with modified coupon code

couponmagento-1.7orders

I've modified the coupon validation function to check for grouped SKUs as well as the simple SKU. It works great on the Magento front end, but I have a process that creates an order programmatically, and I'm not able to get the coupon to work there. I've traced it down to the fact that since the programmatic order doesn't use the checkout/cart, it can't get the parent item.

Here's the validate function inside of local/Mage/Rule/Model/Condition/Abstract.php:

public function validate(Varien_Object $object)
{
    $parentCode = '';
    if($this->getAttribute() == 'sku'){
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        if($qItem = $quote->getItemByProduct($object)){
            if($parent = $qItem->getParentItem()){
                $parentCode = $parent->getSku();
            }
            else if($buyRequest = $qItem->getOptionByCode('info_buyRequest'))
            {
                $options = unserialize($buyRequest->getValue());
                if(!empty($options['super_product_config']))
                {
                    $prodID = $options['super_product_config']['product_id'];
                    $parentCode = Mage::getModel('catalog/product')->load($prodID)->getSku();
                }
            }
        }
    }
    return ($this->validateAttribute($object->getData($this->getAttribute())) || $this->validateAttribute($parentCode)) ? true : false;
}

And the relevant parts of my order creation:

function createQuote(){
    $quote = Mage::getModel('sales/quote');
    if (!$quote) {
        Mage::log("Error in quote! ".$parentId);
        return;
    }
    $this->quote = $quote;
}

function setCoupon($coupon){
    if(!empty($coupon))
        $this->_coupon = $coupon;
}

function setQuoteAddress(){
    $quote = $this->quote;
    $shippingAddress = new Mage_Sales_Model_Quote_Address;
    $billingAddress = new Mage_Sales_Model_Quote_Address;
    $billingAddress->setData($this->_billingAddr);
    $shippingAddress->setData($this->_shippingAddr);
    $quote->setBillingAddress($billingAddress);
    $quote->setShippingAddress($shippingAddress);
    $quote->getShippingAddress()->setSameAsBilling($this->_sameAsBilling);
    $quote->getBillingAddress()->setEmail($this->_billingAddr['email']);

    if(!empty($this->_coupon)){
        $quote->setCouponCode($this->_coupon);
    }
}

function setAddress($request){

    // Address validation here
    $this->setQuoteAddress();

    return false;   
}

Edit It could possibly be because I wasn't adding the items to the cart, like I had thought, but when I try, the total is 0:

// Total is correct, cannot pull the ItemByProduct/getParentItem
$cart = Mage::getModel('checkout/cart');
if(!$quote->hasProductId($parentId)){
    $quote->addProduct($product, new Varien_Object($params));
//  $quote->collectTotals()->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}

// Amount is 0
$cart = Mage::getModel('checkout/cart');
$cart->init();
//Add product to cart with specified parameters and save the cart object
if(!$quote->hasProductId($parentId)){
    $cart->addProduct($product, new Varien_Object($params));
    $cart->save();
    //  $quote->collectTotals()->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}

Best Answer

Oddly, I fixed it by adding it to both the cart and the quote:

$cart = Mage::getModel('checkout/cart');
//Add product to cart with specified parameters and save the cart object
if(!$quote->hasProductId($parentId)){
    $cart->addProduct($product, new Varien_Object($params))->save();
    $quote->addProduct($product, new Varien_Object($params))->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}

I'm not sure if this will cause issues, but I'm going to do a bit more testing.

Related Topic