Leonardo Lanchas
Leonardo Lanchas

Reputation: 1668

How prevent PrestaShop from updating product quantities after order validation

I am new to prestashop and I am trying to make a payment module where I need to duplicate an order for statistical issues. My problem is that the duplicate order also substracts from product stock and I need to know where, after an order validation, does prestashop update stock to avoid calling the corresponding function. In few words, I call validateOrder() twice but I need that StockAvailable be updated once.

By the way, I examined the whole validateOrder() function searching for the update section / function but I haven't been able to find it.

The only related code that I have been able to find was this:

// updates stock in shops
    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
    {
        $product_list = $order->getProducts();
        foreach ($product_list as $product)
        {
            // if the available quantities depends on the physical stock
            if (StockAvailable::dependsOnStock($product['product_id']))
            {
                // synchronizes
                StockAvailable::synchronize($product['product_id'], $order->id_shop);
            }
        }
    }

but this is only works when advanced stock management is enabled.

Thanks.

Upvotes: 4

Views: 2591

Answers (1)

unloco
unloco

Reputation: 7330

The code you're looking for is located in the OrderDetail::create method which uses OrderDetail::checkProductStock which in turn calls StockAvailable::updateQuantity

$update_quantity = StockAvailable::updateQuantity(...

What's interesting is that before updating the quantity there's this condition

if (!StockAvailable::dependsOnStock($product['id_product']))

I suggest that you override this method and return true when you need to.

You might set a global flag before duplicating the order and then check that flag and if it's true return true to prevent updating the stock.

the override code in override/classes/stock/StockAvailable.php

class StockAvailable extends StockAvailableCore 
{

  public static function dependsOnStock($id_product, $id_shop = null)
  {
    $no_quantity_update = isset($GLOBALS['no_quantity_update']) && $GLOBALS['no_quantity_update'];
    if ($no_quantity_update) 
      return true;
    else return parent::dependsOnStock($id_product, $id_shop = null);
  }

}

To make this override effective, delete the file cache/class_index.php to refresh the overrides list

Your module code:

//set to false just to be sure
$GLOBALS['no_quantity_update'] = false;
$this->module->validateOrder($cart->id, Configuration...
//set to true to prevent quantity update
$GLOBALS['no_quantity_update'] = true;
$this->module->validateOrder($cart->id, Configuration...

You can modify directly on the core code but that's not recommended

Upvotes: 2

Related Questions