Forums / Managing Customers and Orders / First Notifiers upon clicking "add this to my cart"

First Notifiers upon clicking "add this to my cart"

Started by dave224 on 19 Oct 2013, 4:41 PM · Views: 3,535

Results 1 to 20 of 27
19 Oct 2013, 4:41 PM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

First Notifiers upon clicking "add this to my cart"

Can someone please identify the first several notifiers encountered when a customer clicks "Add this to my Cart" on a product page?

Thank you!

Dave

19 Oct 2013, 5:17 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

They're not notifiers, per se, but after you've clicked "Add to Cart" (or any other cart-related action) the file /includes/main_cart_actions.php performs some of the first processing for the subsequent page-load. The first thing it does is to load and run any .php files present in /includes/extra_cart_actions.

You can see what goes on in /includes/main_cart_actions.php to get a feel for what actions are available to process.

19 Oct 2013, 7:10 PM
#3
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

Thanks lat9. I can see where additional processing could be added in extra_cart_actions.php. Following the processing there, case add_product is executed which executes actionAddProduct in includes/classes/shopping_cart.php. I get lost there and fail to comprehend where processing goes from there. Still no notifiers encountered!

Dave

20 Oct 2013, 12:45 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

Perhaps if you'd describe what processing you'd like to perform after the customer clicks "Add to Cart", I could give you a more informed answer.

If you want to do something in addition to the "standard" add-to-cart processing, I initially sent you to the right place. Take a look, for example, at Limit Quantities for Downloads and Virtual Products. That plugin inspects the products going into a cart on an 'add_product' or 'update_product' action before the standard cart-handler and either allows the addition/update or refuses it with a message to the customer.

The cart-related actions are one place where the additional code can override processing without hooking a notifier.

20 Oct 2013, 3:13 PM
#5
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

I'm trying to implement donations on my site. I currently have set up a product with cost of $1.00, so the customer can establish a donation by setting quantity. But this approach does not appear very professional since the shopping cart and other checkout pages show a unit price of $1.00, the customer selected quantity and the total donation. I would much rather have a quantity of 1 and the unit price and total price equal to the donation.

The processing I want to perform is to set the quantity purchased to 1 and the product unit price to the quantity entered (for the donation product only). In other words, I want to reverse quantity and unit price for donation product when the user clicks "add to cart". So I've been looking for a notifier where the user entered quantity has been processed on the server side and the unit price has been extracted from the database but before any display on the shopping cart page or sidebox shopping cart, and that the new quantity and unit price will persist through the remaining checkout pages.

The extra cart actions approach seems to me to be located too early in the processing as I don't believe quantity and unit price have been determined yet. I have made several mods using the notifier approach with great success. The problem now is to find a notifier best suited to the processing needed and that's where I get lost.

Note that this approach is an experiment. I'm not sure it will work especially with post-sale admin report generation. But I want to try!

Thanks for any advice!

Dave

20 Oct 2013, 3:54 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

Dave, you're right; the extra_cart_actions is too early for what you want to do. You'll need to get very familiar with the inner workings of /includes/classes/shopping_cart.php to get the effect you want for the storefront and you'll also need (as you indicated) to do some changes to the admin orders-related processing (for invoices and packingslips) to get it all tied together.

Here are a couple of the notifiers that you'll need to play with:

  1. NOTIFIER_CART_COUNT_CONTENTS_START ... I'm assuming that you want your donation product to show as a single item in the cart ...
  2. NOTIFIER_CART_GET_PRODUCTS_END ... Here's where the products (and quantity) currently in the cart are determined and returned as an array. I'm guessing that you could just swap the quantity and final_price elements for your donation product.
    There might be others within the shoppingCart class, but those two will get you started. You'll need to verify the display/operation of the shopping_cart, checkout_confirmation and account_history_info pages at a minimum to make sure that the display is what you want as well as how the confirmation emails go out to the customer.
20 Oct 2013, 4:05 PM
#7
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

lat9, thanks. I initially tried NOTIFY_HEADER_END_SHOPPING_CART without much success. I had been looking at NOTIFIER_CART_GET_PRODUCTS_END and am about to test it. I haven't looked at NOTIFIER_CART_COUNT_CONTENTS_START yet but will. Thanks again for the advice!

Dave

20 Oct 2013, 9:11 PM
#8
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

I give up. Very frustrating and I'm very disappointed. :(

Dave

20 Oct 2013, 9:35 PM
#9
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

