If this is your first visit, check our documentation. To post, register or login.
Forums / Zen Cart Code Suggestions / order notifiers

order notifiers

Started by mc12345678 on 24 Nov 2014, 2:36 AM · Views: 103

Results 1 to 5 of 5
24 Nov 2014, 2:36 AM
#1
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

order notifiers

in includes/classes/order.php there are a few modifications to suggest moving forwards:
the notifier NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM or beyond does not include the updated value from the attributes sql that was performed just before it from which an orders_products_attributes_id could be generated similar to NOTIFY_ORDER_DURING_CREATE_ADDED_ORDER_HEADER at line 667 and NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM at line 794
Each of the above two examples have a value that is assigned by the completion of the sql action before the notifier (ie., order_id)

Ultimately, this is being considered for a version of Stock By Attributes that would include a table to track the orders completed which is generated similar to the existing Orders tables.

An additional aspect/question, there are several notifier variables that were added in ZC 1.5.3 at several of the notify locations (ie. $i, $products[$i], etc...). Some of these make sense at the location they are included; however, the majority of the variables are added as being able to be modified by reference. As a result, for example $i passed at the beginning of the run could be a different value immediately after the call and essentially at each such call. I am surprised to see that they are not added to an array that is immutable (first of 9 variables accepted when using the observer class). Because they are passed at the point(s) they are, almost would suggest that such variables be passed at the last moment they may be needed for attributes such as beginning order attribute processing at: NOTIFY_ORDER_PROCESSING_ATTRIBUTES_BEGIN.

In absence of such, guidelines suggested would be to use the existing notifier variables as best they can be without modification of where the variable(s) are used. Currently have been able to incorporate Stock by Attributes into the includes/classes/order.php file without modifying the file except to incorporate the above additional notify variable as the variable comes from a recent database transaction which is suggested not to try to identify across a notify/observer action.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

24 Nov 2014, 2:50 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: order notifiers

mc12345678:

Currently have been able to incorporate Stock by Attributes into the includes/classes/order.php file without modifying the file except to incorporate the above additional notify variable as the variable comes from a recent database transaction which is suggested not to try to identify across a notify/observer action.
Can you post the change you made for this?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

24 Nov 2014, 3:01 AM
#3
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: order notifiers

Umm.. To be clear, not everything associated with SBA is merely in the includes/classes/order.php file, just the interface of SBA to the order.php file. Meaning, there is no longer SBA code inside the includes/classes/order.php file. The below SBA code does modify the order stored model # though using the newly created customid field if it is populated. It does not change the default stored model number, just the model number in the orders table for example. Also have been trying to use handler functions in the event that the background data structure changes (which is anticipated to some extent based on the desired usage for SBA and an ultimate goal of normalizing the table structure).

Below are the two files:

modification made to order.php without changing other notifiers.

From:

          zen_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);

          $this->notify('NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM', $sql_data_array);

To:

          zen_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);

          $order_products_attributes_id = $db->Insert_ID();

          //mc12345678 probably want to obtain the same merged data as above related to the orders_products_attributes_id.
          $this->notify('NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM', array_merge(array('orders_products_attributes_id' => $order_products_attributes_id), $sql_data_array));

and for the observer on the order.php file for SBA Version 1.5.4 for ZC 1.5.3 which I am still awaiting feedback on, but works in my test environment:

<?php

/**
 * Description of class.products_with_attributes_stock: This class is used to support order information related to Stock By Attributes.  This way reduces the modifications of the includes/classes/order.php file to nearly nothing.
 *
 * @property array() $_productI This is the specific product that is being worked on in the order file.
 * @property integer $_i This is the identifier of which product is being worked on in the order file
 * @property array $_stock_info This contains information related to the SBA table associated with the product being worked on in the order file.
 * @property double $_attribute_stock_left This is the a referenced value that relates to the SBA tracked quantity that remain.
 * @property array $_stock_values The results of querying on the database for the stock remaining and other associated information.
 * @author mc12345678
 */
class products_with_attributes_stock extends base {

  //
  private $_productI;
  
  private $_i;

  private $_stock_info = array();
  
  private $_attribute_stock_left;

  private $_stock_values;
  
  /*
   * This is the observer for the includes/classes/order.php file to support Stock By Attributes when the order is being processed at the end of the purchase.
   */
  function products_with_attributes_stock() {
        global $zco_notifier;
        $zco_notifier->attach($this, array('NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM', 'NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM','NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_INIT','NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN','NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END')); 
    }    

  /*
   * Function that is activated when NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_INIT is encountered as a notifier.
   */
  //NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_INIT //Line 716
    function updateNotifyOrderProcessingStockDecrementInit(&$callingClass, $notifier, $paramsArray, & $productI, & $i) {
    $this->_i = $i;
    $this->_productI = $productI;

    $this->_stock_info = zen_get_sba_stock_attribute_info(zen_get_prid($this->_productI['id']), $this->_productI['attributes']);

    // START "Stock by Attributes"
    $attributeList = null;
    $customid = null;
    if(isset($this->_productI['attributes']) and sizeof($this->_productI['attributes']) >0){
      foreach($this->_productI['attributes'] as $attributes){
        $attributeList[] = $attributes['value_id'];
      }
      $customid = zen_get_customid($this->_productI['id'],$attributeList);
      $productI['model'] = (zen_not_null($customid) ? $customid : $productI['model']);
      $this->_productI['model'] = $productI['model'];
    }
    // END "Stock by Attributes"
  }

