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 21 to 27 of 27
26 Oct 2013, 1:45 PM
#21
lat9 avatar

lat9

Administrator

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

First Notifiers upon clicking "add this to my cart"

It's the function actionUpdateProduct (called by /includes/main_cart_actions.php when $_GET['action'] is set to 'update_product') that's biting you. Your code has modified the output (swapping the quantity and price) for the shopping_cart page, e.g. quantity = 1, price = $50.00, while the cart still contains quantity = 50, price = $1.00.

When the "other" product's quantity is updated, that function sees the "quantity = 1" as a replacement for the "quantity = 50" so you'll need to trick it into doing the right thing.

For this case, your best approach is to add another file to the mix: /includes/extra_cart_actions/fix_donation_quantity.php to
check to see if a donation product is already in the cart and is not being deleted (i.e. the quantity set to 0) and, if so, set the posted quantity value to be the quantity that's currently in the cart. Please note that this code assumes that only one donation product will be in the cart at any one time.

if (isset($_GET['action']) && $_GET['action'] == 'update_product') {
  for ($i = 0, $n = sizeof($_POST['products_id']); $i < $n; $i++) {
    if ($_POST['products_id'] == DONATION_PRODUCT_ID) {
      if (! (in_array(DONATION_PRODUCT_ID, (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array())) || $_POST['cart_quantity'][$i] == 0) ){
        foreach ($_SESSION['cart']->contents as $uprid => $contents) {
          if (((int)$uprid) == DONATION_PRODUCT_ID) {
            $_POST['cart_quantity'][$i] = $contents['qty'];
            break; // out of foreach
          }
        }
      }
    }
  }
}
26 Oct 2013, 4:27 PM
#22
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

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

Been watching this thread with GREAT interest.. would be FABULOUS to see this turned into a "Donations" add-on...:smile: IJS.. lol

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

26 Oct 2013, 6:05 PM
#23
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,
Wow! You're awesome! Have a cup/glass of your favorite beverage on me. Donation on its way!

Small correction to your code though. In line 3 of your post, $_POST['products_id'] needs an index, i.e., $_POST['products_id'][$i]. My code (really yours!) for the new file follows:

<?php
/**
 * Edit to retain donation amount if the quantity of another item in the shopping cart is changed
 * Code written and supplied by lat9 on the Zen Cart forum
 */
 