If you'd post the code that you've got (along with what does/doesn't work), I might be able to help ...

21 Oct 2013, 12:03 AM
#10
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

lat9,

The first problem is I can't access the variable I want to change in the observer class triggered by NOTIFIER_CART_GET_PRODUCTS_END notifier. I want to access $products_array that is set near the end of function get_products in includes/classes/shopping_cart.php. This variable is set just prior to the NOTIFIER_CART_GET_PRODUCTS_END notifier.

Code for my observer class and autoloader config files follow: Note that the observer class code simply prints out the $eventID and $products_array variable. All I see is the $eventID. No data is printed for the $products_array. So the observer class is being executed but the $product_array is not accessible.

<?php
/**
 * observer class to process donation entry box
 *
 * @package classes
 * @copyright Copyright 2003-2013 Zen Cart Development Team
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: class.processDonationEntry.php   $
 */
class processDonationEntry extends base {

 /**  constructor method !
   *
   * Attach observer class to the global $zco_notifier and watch for a single notifier event.
   */
  function __construct() {
      $this->attach($this, array('NOTIFIER_CART_GET_PRODUCTS_END'));
  }


 /**   Actual Method that does the desired activity
   *
   * Called by observed class when any of the notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   * @param array $paramsArray
   */
  function update(&$class, $eventID, $paramsArray = array()) {
    global $products_array;
    echo "eventID: " . $eventID;
//    echo "<pre>"; print_r($_SESSION); echo "</pre>";
    echo "<pre>"; print_r($products_array); echo "</pre>";
  } // end of function update
} // end of class
<?php
/**
 * autoloader activation point for donation entry box processing
 *
 * @package initSystem
 * @copyright Copyright 2003-2013 Zen Cart Development Team
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: config.processDonationEntry.php $
 */
/**
 * Autoloader to instantiate observer class
 */
$autoLoadConfig[203][] = array('autoType'=>'class',
                              'loadFile'=>'observers/class.processDonationEntry.php');
$autoLoadConfig[203][] = array('autoType'=>'classInstantiate',
                              'className'=>'processDonationEntry',
                              'objectName'=>'processDonationEntry'); 

Dave

21 Oct 2013, 12:30 AM
#11
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

lat9,
As a proof of concept, I modified the code in function get_products in includes/classes/shopping_cart.php to swap price and quantity and set final_price. See code below. This worked for the shopping cart, sidebox shopping cart and subsequent checkout pages and emails to the customer and copy to the store. The only problem was the Total Items displayed above the shopping cart on the shopping cart page.

Notes: Product ID of 600 is my donation product. The test code is set up for only one item in the cart, hence $products_array[0]['id'], for example. Actual code will deal with multiple items in the cart and hopefully will be in the observer class and not in the core code.

For full disclosure, I deleted my earlier code which did not work as well as the following code. When I recreated the code, it seemed to work much better! The code below is located at the end of the function get_products.

      $products_array[] = array('id' => $products_id,
                                 'category' => $products->fields['master_categories_id'],
                                 'name' => $products->fields['products_name'],
                                 'model' => $products->fields['products_model'],
                                 'image' => $products->fields['products_image'],
                                 'price' => ($products->fields['product_is_free'] =='1' ? 0 : $products_price),
       //                                    'quantity' => $this->contents[$products_id]['qty'],
                                 'quantity' => $new_qty,
                                 'weight' => $products->fields['products_weight'] + $this->attributes_weight($products_id),
                                 // fix here
                                 'final_price' => ($products_price + $this->attributes_price($products_id)),
                                 'onetime_charges' => ($this->attributes_price_onetime_charges($products_id, $new_qty)),
                                 'tax_class_id' => $products->fields['products_tax_class_id'],
                                 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''),
                                 'attributes_values' => (isset($this->contents[$products_id]['attributes_values']) ? $this->contents[$products_id]['attributes_values'] : ''),
                                 'products_priced_by_attribute' => $products->fields['products_priced_by_attribute'],
                                 'product_is_free' => $products->fields['product_is_free'],
                                 'products_discount_type' => $products->fields['products_discount_type'],
                                 'products_discount_type_from' => $products->fields['products_discount_type_from']);
       if ($products_array[0]['id'] == 600 ) {
         $products_array[0]['price'] = $products_array[0]['quantity'];
         $products_array[0]['final_price'] = $products_array[0]['price'];
         $products_array[0]['quantity'] = 1;
       }
//        echo "<pre>"; print_r($products_array); echo "</pre>";
     }
   }
   $this->notify('NOTIFIER_CART_GET_PRODUCTS_END');
   return $products_array;

So the two problems now are access to $products_array in my observer class and correcting quantity in the line on the shopping cart page above the cart.

Dave

21 Oct 2013, 6:58 AM
#12
dw08gm avatar

dw08gm

Totally Zenned

Join Date:
Sep 2008
Location:
DownUnder, overlooking South Pole.
Posts:
1,022
Plugin Contributions:
2

Re: First Notifiers upon clicking "add this to my cart"

If this code is to apply only to donation products, I am sure there is an easier way to achieve this, like changing the quantity to accept 0.01 increments (for decimalised currency amounts).

21 Oct 2013, 12:13 PM
#13
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

dw08gm,
While your suggestion would allow donations with 0.01 increments, I don't see how it would change the basic functionality that I want, namely to have a quantity of 1 and a unit price and total price equal to the quantity entered. That is I want to see 1 donation with a total amount of whatever the customer enters.
All the best,

