If this is your first visit, check our documentation. To post, register or login.
Forums / Code Collaboration / using observers...

using observers...

Started by carlwhat on 24 Jul 2020, 9:25 PM · Views: 3,024

Results 1 to 3 of 3
24 Jul 2020, 9:25 PM
#1
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

using observers...

v157.

i am curious about the order class and specifically the number of observers. i see 39 different observers. i'm curious if anyone makes use of this one:

NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM

if we look at the code in

includes/classes/order.php

lines: 871-880

            zen_db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);

            $order_products_id = $db->insert_ID();

            $this->notify('NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM',
                array_merge(array('orders_products_id' => $order_products_id, 'i' => $i), $sql_data_array),
                $order_products_id);

            $this->notify('NOTIFY_ORDER_PROCESSING_CREDIT_ACCOUNT_UPDATE_BEGIN');
            $order_total_modules->update_credit_account($i);//ICW ADDED FOR CREDIT CLASS SYSTEM

            $this->notify('NOTIFY_ORDER_PROCESSING_ATTRIBUTES_BEGIN');

would it not make more sense to have the obsever BEFORE the insert? what is the point of having the $sql_data_array after the insert is already done?

the notifier is valuable in that one could now, in theory, manipulate data within that row. but would it make more sense to have the observer prior to the insert being done? from a performance standpoint, manipulating the data and then hitting the db seems more efficient to me.

and while i am sure there is a reason, i suppose it escapes me the need to have 3 different observers almost directly in a row.

curious about anyone's thoughts on this.

thanks in advance.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

25 Jul 2020, 10:15 PM
#2
drbyte avatar

drbyte

Sensei

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

Re: using observers...

The "NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM" notifier was placed there, and named using "ADDED" to imply past-tense, that the action is finished.
It supplies details of the record added and the data used to do so.

You are correct: there's not one to intercept the data before the record is created.

.
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.

26 Jul 2020, 8:42 PM
#3
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: using observers...

Stock by Attributes in support of earlier operations used to specifically observe that notifier. It still does if/when installed to ZC 1.5.1, but now because other specific code is monitored and performs necessary actions, the operation(s) previously performed are actually captured in another observer.

Looking over the (recent?) history of the notifier, the data that was associated with the added product is provided; however, it wasn't until ZC 1.5.5 that the notifier actually included the database record associated with the previous insertion. Note that with this information there seems to be two sides of the discussion, 1) the notifer is there to alter the information to be stored into the database or to build off of it or as used in SBA, 2) to use the record indicator to be able to populate a separate table that references the original information. This allows using a different database table to manage additional information rather than to alter an existing ZC table.

As far as a "pre-use" notifier, I would say that there is actually a notifier that can be used just before that database insertion: NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END can be used to alter any of the $this->products data by referencing the variable of the first parameter of the observer (I tend to use &$callingClass) as in $callingClass->products[$passedIVar]['whateverKeyIsNeeded']. Only thing is that if there are other pieces of data to be captured/stored, then they have to be addressed in the later observer because the array that is built does not include some sort of array_merge or other builder for additional array data. So one way to use the notifiers in association with their "names" to incorporate information into the orders_products table without modifying the order.php class would be to possibly use the NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END notifier to first populate all of the $this->products data necessary, then in observing NOTIFY_ORDER_DURING_CREATE_ADDED_PRODUCT_LINE_ITEM, to use the new insertion record ($order_products_id) to then store the additional information that was not in $sql_data_array or alternatively to store only specific additional information that was previously captured in the $this->products array.

I do know that some of the way that notifiers are used/what they pass is a result of a request made here: https://www.zen-cart.com/showthread.php?215320-order-notifiers as a result of seeing how data was being handled and the operation possibilities based on what had been provided over time. Anyways, if there is something that is modified about the discussed notifier, I believe it is still necessary to at least pass along or provide a method to directly obtain the record(s) generated for relational use across other database records.

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