if (isset($_GET['action']) && $_GET['action'] == 'update_product') {
  for ($i = 0, $n = sizeof($_POST['products_id']); $i < $n; $i++) {
    if ($_POST['products_id'][$i] == DONATION_PRODUCT_ID) { // index $i added to LAT9 supplied code
      if (! (in_array(DONATION_PRODUCT_ID, (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array())) || $_POST['cart_quantity'][$i] == 0) ){
        foreach ($_SESSION['cart']->contents as $uprid => $contents) {
          if (((int)$uprid) == DONATION_PRODUCT_ID) {
            $_POST['cart_quantity'][$i] = $contents['qty'];
            break; // out of foreach
          }
        }
      }
    }
  }
}
26 Oct 2013, 6:16 PM
#24
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, thanks for the update (and I'm glad you were able to make "proper" sense of the code I posted)!

27 Oct 2013, 12:52 AM
#25
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

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

Can't wait to get home and try this..

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.

27 Oct 2013, 4:25 PM
#26
dave224 avatar

dave224

Zen Follower

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

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

DivaVocals,

I'm not planning to submit and support an add-on but will show you what I did. Caution: while the following code appears to work for me, I suggest you thoroughly test this code on a development site before you go live with it.

  1. Create a donation product - I called mine "Donation to <name of organization>". Replace <name of organization> with appropriate text. Set the price at $1.00. You can set the Product is Virtual to yes, or in my case I set it to no because of a free gift we provide for a donation. I set Always Free Shipping to yes (due to the free gift). I set up attributes for the donation product so that the customer can select which free gift they want. But you don't need to set up attributes for this code to work.

  2. Modify includes/templates/<your_template>/templates/tpl_product_info_display.php to not display the donation unit price and change the "add to cart" text where you enter quantity. Replace <your_template> with the appropriate text for your template. Changes to sections of the file are indicated in red in the code snippets below.

<!--bof Product Price block -->
<h2 id="productPrices" class="productGeneral">
<?php
// base price
  if ($show_onetime_charges_description == 'true') {
    $one_time = '<span >' . TEXT_ONETIME_CHARGE_SYMBOL . TEXT_ONETIME_CHARGE_DESCRIPTION . '</span><br />';
  } else {
    $one_time = '';
  }
  if ((int)$_GET['products_id'] <> DONATION_PRODUCT_ID) {
    echo $one_time . ((zen_has_product_attributes_values((int)$_GET['products_id']) and $flag_show_product_info_starting_at == 1) ? TEXT_BASE_PRICE : '') . zen_get_products_display_price((int)$_GET['products_id']);
  }
?></h2>
<!--eof Product Price block -->
<!--bof Add to Cart Box -->
<?php
if (CUSTOMERS_APPROVAL == 3 and TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM == '') {
  // do nothing
} else {
?>
            <?php
    if ($_GET['products_id'] <> DONATION_PRODUCT_ID) {
      $display_qty = (($flag_show_product_info_in_cart_qty == 1 and $_SESSION['cart']->in_cart($_GET['products_id'])) ? '<p>' . PRODUCTS_ORDER_QTY_TEXT_IN_CART . $_SESSION['cart']->get_quantity($_GET['products_id']) . '</p>' : '');
	} else {
      $display_qty = (($flag_show_product_info_in_cart_qty == 1 and $_SESSION['cart']->in_cart($_GET['products_id'])) ? '<p>' . DONATION_ORDER_QTY_TEXT_IN_CART . $_SESSION['cart']->get_quantity($_GET['products_id']) . '</p>' : '');	
	}
    if ($products_qty_box_status == 0 or $products_quantity_order_max== 1) {
      // hide the quantity box and default to 1
      $the_button = '<input type="hidden" name="cart_quantity" value="1" />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT);
    } else {
      // show the quantity box
      if ($_GET['products_id'] <> DONATION_PRODUCT_ID) {      
      	$the_button = PRODUCTS_ORDER_QTY_TEXT . '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($_GET['products_id'])) . '" maxlength="6" size="4" /><br />' . zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . '<br />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT);
      } else {
		$the_button = DONATION_ORDER_QTY_TEXT . '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($_GET['products_id'])) . '" maxlength="6" size="4" /><br />' . zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . '<br />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT);
	  }
    }
    $display_button = zen_get_buy_now_button($_GET['products_id'], $the_button);
  ?>
  <?php if ($display_qty != '' or $display_button != '') { ?>
    <div id="cartAdd">
    <?php
      echo $display_qty;
      echo $display_button;
            ?>
          </div>
  <?php } // display qty and button ?>
<?php } // CUSTOMERS_APPROVAL == 3 ?>
<!--eof Add to Cart Box-->
  1. Be sure to define the constants DONATION_PRODUCT_ID, DONATION_ORDER_QTY_TEXT_IN_CART, and DONATION_ORDER_QTY_TEXT. In my case, I used 600, 'Donation in Cart: ', and 'Donation Amount: ' respectively. I included them in includes/languages/<your_template>/english.php in the section concerning cart contents. I'm certain there are other files where you can define these constants.

  2. Set up an observer for the NOTIFIER_CART_GET_PRODUCTS_END event. Two files will need to be set up. First, includes/autoloaders/config.processDonationEntry.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'); 

Second, includes/classes/observers/class.processDonationEntry.php

<?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 $db, $products_array;
    foreach ($products_array as &$product) {
        if ($product['id'] == DONATION_PRODUCT_ID ) {
          $product['price'] = $product['quantity'];
          $product['final_price'] = $product['price'];
          $product['quantity'] = 1;
        }
    } // end of foreach
    unset($product); // break reference with the last element
//        echo "<pre>"; print_r($products_array); echo "</pre>";
  } // end of function update
} // end of class
  1. Modify a core file (sorry) to allow the observer function access to a variable in file includes/classes/shopping_cart.php in function get_products.
/**
 * Method to return details of all products in the cart
 *
 * @param boolean whether to check if cart contents are valid
 * @return array
 */
