Quote Originally Posted by ttfan View Post
Here is the code you requested:
Code:
function updateNotifyOrderProcessingStockDecrementBegin(&$callingClass, $notifier, $paramsArray, &$stock_values, &$attribute_stock_left = 0){
    global $db; //, $pwas_class;

    $this->_stock_values = $stock_values;

    if ($this->_orderIsSBA && $stock_values->RecordCount() > 0) {
      // kuroi: Begin Stock by Attributes additions
      // added to update quantities of products with attributes
      // $stock_attributes_search = array();
      $attribute_stock_left = STOCK_REORDER_LEVEL + 1;  // kuroi: prevent false low stock triggers
      $this->_attribute_stock_left = $attribute_stock_left;

      // mc12345678 If the has attibutes then perform the following work.
      if(isset($this->_productI['attributes']) and sizeof($this->_productI['attributes']) > 0){
        // Need to identify which records in the PWAS table need to be updated to remove stock from
          // them.  Ie. provide a list of attributes and get a list of stock_ids from pwas.
          // Then process that list of stock_ids to decrement based on their impact on stock.  This
          // all should be a consistent application.
        // mc12345678 Identify a list of attributes associated with the product
        $stock_attributes_search = $_SESSION['pwas_class2']->zen_get_sba_stock_attribute(zen_get_prid($this->_productI['id']), $this->_productI['attributes'], 'order');
        $stock_attributes_search_new = $_SESSION['pwas_class2']->zen_get_sba_attribute_info($this->_productI['id'], $this->_productI['attributes'], 'order', 'ids');
          if (isset($stock_attributes_search_new) && $stock_attributes_search_new === false) {
              
          } elseif (isset($stock_attributes_search_new) && is_array($stock_attributes_search_new) && count($stock_attributes_search_new) == 0) {
              
          } elseif (isset($stock_attributes_search_new) && $stock_attributes_search_new && count($stock_attributes_search_new) > 0) {
              foreach ($stock_attributes_search_new as $stock_id) {
                  // @todo: address in PWAS table whether particular variant should be altered with stock quantities.
                  $get_quantity_query = 'SELECT quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id=' . zen_get_prid($this->_productI['id']) . ' and stock_id=' . (int)$stock_id;
                  $attribute_stock_available = $db->Execute($get_quantity_query, false, false, 0, true);
                  if (true) { // Goal here is to identify if the particular attribute/stock item should be affected by a stock change.  If it is not, then this should be false or not performed.
                      $attribute_stock_left_test = $attribute_stock_available->fields['quantity'] - $this->_productI['qty'];
                      $attribute_update_query = 'UPDATE ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' SET quantity="'.$attribute_stock_left_test.'" where products_id=' . zen_get_prid($this->_productI['id']) . ' and stock_id=' . (int)$stock_id;
                      $db->Execute($attribute_update_query, false, false, 0, true);
                      if ($attribute_stock_left_test < $attribute_stock_left) {
                          $this->_attribute_stock_left = min($attribute_stock_left_test, $this->_attribute_stock_left);
                          $attribute_stock_left = $this->_attribute_stock_left;
                      }
                  }
              }
          }
          
/*        $get_quantity_query = 'select quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id="' . zen_get_prid($this->_productI['id']) . '" and stock_attributes="' . $stock_attributes_search . '"';
        $get_quantity = $_SESSION['pwas_class2']->zen_get_sba_attribute_info($this->_productI['id'], $this->_productI['attributes'], 'products', 'stock');
  
        // mc12345678 Identify the stock available from SBA.
        $attribute_stock_available = $db->Execute($get_quantity_query, false, false, 0, true);  
        // mc12345678 Identify the stock remaining for the overall stock by removing the number of the current product from the number available for the attributes_id. 
        $attribute_stock_left = *//*$attribute_stock_available->fields['quantity']*//* $get_quantity - $this->_productI['qty'];
  
        // mc12345678 Update the SBA table to reflect the stock remaining based on the above.
        $attribute_update_query = 'update ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' set quantity='.$attribute_stock_left.' where products_id="' . zen_get_prid($this->_productI['id']) . '" and stock_attributes="' . $stock_attributes_search . '"';
        $db->Execute($attribute_update_query, false, false, 0, true);  
        //$this->_attribute_stock_left = $attribute_stock_left;*/
      }
      $attribute_stock_left = $this->_attribute_stock_left;
    }
  }

  /*
   * Function that is activated when NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END is encountered as a notifier.
   */
  // Line 776
    /**
     * @param $callingClass
     * @param $notifier
     * @param $paramsArray
     */
Very few plugins are installed at this stage, as we're still trying to get things working. The plugins are: CK editor & image handler, as well as stock by attributes of course.

I can see no commonalities between orders that pass/fail, except that the ones fails always have the atrributes discussed above. Items with no attributes work properly.
That appears to be the latest code which supports the rewritten stock decrement functionality for all three variant styles of:
1) One option name for a product with one option value per variant entry.
2) Multiple option names, each option name's value listed one time such that no two option values are combined in one entry.
3) Multiple option names with combinations of option values from different option names all entered in a single variant.

So, that said, I tried to look through the code to find something that might cause a spurious result and found two queries "buried" in the process that do not clear the query cache. Is that the cause? I don't know because haven't been able to repeat the issue and no further information has yet been provided/analyzed to be able to verify that the problem has been resolved. In fact, I'm not even sure that by making the changes (or not) if there is a way to validate that the problem has been solved, but anyways...

The suggested fix is available from: https://github.com/mc12345678/Stock_...3ab46ef5a4aa0e
and affects only one file in two places. The file is: includes/classes/class.products_with_attributes_class_stock.php

The lines affected are 416 and 1506 with the same fix applied to both of effectively adding to the query:
Code:
, false, false, 0, true
as can be seen at the above link.

Feedback would be appreciated.