2

I'm developing some functionality using events, observers to determine if certain conditions for an order are met. If not, the user is redirected back to the cart screen with a message.

The conditions are that the first order must be at least 500 and the user must be in a certain customer group. This works fine. Now when i click the order button, stock gets decreased but no order is made. I'm trying to find a way to have this stock decrease not happen.

I'm using an observer on the following event:

"sales_model_service_quote_submit_before"

The observer sits in a custom module. I was trying to find an event "before" stock decrease but i couldn't find it. I'm happy if you provide me with the right event, also with a solution to this stock decrease. I'll show you some of my code:

part of config.xml

<frontend>
    <events>
        <sales_model_service_quote_submit_before>
            <observers>
                <company_firstorder>
                    <type>singleton</type>
                    <class>COMPANY_FirstOrder_Model_Observer</class>
                    <method>firstOrder</method>
                </company_firstorder>
            </observers>
        </sales_model_service_quote_submit_before>
    </events>
</frontend>

Part of my observer.php

        if ($isActive){
        if ($customer->isLoggedIn() && $customerGroupId === $currentCustomerGroupId){
            if ($ordersCount < 1){
                if ($orderGrandTotal < $minimumOrderAmount){

                    // Here, something should happen that fixes my stock decrease problem
                    Mage::getSingleton('checkout/session')->addNotice('Custom error notice - To Do: Add field in backend for this notice');
                    Mage::app()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'))->sendResponse();
                    exit;
                }
            }
        }
    }

I have been stuck on this issue the whole day so any help is greatly appreciated :)

P.s. There is a post about programatically increasing the stock but this is not a good solution to my problem. I checked loads of stack posts before writing this one.

EDIT Due to complexity i used a different approach. I'm performing these checks now on the checkout page. This will hide/show my checkout button and display a message. This alternative works for me. Case closed

1

I would not wait with this check until the customer has gone through the whole checkout. When is the earliest point where you have all necessary data available? I'd say after the first checkout step (login or register) or right before if the customer is already logged in. So listen to the appropriate event of the one page checkout controller.

controller_action_predispatch_checkout_onepage_index should work for both cases. And since this is a normal non-ajax request you can redirect to the cart as before.

  • I use the onestepcheckout so there's no process. I want this to run before my customers hits onestepcheckout. The predispatch functionality is a little unknown to me, i should dive into it. I will try your event tomorow when i'm back at work and let you know, thanks – FROSIT Jul 21 '15 at 21:40
  • Implemented it! – FROSIT Jul 31 '15 at 14:03
0

if you want to check inventory and not allow to place order then sales_order_place_before event will be usefull, this will listen at check out page

function checkInventory(Varien_Event_Observer $observer) {

    if (!$helper->isEnabled())
        return;
    $requestedArea = mage::app()->getRequest()->getModuleName();

    if ($requestedArea == 'checkout')
    {
        $order = $observer->getEvent()->getOrder();
        $orderItems = array();
        foreach ($order->getQuote()->getAllItems() as $key => $item)
        {
            $orderItems[$key]['qty'] = $item->getProduct()->getStockItem()->getQty();
            $orderItems[$key]['sku'] = $item->getProduct()->getSku();
            $orderItems[$key]['quatityOrdered'] = $item->getQty();
        }


    }
}
  • If the conditions are met, so my code runs. The inventory should not be changed at all. I have tried this event, thanks you for helping. But the inventory still gets decreased due to quote. So i'm looking for a way to have my customer be redirected back to cart without having an order or change in stock. My code does this except for the stock part... – FROSIT Jul 6 '15 at 13:44

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.