  /*
   * Function that is activated when NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN is encountered as a notifier.
   */
  // Line 739
  function updateNotifyOrderProcessingStockDecrementBegin(&$callingClass, $notifier, $paramsArray, &$stock_values, &$attribute_stock_left){
      global $db;

    $this->_stock_values = $stock_values;

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

      // mc12345678 If the has attibutes then perform the following work.
            if(isset($this->_productI['attributes']) and sizeof($this->_productI['attributes']) >0){
        // mc12345678 Identify a list of attributes associated with the product
                $stock_attributes_search = zen_get_sba_stock_attribute(zen_get_prid($this->_productI['id']), $this->_productI['attributes']);
        
                $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 . '"';

        // mc12345678 Identify the stock available from SBA.
              $attribute_stock_available = $db->Execute($get_quantity_query);    
        // 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'] - $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);    
        $this->_attribute_stock_left = $attribute_stock_left;
      }
    }
  }

  /*
   * Function that is activated when NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END is encountered as a notifier.
   */
  // Line 776
  function updateNotifyOrderProcessingStockDecrementEnd(&$callingClass, $notifier, $paramsArray) {
    //Need to modify the email that is going out regarding low-stock.
    //paramsArray is $i at time of development.
    if ($callingClass->email_low_stock == '' && $callingClass->doStockDecrement && $this->_stock_values->RecordCount() > 0 && $this->_attribute_stock_left <= STOCK_REORDER_LEVEL) {
      // kuroi: trigger and details for attribute low stock email
      $callingClass->email_low_stock .=  'ID# ' . zen_get_prid($this->_productI['id']) . ', ' . $this->_productI['model'] . ', ' . $this->_productI['name'] . ', ';
            foreach($this->_productI['attributes'] as $attributes){
                $callingClass->email_low_stock .= $attributes['option'] . ': ' . $attributes['value'] . ', ';
            }
            $callingClass->email_low_stock .= 'Stock: ' . $this->_attribute_stock_left . "\n\n";
        // kuroi: End Stock by Attribute additions
    }
  }

  /*
   * Function that is activated when NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM is encountered as a notifier.
   */
//Line 883
  function updateNotifyOrderDuringCreateAddedAttributeLineItem(&$callingClass, $notifier, $paramsArray) {
    /* First check to see if SBA is installed and if it is then look to see if a value is 
     *  supplied in the stock_id parameter (which should only be populated when a SBA tracked
     *  item is in the order */
//      $_SESSION['paramsArray'] = $paramsArray;
    if (defined('TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK') && zen_not_null($this->_stock_info['stock_id'])) {  
      //Need to validate that order had attributes in it.  If so, then were they tracked by SBA and then add to appropriate table.
/*          `orders_products_attributes_stock_id` INT(11) NOT NULL auto_increment, 
  `orders_products_attributes_id` INT(11) NOT NULL default '0',
  `orders_id` INT(11) NOT NULL default '0', 
  `orders_products_id` INT(11) NOT NULL default '0', 
  `stock_id` INT(11) NOT NULL default '0', 
  `stock_attribute` VARCHAR(255) NULL DEFAULT NULL, 
  `products_prid` TINYTEXT NOT NULL, */
            $sql_data_array = array('orders_products_attributes_id' =>$paramsArray['orders_products_attributes_id'],
                            'orders_id' =>$paramsArray['orders_id'], 
                            'orders_products_id' =>$paramsArray['orders_products_id'], 
                            'stock_id' => $this->_stock_info['stock_id'], 
                            'stock_attribute' => $this->_stock_info['stock_attribute'], 
                            'products_prid' =>$paramsArray['products_prid']);
    zen_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES_STOCK, $sql_data_array); //inserts data into the TABLE_ORDERS_PRODUCTS_ATTRIBUTES_STOCK table.

    }
  } //endif NOTIFY_ORDER_DURING_CREATE_ADDED_ATTRIBUTE_LINE_ITEM - mc12345678
  
  
  /*
   * Generic function that is activated when any notifier identified in the observer is called but is not found in one of the above previous specific update functions is encountered as a notifier.
   */
  function update(&$callingClass, $notifier, $paramsArray) {
    global $db;
    
    if ($notifier == 'NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM'){
      
    }
    if ($notifier == 'NOTIFY_ORDER_PROCESSING_ATTRIBUTES_BEGIN') {
      
//      $stock_attribute = zen_get_sba_stock_attribute(zen_get_prid($this->products[$i]['id']), $this->products[$i]['attributes']);
//      $stock_id = zen_get_sba_stock_attribute_id(zen_get_prid($this->products[$i]['id']), $this->products[$i]['attributes']); //true; // Need to use the $stock_attribute/attributes to obtain the attribute id.
    }

    } //end update function - mc12345678
} //end class - mc12345678

?>

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

24 Nov 2014, 7:06 AM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: order notifiers

Also, for what it's worth. I have a method that can deconstruct the orders/orders_attributes tables assuming that the sequencing of the attributes is and always is as the code is currently written, though it requires working all methods of prid generation to verify/ensure that the match is performed in the correct sequence, and it relies on some data remaining currrent (ie, not being removed from the database). This method while possible seemed to me to be incomplete and necessitating a separate orders table associated with SBA. Currently it seems like the existing orders attributes table could be expanded to hold the data of the sba table; however, that could foul the normalization of the orders_products_attributes table if an attributed product was not tracked by SBA. So... Separate table it became. The data captured there may still be in question/less than desired, but it is a start, with more to likely follow. :)

I know I had seen someone asking about attributes and storage/retrieval so I thought I would expand on the solution I proposed.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

28 Dec 2015, 7:25 PM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: order notifiers

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.