function get_products($check_for_valid_cart = false) {
  global $db, $products_array;

  $this->notify('NOTIFIER_CART_GET_PRODUCTS_START');
  1. Finally, include a file developed by lat9 to retain the customer entered donation if the quantity of another item in the cart is changed. This file is includes/extra_cart_actions/fix_donation_quantity.php
<?php
/**
 * Edit to retain donation amount if the quantity of another item in the shopping cart is changed
 * Code written and supplied by lat9 on the Zen Cart forum
 */
 
if (isset($_GET['action']) && $_GET['action'] == 'update_product') {
  for ($i = 0, $n = sizeof($_POST['products_id']); $i < $n; $i++) {
    if ($_POST['products_id'][$i] == DONATION_PRODUCT_ID) { // index $i added to LAT9 supplied code
      if (! (in_array(DONATION_PRODUCT_ID, (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array())) || $_POST['cart_quantity'][$i] == 0) ){
        foreach ($_SESSION['cart']->contents as $uprid => $contents) {
          if (((int)$uprid) == DONATION_PRODUCT_ID) {
            $_POST['cart_quantity'][$i] = $contents['qty'];
            break; // out of foreach
          }
        }
      }
    }
  }
}

That should be all that is needed to implement a donation product where the quantity is 1 and the donation is the amount entered rather than seeing a quantity of 50 $1.00 donations in the shopping cart throughout the checkout process. Be sure to test your implementation. Good luck!

Dave

27 Oct 2013, 4:40 PM
#27
divavocals avatar

divavocals

Totally Zenned

Join Date:
Jan 2007
Location:
Los Angeles, California, United States
Posts:
10,011
Plugin Contributions:
3

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

Dave you are a doll for summarizing this.. Thanks for that! Will test out when I get home.. > Dave224:

DivaVocals,

I'm not planning to submit and support an add-on but will show you what I did. Caution: while the following code appears to work for me, I suggest you thoroughly test this code on a development site before you go live with it.

  1. Create a donation product - I called mine "Donation to <name of organization>". Replace <name of organization> with appropriate text. Set the price at $1.00. You can set the Product is Virtual to yes, or in my case I set it to no because of a free gift we provide for a donation. I set Always Free Shipping to yes (due to the free gift). I set up attributes for the donation product so that the customer can select which free gift they want. But you don't need to set up attributes for this code to work.

  2. Modify includes/templates/<your_template>/templates/tpl_product_info_display.php to not display the donation unit price and change the "add to cart" text where you enter quantity. Replace <your_template> with the appropriate text for your template. Changes to sections of the file are indicated in red in the code snippets below.

<!--bof Product Price block --> <h2 id="productPrices" class="productGeneral"> <?php // base price if ($show_onetime_charges_description == 'true') { $one_time = '<span >' . TEXT_ONETIME_CHARGE_SYMBOL . TEXT_ONETIME_CHARGE_DESCRIPTION . '</span><br />'; } else { $one_time = ''; } if ((int)$_GET['products_id'] <> DONATION_PRODUCT_ID) { echo $one_time . ((zen_has_product_attributes_values((int)$_GET['products_id']) and $flag_show_product_info_starting_at == 1) ? TEXT_BASE_PRICE : '') . zen_get_products_display_price((int)$_GET['products_id']); } ?></h2> <!--eof Product Price block --> ``` > > ``` <!--bof Add to Cart Box --> <?php if (CUSTOMERS_APPROVAL == 3 and TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM == '') { // do nothing } else { ?> <?php if ($_GET['products_id'] <> DONATION_PRODUCT_ID) { $display_qty = (($flag_show_product_info_in_cart_qty == 1 and $_SESSION['cart']->in_cart($_GET['products_id'])) ? '<p>' . PRODUCTS_ORDER_QTY_TEXT_IN_CART . $_SESSION['cart']->get_quantity($_GET['products_id']) . '</p>' : ''); } else { $display_qty = (($flag_show_product_info_in_cart_qty == 1 and $_SESSION['cart']->in_cart($_GET['products_id'])) ? '<p>' . DONATION_ORDER_QTY_TEXT_IN_CART . $_SESSION['cart']->get_quantity($_GET['products_id']) . '</p>' : ''); } if ($products_qty_box_status == 0 or $products_quantity_order_max== 1) { // hide the quantity box and default to 1 $the_button = '<input type="hidden" name="cart_quantity" value="1" />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT); } else { // show the quantity box if ($_GET['products_id'] <> DONATION_PRODUCT_ID) { $the_button = PRODUCTS_ORDER_QTY_TEXT . '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($_GET['products_id'])) . '" maxlength="6" size="4" /><br />' . zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . '<br />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT); } else { $the_button = DONATION_ORDER_QTY_TEXT . '<input type="text" name="cart_quantity" value="' . (zen_get_buy_now_qty($_GET['products_id'])) . '" maxlength="6" size="4" /><br />' . zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . '<br />' . zen_draw_hidden_field('products_id', (int)$_GET['products_id']) . zen_image_submit(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT); } } $display_button = zen_get_buy_now_button($_GET['products_id'], $the_button); ?> <?php if ($display_qty != '' or $display_button != '') { ?> <div id="cartAdd"> <?php echo $display_qty; echo $display_button; ?> </div> <?php } // display qty and button ?> <?php } // CUSTOMERS_APPROVAL == 3 ?> <!--eof Add to Cart Box--> ``` > > 3. Be sure to define the constants DONATION_PRODUCT_ID, DONATION_ORDER_QTY_TEXT_IN_CART, and DONATION_ORDER_QTY_TEXT. In my case, I used 600, 'Donation in Cart: ', and 'Donation Amount: ' respectively. I included them in includes/languages/<your_template>/english.php in the section concerning cart contents. I'm certain there are other files where you can define these constants. > > 4. Set up an observer for the NOTIFIER_CART_GET_PRODUCTS_END event. Two files will need to be set up. First, includes/autoloaders/config.processDonationEntry.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'); ``` > > Second, includes/classes/observers/class.processDonationEntry.php > > ``` <?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 $db, $products_array;
    foreach ($products_array as &$product) {
    if ($product['id'] == DONATION_PRODUCT_ID ) {
    $product['price'] = $product['quantity'];
    $product['final_price'] = $product['price'];
    $product['quantity'] = 1;
    }
    } // end of foreach
    unset($product); // break reference with the last element
    // echo "<pre>"; print_r($products_array); echo "</pre>";
    } // end of function update
    } // end of class
