1

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);
}
  • you should use getSingleton to initialize the cart. And the quote and the cart should be referenced... – Fabian Blechschmidt Aug 27 '13 at 16:41
  • Instead of getModel? I'll try that. Referenced how? – aynber Aug 27 '13 at 17:31
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.