5

I have a problem with my observer. I want to apply a discount on the before product add to cart event, but I am getting all price set as null. My observer code is:

public function applyCartDiscount(Varien_Event_Observer $observer)
{

    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // Discounted 25% off
    $percentDiscount = 0.25;

    // This makes sure the discount isn't applied over and over when refreshing
    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

    // Make sure we don't have a negative
    $item->setCustomPrice($specialPrice);
    $item->setOriginalCustomPrice($specialPrice);
    $item->getProduct()->setIsSuperMode(true);

}

When I print array using

print_r($item->toArray());

It gives array something like

Array
(
    [item_id] => 21
    [quote_id] => 4
    [created_at] => 2013-11-28 05:46:02
    [updated_at] => 2013-11-28 05:46:02
    [product_id] => 51
    [store_id] => 1
    [parent_item_id] => 
    [is_virtual] => 0
    [sku] => 1111
    [name] => Ottoman
    [description] => 
    [applied_rule_ids] => 
    [additional_data] => 
    [free_shipping] => 0
    [is_qty_decimal] => 0
    [no_discount] => 0
    [weight] => 20.0000
    [qty] => 2
    [price] => 0.0000
    [base_price] => 0.0000
    [custom_price] => 0.0000
    [discount_percent] => 0.0000
    [discount_amount] => 0.0000
    [base_discount_amount] => 0.0000
    [tax_percent] => 0.0000
    [tax_amount] => 0.0000
    [base_tax_amount] => 0.0000
    [row_total] => 0.0000
    [base_row_total] => 0.0000
    [row_total_with_discount] => 0.0000
    [row_weight] => 20.0000
    [product_type] => simple
    [base_tax_before_discount] => 
    [tax_before_discount] => 
    [original_custom_price] => 0.0000
    [gift_message_id] => 
    [weee_tax_applied] => a:0:{}
    [weee_tax_applied_amount] => 0.0000
    [weee_tax_applied_row_amount] => 0.0000
    [base_weee_tax_applied_amount] => 0.0000
    [base_weee_tax_applied_row_amnt] => 
    [weee_tax_disposition] => 0.0000
    [weee_tax_row_disposition] => 0.0000
    [base_weee_tax_disposition] => 0.0000
    [base_weee_tax_row_disposition] => 0.0000
    [redirect_url] => 
    [base_cost] => 50.0000
    [price_incl_tax] => 0.0000
    [base_price_incl_tax] => 0.0000
    [row_total_incl_tax] => 0.0000
    [base_row_total_incl_tax] => 0.0000
    [hidden_tax_amount] => 
    [base_hidden_tax_amount] => 
    [qty_options] => Array
        (
        )
[product] => Array
    (
        [store_id] => 1
        [entity_id] => 51
        [entity_type_id] => 10
        [attribute_set_id] => 42
        [type_id] => simple
        [sku] => 1111
        [created_at] => 2007-08-28 16:25:46
        [updated_at] => 2008-08-08 14:59:04
        [has_options] => 0
        [required_options] => 0
        [name] => Ottoman
        [meta_title] => Ottoman
        [meta_description] => Ottoman
        [image] => /o/t/ottoman.jpg
        [small_image] => /o/t/ottoman.jpg
        [url_key] => ottoman
        [thumbnail] => /o/t/ottoman.jpg
        [gift_message_available] => 
        [url_path] => ottoman.html
        [custom_design] => 
        [options_container] => container2
        [color] => 26
        [status] => 1
        [tax_class_id] => 2
        [room] => 72
        [visibility] => 4
        [description] => The Magento ottoman will impress with its style while it delivers on quality. This piece of living room furniture is built to last with durable solid wood framing, generous padding and plush stain-resistant microfiber upholstery.
        [meta_keyword] => Ottoman
        [dimension] => 
        [model] => magotto
        [short_description] => With durable solid wood framing, generous padding and plush stain-resistant microfiber upholstery.
        [country_orgin] => Italy
        [finish] => Microfiber
        [custom_layout_update] => 
        [price] => 299.9900
        [cost] => 50.0000
        [weight] => 20.0000
        [minimal_price] => 299.9900
        [group_price] => Array
            (
            )

        [group_price_changed] => 0
        [tier_price] => Array
            (
            )

        [tier_price_changed] => 0
        [media_gallery] => Array
            (
                [images] => Array
                    (
                        [0] => Array
                            (
                                [value_id] => 201
                                [file] => /o/t/ottoman.jpg
                                [label] => 
                                [position] => 0
                                [disabled] => 1
                                [label_default] => 
                                [position_default] => 0
                                [disabled_default] => 1
                            )

                    )

                [values] => Array
                    (
                    )

            )

        [stock_item] => Array
            (
                [item_id] => 29
                [product_id] => 51
                [stock_id] => 1
                [qty] => 706.0000
                [min_qty] => 0.0000
                [use_config_min_qty] => 1
                [is_qty_decimal] => 0
                [backorders] => 0
                [use_config_backorders] => 1
                [min_sale_qty] => 1.0000
                [use_config_min_sale_qty] => 1
                [max_sale_qty] => 100.0000
                [use_config_max_sale_qty] => 1
                [is_in_stock] => 1
                [low_stock_date] => 0000-00-00 00:00:00
                [notify_stock_qty] => 
                [use_config_notify_stock_qty] => 1
                [manage_stock] => 0
                [use_config_manage_stock] => 1
                [stock_status_changed_auto] => 0
                [use_config_qty_increments] => 1
                [qty_increments] => 0.0000
                [use_config_enable_qty_inc] => 1
                [enable_qty_increments] => 0
                [is_decimal_divided] => 0
                [type_id] => simple
                [stock_status_changed_automatically] => 0
                [use_config_enable_qty_increments] => 1
                [product_name] => Ottoman
                [store_id] => 1
                [product_type_id] => simple
                [product_status_changed] => 1
                [product_changed_websites] => 
                [ordered_items] => 2
            )

        [is_in_stock] => 1
        [is_salable] => 1
        [website_ids] => Array
            (
                [0] => 1
            )

        [cart_qty] => 1
        [qty] => 1
        [stick_within_parent] => 
        [customer_group_id] => 0
        [final_price] => 
    )

[tax_class_id] => 2
[is_recurring] => 
[has_error] => 
[qty_to_add] => 1

Please help where am I going wrong..

  • Is there any reason to change code to add discount? You can do this using Catalog Price Rule also. For more detail please review magentocommerce.com/knowledge-base/entry/… – Bijal Bhavsar Nov 28 '13 at 6:42
  • yes discount is dynamic come from one of my api for perticuler customer group – Zaheerabbas Nov 28 '13 at 6:48
  • @what event are you observing? – Marius Nov 28 '13 at 6:57
  • checkout_cart_product_add_after – Zaheerabbas Nov 28 '13 at 6:58
  • you can get added product price by using $product = $observer->getProduct(); $price = $product->getPrice(); – Bijal Bhavsar Nov 28 '13 at 7:22
3

The problem happens because the attribute original_price is only set after the totals have been collected. It is set in Mage_Tax_Model_Sales_Total_Quote_Subtotal as part of the function _recollectItem.

The reason that you get 0 for price is because the first time you add to the cart you are resetting the prices to 0.

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.