1

I am trying to understand what are the steps involved to developed extension in Magento CE to check (by sending SOAP message) for product availability on external inventory system during checkout process. Thanks ray

  • can you tell me what is mean by on external inventory system during checkout process – Amit Bera Aug 28 '14 at 6:12
5

To check an external stock system to verify that the amount is available you need to create an observer on the sales_quote_item_qty_set_after event. In your observer you can then check the remote system with the following example:

In config.xml add:

    <events>
        <sales_quote_item_qty_set_after>
            <observers>
                <my_check_qty>
                    <type>singleton</type>
                    <class>My_Module_Model_Observer</class>
                    <method>salesQuoteItemQtySetAfter</method>
                </my_check_qty>
            </observers>
        </sales_quote_item_qty_set_after>
    </events>

In your observer you can use:

/*
 * Check if products qty is available before placing order
 * Throw Exception if not and quit order
 */

public function salesQuoteItemQtySetAfter(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getEvent()->getItem();
    $qty = $quoteItem->getQty();

    $lastCheckResult = Mage::app()->loadCache('api_check_' . $quoteItem->getId() . '_' . $qty);
    if ($lastCheckResult == '') {
        $lastCheckResult = true;
        // Check remote SOAP API
        if (!$api->checkStockQty($quoteItem->getSku(), $qty)) {
            $lastCheckResult = false;
        }
    }

    // Cache API Response for 1 minute to minimize requests
    Mage::app()->saveCache($lastCheckResult, 'api_check_' . $quoteItem->getId() . '_' . $qty, array(), 60);

    // If the qty is not available then throw an exception to display an error and disable the Checkout button in the cart page.
    if (!$lastCheckResult) {
        $quoteItem->getQuote()->setHasError(true);
        $quoteItem->addErrorInfo(
            'cataloginventory',
            Mage_CatalogInventory_Helper_Data::ERROR_QTY,
            Mage::helper('cataloginventory')->__('The requested Qty is not available.')
        );
    }
}
  • Thank you for the suggestion. What about built-in observer in Magento (cataloginventory/observer) on the same event sales_quote_item_qty_set_after? Do I have to turn this off? – raybi Aug 29 '14 at 5:03
  • Hi Vladimir. Would this work also if we know the external stock qty thru CSV stock updates? Or would you use a commercial extension? Thx – snh_nl Jun 21 '16 at 18:32
  • It should be possible to write your own wrapper for the CSV stockupdates to check the contents of that file. But maybe its easier to have it stored in a database as it makes checking easier. – Vladimir Kerkhoff Jun 21 '16 at 19: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.