1
votes

I also asked here https://magento.stackexchange.com/questions/167982/magento-1-9-x-how-to-add-an-extra-fee-to-order-while-adding-item-to-cart-progra but didn't get any answer yet so I am asking here.

I am adding item programmatically into cart (code below) and want to add an extra fee to order. I found a lot of results here also but unable to follow as I am new in magento. Can anyone please share where to create file (observer, config.xml etc with path) and what should be the name of the file? Below code is adding item to cart successfully but I am unable to add fee to complete order.

Any help will be really appreciable.

Thanks in advance. My code is:

$productId = 13114;
$price_extra = 50.00;
$product_model = Mage::getModel('catalog/product');
$code = Mage::app()->getStore()->getCode();
$_product = Mage::getModel('catalog/product')->load($productId);
$_product->setMinimalPrice($price_extra);

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$params = array(
    'product' => $productId,
    'related_product' => null,
    'qty' => 1,
    'options' => array(
        2 => array('7', '8', '9', '10', '11', '12'),
        1 => array('1', '2', '3', '4', '5', '6'),
    )
);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$storeId = $customer->getStoreId();

$cart->addProduct($_product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
2
Do you mean if product price is 100 and you want to make it 150 before adding to cart?? @PankajKlaus Mikaelson
YES! or add a fee to the order, either way.Pankaj Verma

2 Answers

0
votes

After you load your product you can set price of that product, Replace your this line

$_product->setMinimalPrice($price_extra);

with this

$_product->setPrice($_product->getPrice()+$price_extra);
0
votes

You need to set custom price using event observer

Use this event. sales_quote_add_item 1. config.xml

<frontend>
    <events>
        <sales_quote_add_item>
            <observers>
                <mymodule_observer>
                    <type>singleton</type>
                    <class>MyCompany_MyModule_Model_Observer</class>
                    <method>updateCartPrice</method>
                </mymodule_observer>
            </observers>
        </sales_quote_add_item>
    </events>
</frontend>

  1. Observer.php

class MyCompany_Mymodule_Model_Observer {

public function updateCartPrice(Varien_Event_Observer $observer) {
    $event = $observer->getEvent();
    $quoteItem = $event->getQuoteItem();
    $product = $item->getProduct();

    $extraPrice = 100;
    $customPrice = $product->getFinalPrice() + $extraPrice;
    $quoteItem->setOriginalCustomPrice($customPrice);
    $quoteItem->setCustomPrice($customPrice);
    $quoteItem->save();
}

}

Try this. It will work for you.