> 
> 5. Modify a core file (sorry) to allow the observer function access to a variable in file includes/classes/shopping_cart.php in function get_products.
> 
>   ```
/**
   * Method to return details of all products in the cart
   *
   * @param boolean whether to check if cart contents are valid
   * @return array
   */
  function get_products($check_for_valid_cart = false) {
    global $db, $products_array;

    $this->notify('NOTIFIER_CART_GET_PRODUCTS_START');
  1. Finally, include a file developed by lat9 to retain the customer entered donation if the quantity of another item in the cart is changed. This file is includes/extra_cart_actions/fix_donation_quantity.php
<?php /** * Edit to retain donation amount if the quantity of another item in the shopping cart is changed * Code written and supplied by lat9 on the Zen Cart forum */ if (isset($_GET['action']) && $_GET['action'] == 'update_product') { for ($i = 0, $n = sizeof($_POST['products_id']); $i < $n; $i++) { if ($_POST['products_id'][$i] == DONATION_PRODUCT_ID) { // index $i added to LAT9 supplied code if (! (in_array(DONATION_PRODUCT_ID, (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array())) || $_POST['cart_quantity'][$i] == 0) ){ foreach ($_SESSION['cart']->contents as $uprid => $contents) { if (((int)$uprid) == DONATION_PRODUCT_ID) { $_POST['cart_quantity'][$i] = $contents['qty']; break; // out of foreach } } } } } } ``` > > That should be all that is needed to implement a donation product where the quantity is 1 and the donation is the amount entered rather than seeing a quantity of 50 $1.00 donations in the shopping cart throughout the checkout process. Be sure to test your implementation. Good luck! > > Dave

My Site - Zen Cart & WordPress integration specialist
I don't answer support questions via PM. Post add-on support questions in the support thread. The question & the answer will benefit others with similar issues.