0

I want to add a product to a cart and change its price to 0.

Currently I have :

$request = new Varien_Object([
    'qty' => 1,
]);
$cart->getQuote()
     ->addProductAdvanced(Mage::getModel('catalog/product')
     ->load($productId), $request)
     ->setCustomPrice(0)
     ->setOriginalCustomPrice(0);

That work, but in the DB, I have only the product, not the linked configurable (and I need it). So I try the same thing with the configurable :

$request = new Varien_Object([
    'qty' => 1,
    'super_attribute' => array(
        $sizeAttributeId => $sizeValue, 
        $priceAttributeId => 0,
    )
]);
$cart->getQuote()
     ->addProductAdvanced(Mage::getModel('catalog/product')
     ->load($configurableProductId), $request)
     ->setCustomPrice(0)
     ->setOriginalCustomPrice(0);

Now, that work too : when I go on the cart, I have my product with 0 in price (and if I valid the order, I have my configurable in DB). But then, if a go in the checkout process, my price is reset to the original price... Someone now why ? May be I did something bad ?

1

Finally I find a way (if womeone is interested) :

$request = new Varien_Object([
    'qty' => 1,
    'super_attribute' => array(
        $sizeAttributeId => $sizeValue, 
        $priceAttributeId => 0,
    )
]);
$newItem = $cart
    ->getQuote()
    ->addProductAdvanced(Mage::getModel('catalog/product')
    ->load($configurableProductId), $request)
    ->getParentItem();
$newItem->setCustomPrice(0)->setOriginalCustomPrice(0);

That works for me and no side effect for the moment :)

| improve this answer | |

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.