Dave

21 Oct 2013, 12:16 PM
#14
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

lat9,
Since get_products is executed multiple times during a session, I should add code to switch the variables only once per session. Otherwise, the variables get switched, then switched back, etc. It is fortuitous that get_products is executed three times in my current testing, so the variables end up as I want them.
Dave

21 Oct 2013, 12:53 PM
#15
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

Dave224:

lat9,

The first problem is I can't access the variable I want to change in the observer class triggered by NOTIFIER_CART_GET_PRODUCTS_END notifier. I want to access $products_array that is set near the end of function get_products in includes/classes/shopping_cart.php. This variable is set just prior to the NOTIFIER_CART_GET_PRODUCTS_END notifier.

Code for my observer class and autoloader config files follow: Note that the observer class code simply prints out the $eventID and $products_array variable. All I see is the $eventID. No data is printed for the $products_array. So the observer class is being executed but the $product_array is not accessible.

<?php /** * observer class to process donation entry box * * @package classes * @copyright Copyright 2003-2013 Zen Cart Development Team * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: class.processDonationEntry.php $ */ class processDonationEntry extends base { /** constructor method ! * * Attach observer class to the global $zco_notifier and watch for a single notifier event. */ function __construct() { $this->attach($this, array('NOTIFIER_CART_GET_PRODUCTS_END')); } /** Actual Method that does the desired activity * * Called by observed class when any of the notifiable events occur * * @param object $class * @param string $eventID * @param array $paramsArray */ function update(&$class, $eventID, $paramsArray = array()) { global $products_array; echo "eventID: " . $eventID; // echo "<pre>"; print_r($_SESSION); echo "</pre>"; echo "<pre>"; print_r($products_array); echo "</pre>"; } // end of function update } // end of class ``` > > ```php <?php /** * autoloader activation point for donation entry box processing * * @package initSystem * @copyright Copyright 2003-2013 Zen Cart Development Team * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: config.processDonationEntry.php $ */ /** * Autoloader to instantiate observer class */ $autoLoadConfig[203][] = array('autoType'=>'class', 'loadFile'=>'observers/class.processDonationEntry.php'); $autoLoadConfig[203][] = array('autoType'=>'classInstantiate', 'className'=>'processDonationEntry', 'objectName'=>'processDonationEntry'); ``` > > Dave Dave, in situations like this (the variable you need to modify is not within the scope of the notification), I try to take the minimalist approach to making core-file changes. What I suggest here is simply slapping a "global $products_array;" statement in the get_products function in the shoppingCart class as well as towards the top of your observer module. That way, your observer has access to the products_array without making massive changes to the core code.
21 Oct 2013, 12:56 PM
#16
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

Dave224:

lat9,
Since get_products is executed multiple times during a session, I should add code to switch the variables only once per session. Otherwise, the variables get switched, then switched back, etc. It is fortuitous that get_products is executed three times in my current testing, so the variables end up as I want them.
Dave
Dave, you're modifying an output array, not the data itself. I think that you want simply to swap 'quantity' and 'final_price' (I don't think that the 'price' variable comes into play here, but I'll look and report back).

22 Oct 2013, 3:00 AM
#17
cvhainb avatar

cvhainb

Zen Follower

Join Date:
Sep 2008
Posts:
212
Plugin Contributions:
19

Re: First Notifiers upon clicking "add this to my cart"

You can:

  1. Create a new file in /extra_cart_actions/extra_cart.php
  2. You can copy all code in function actionAddProduct() and modify it if you want.

Our Site: http://zucando.com
Marketing Plugins: Marketing Modules
Free Response Templates: Responsive Templates

22 Oct 2013, 12:01 PM
#18
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

Wouldn't the unmodified core code be executed as well? Is that desirable?
Dave

22 Oct 2013, 12:36 PM
#19
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,137
Plugin Contributions:
56

Re: First Notifiers upon clicking "add this to my cart"

Dave224:

Wouldn't the unmodified core code be executed as well? Is that desirable?
Dave
If you take the approach suggested by cvhainb, you'd make your modifications and then set $_GET['action'] = '' so that the main_cart_actions.php didn't replicate your processing.

26 Oct 2013, 2:09 AM
#20
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: First Notifiers upon clicking "add this to my cart"

My approach works pretty well. Donation quantity is 1 and unit price and total price are set to the entered quantity. Everything seems to work throughout the checkout process.

However, if the quantity of another product in the cart is changed on the shopping cart page, the unit price and the total price of the donation product revert to $1.00, consistent with the desired donation quantity which remains at 1. In effect, the entered quantity is lost. I'm not sure where in the code this is happening. One possibility is function calculate in includes/classes/shopping_cart.php which is called from functions show_total, show_weight, free_shipping_items, free_shipping_prices, and free_shipping_weight in shopping_cart.php. The donation product is a free shipping product.

Any suggestions on how to proceed (other than give up again)?

Dave