0

I need a module that when you add product to cart check two conditions and if they are correct will change the price for the product in the basket.

I need do something like this: - when product is added to the cart - get Qty and custom_attribute of product - if qty<0 and cutom_attribute value for product is >0 change of product price in cart (the changed price should be use in transactional emails, order view, on the invoice etc.)

I try use checkout_product_add_after, but I cannot get product attribute and check condition. How to get custom product attribute in this event?

Maybe should I use another event?

  • What about the weight? Why are you trying to change it? Has it something to do with the shipping costs? And why should the qty be checked for <0 (smaller then 0)? Typo? Because this cries out for a simple cart rule to me. – Nicolas Sep 19 '15 at 7:24
  • I have 2 warehouses. First (local) warehouse - if I have product in local warehouse a Qty attribute is more than 0 (Qty>0) Second warehouse - if I haven't product in local warehouse customer can buy the same product (or another) with different price; if order is from the second warehouse the weight must change becouse I have different shipping cost (different product weight and another shipping pricing); – gregory Sep 19 '15 at 21:52
1

Try adding an observer for event event 'sales_quote_add_item':

<events>
    <sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
    </sales_quote_add_item>
</events>

In your observer :

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();

    $product = $quote_item->getProduct(); //You may need to load the product module or add the attribute to flat table
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->setWeight($newWeight);
    $quote_item->save();
}

See https://stackoverflow.com/questions/9721583/changing-the-price-in-quote-while-adding-product-to-cart-magento

  • I don't think this is going to work unless you enable Super Mode ;) : $quote_item->getProduct()->setIsSuperMode(true); – Mayers Sep 18 '15 at 21:12
  • Ok, but when I add some condition under $product = $quote_item->getProduct(); and the condition is false then a shopping cart page is crashed... – gregory Sep 19 '15 at 21:53
  • Could you add the code to your question above? – Renon Stewart Sep 20 '15 at 12:48
  • This will update the price in database right? What if i only need to change the price in the checkout process (Customer only pays the price i put in the checkout) but the price in the product page remains the same? – Gianni Di Falco Jun 17 '16 at 10:46

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.