ProTx Form - Subtracting Stock On Successful Payment

Last modified by JamesBee on Tue, September 2, 2008 07:58
Source|Old Revisions  |  Back To Group

When using Magento with stock management turned on and the ‘Decrease Stock When Order is Placed’ set to Yes, stock will be deducted when the order is created, this takes place before the user is redirected to the site which could easily lead to stock issues if the customer does not complete the payment or uses multiple attempts to order.

I have created a work-around for the Protx Form module which credits the stock back to the system before the customer is redirected and then re-deducts the stock when the customer successfully returns.

This requires adding two changes to:

/app/code/core/Mage/Protx/controllers/StandardController.php

Line 63

  1.     public function redirectAction()
  2.     {
  3.         $session = Mage::getSingleton('checkout/session');
  4.        
  5.  
  6.        
  7.         $session->setProtxStandardQuoteId($session->getQuoteId());
  8.  
  9.         $order = Mage::getModel('sales/order');
  10.         $order->loadByIncrementId($session->getLastRealOrderId());
  11.         $order->addStatusToHistory(
  12.             $order->getStatus(),
  13.             Mage::helper('protx')->__('Customer was redirected to Protx')
  14.         );
  15.         $order->save();

Change to:

  1.     public function redirectAction()
  2.     {
  3.         $session = Mage::getSingleton('checkout/session');
  4.        
  5.  
  6.        
  7.         $session->setProtxStandardQuoteId($session->getQuoteId());
  8.  
  9.         $order = Mage::getModel('sales/order');
  10.         $order->loadByIncrementId($session->getLastRealOrderId());
  11.         $order->addStatusToHistory(
  12.             $order->getStatus(),
  13.             Mage::helper('protx')->__('Customer was redirected to Protx')
  14.         );
  15.         $order->save();       
  16.         /* Put the stock back on, we don't want it taken off yet */
  17.            
  18.         if(!isset($_SESSION['updated']) || $_SESSION['updated'] != $session->getLastRealOrderId()) {
  19.             /* Put the stock back on, we don't want it taken off yet */
  20.                     $items = $order->getAllItems(); // Get all items from the order
  21.                     if ($items) {
  22.                         foreach($items as $item) {
  23.                             if ($item->getParentItem()) { // If product is a parent of a configured product (protected function getFormattedCart ())
  24.                            
  25.                                     $quantity = $item->getQtyOrdered(); // get Qty ordered
  26.                                     $product_id = $item->getProductId(); // get it's ID
  27.                                    
  28.                                     $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id); // Load the stock for this product
  29.                                     $stock->setQty($stock->getQty()+$quantity); // Set to new Qty           
  30.                                     $stock->save(); // Save
  31.                             }
  32.                             continue;                       
  33.                         }
  34.                    }
  35.             $_SESSION['updated'] = $session->getLastRealOrderId();
  36.             /* end stock addition */
  37.         }

Line 206 (With above change made)

  1.     protected function saveInvoice (Mage_Sales_Model_Order $order)
  2.     {
  3.         if ($order->canInvoice()) {
  4.             $invoice = $order->prepareInvoice();
  5.  
  6.             $invoice->register()->capture();
  7.             Mage::getModel('core/resource_transaction')
  8.                ->addObject($invoice)
  9.                ->addObject($invoice->getOrder())
  10.                ->save();

Change to

  1.     protected function saveInvoice (Mage_Sales_Model_Order $order)
  2.     {
  3.         if ($order->canInvoice()) {
  4.             $invoice = $order->prepareInvoice();
  5.  
  6.             $invoice->register()->capture();
  7.             Mage::getModel('core/resource_transaction')
  8.                ->addObject($invoice)
  9.                ->addObject($invoice->getOrder())
  10.                ->save();
  11.  
  12.  
  13.             /* order was paid, deduct stock here */
  14.            
  15.             $items = $order->getAllItems(); // Get all items from the order
  16.                 if ($items) {
  17.                     foreach($items as $item) {
  18.                         if ($item->getParentItem()) { // If product is a parent of a configured product (protected function getFormattedCart ())
  19.                        
  20.                                 $quantity = $item->getQtyOrdered(); // get Qty ordered
  21.                                 $product_id = $item->getProductId(); // get it's ID
  22.                                
  23.                                 $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id); // Load the stock for this product
  24.                                 $stock->setQty($stock->getQty()-$quantity); // Set to new Qty           
  25.                                 $stock->save(); // Save
  26.                         }
  27.                         continue;                       
  28.                     }
  29.                }
  30.                
  31.                /* end stock deduction */

This will subtract the stock only when the invoice is made after a successfull order.

I also changed the Module to add the Protx payment information to the Order Status/Comments to make it easier to keep track of the response from ProTx.

Line 137 (With above changes made)

  1.         if (!$order->getId()) {
  2.             /*
  3.             * need to have logic when there is no order with the order id from protx
  4.             */
  5.             return false;
  6.         }
  7.  
  8.         $order->addStatusToHistory(
  9.             $order->getStatus(),
  10.             Mage::helper('protx')->__('Customer successfully returned from Protx
  11.             <BR>Status:'.$this->responseArr['Status'].'
  12.             <BR>StatusDetail:'.$this->responseArr['StatusDetail'].'
  13.             <BR>VendorTxCode:'.$this->responseArr['VendorTxCode'].'
  14.             <BR>Amount:'.$this->responseArr['Amount'].'
  15.             <BR>VPSTxId:'.$this->responseArr['VPSTxId'].'
  16.             <BR>TxAuthNo:'.$this->responseArr['TxAuthNo'].'
  17.             <BR>AVSCV2:'.$this->responseArr['AVSCV2'].'
  18.             <BR>AddressResult:'.$this->responseArr['AddressResult'].'
  19.             <BR>PostCodeResult:'.$this->responseArr['PostCodeResult'].'
  20.             <BR>CV2Result:'.$this->responseArr['CV2Result'].'
  21.             <BR>GiftAid:'.$this->responseArr['GiftAid'].'
  22.             <BR>3DSecureStatus:'.$this->responseArr['3DSecureStatus'])
  23.         );

Hopefully this helps someone, I understand it is not an ideal solution to the problem but it does allow you to continue to use Magento’s stock functions without leaving yourself open to stock problems from incomplete orders.

Hopefully in the future Magento will address this and give better control over when stock is deducted in the order proccess.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs