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?

2
  • 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
    Commented Sep 19, 2015 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
    Commented Sep 19, 2015 at 21:52

2 Answers 2

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

4
  • I don't think this is going to work unless you enable Super Mode ;) : $quote_item->getProduct()->setIsSuperMode(true); Commented Sep 18, 2015 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
    Commented Sep 19, 2015 at 21:53
  • Could you add the code to your question above? Commented Sep 20, 2015 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? Commented Jun 17, 2016 at 10:46
0

I have the same need, that is to say change price as customer is adding the product to his cart. As I'm not accustom to things like $variable->getId() ... I use common PDO queries. I use this event with an observer : checkout_cart_add_product_complete I manage to get the cart, change the price, BUT it is impossible to update table sales_flat_quote_item ! I use this PDO query :

$connexion->exec("update sales_flat_quote_item set price='".$prixHT."', base_price=".$prixHT.", row_total=".$prixHT.", base_row_total=".$prixHT.", tax_amount=".$prixHT*0.2.", base_tax_amount=".$prixHT*0.2.", original_custom_price='".$prixTTC."', price_incl_tax='".$prixTTC."' base_price_incl_tax=".$prixTTC.", row_total_incl_tax='".$prixTTC."', base_row_total_incl_tax='".$prixTTC."' where item_id='".$ligne['item_id']."'");

NO CHANGE into sales_flat_quote_item . If I write the query directly into phpmyadmin, it works ! Why doesn't it work through the observer ? thank you for comments :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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