Forums / Setting Up Categories, Products, Attributes / Restrict Products in Cart Based on Other Products in Cart

Restrict Products in Cart Based on Other Products in Cart

Started by lindasdd on 22 Oct 2013, 3:29 AM · Views: 1,157

Results 1 to 6 of 6
22 Oct 2013, 3:29 AM
#1
lindasdd avatar

lindasdd

Zen Follower

Join Date:
Jun 2007
Posts:
474
Plugin Contributions:
0

Restrict Products in Cart Based on Other Products in Cart

Need some help setting up code so that if Product A,B,or C are added to the cart, the site doesn't allow other products to be added, restricts checkout, and a note indicates the problem.

I thought it might be doable with some modifications to Dr.Byte's code seen here:
http://www.zen-cart.com/showthread.php?199568-Different-payment-methods-for-different-products&highlight=subscription

by implementing steps 1 and 2, yet so far I have been unsuccessful. Might that post be missing something?

I am currently using Fast and Easy Ajax Checkout so that throws a loop in the mix. But I would think my restriction should be kicking in before the checkout process is even allowed to begin.

22 Oct 2013, 2:32 PM
#2
lat9 avatar

lat9

Administrator

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

Re: Restrict Products in Cart Based on Other Products in Cart

What if something's already in the cart when a customer tries to add A, B or C?

31 Oct 2013, 9:04 PM
#3
lindasdd avatar

lindasdd

Zen Follower

Join Date:
Jun 2007
Posts:
474
Plugin Contributions:
0

Re: Restrict Products in Cart Based on Other Products in Cart

Same thing. In my head it would operate similar to the out of stock process.

If Product A is in the cart or added to the cart and Product Z gets added, a note pops up on the shopping cart page saying, "These products need to be ordered separately."

1 Nov 2013, 9:05 PM
#4
lat9 avatar

lat9

Administrator

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

Re: Restrict Products in Cart Based on Other Products in Cart

Put the following code into /includes/extra_cart_actions/one_per_cart.php; you'll need to edit the ONE_PER_CART_PRODUCT_IDS value to reflect the product_ids of your restricted items:

<?php
/**
 * Extra shopping Cart actions supported.
 *
 * The main cart actions supported by the current shoppingCart class.
 *
 * NOTE: Many of the defines used in this module have been included by
 * class.init_quote.php's inclusion of /includes/quotes_status_array.php.
 *
 */
define('ONE_PER_CART_PRODUCT_IDS', '14,4,6,11,15'); /*Identify the product_ids of the products that must be ordered separately*/
define('ERROR_ONE_PER_CART', 'Sorry, but you can\'t add "%s" to an order that already contains another item.');
define('ERROR_ONE_PER_CART_COMBINE', 'Sorry, but you can\'t add another item to an order that already contains "%s".');

switch ($_GET['action']) {
  case 'add_product': 
    $onePerCartIDs = explode (',', ONE_PER_CART_PRODUCT_IDS);
    $inCartName = '';
    foreach ($onePerCartIDs as $theID) {
      if ($theID == $_POST['products_id']) {
        if ($_SESSION['cart']->count_contents() > 0) {
          $error = true;
          $messageStack->add('product_info', sprintf(ERROR_ONE_PER_CART, zen_get_products_name((int)$_POST['products_id'])));
        }
      } elseif ($_SESSION['cart']->in_cart_mixed($theID) != 0) {
        $inCartName = zen_get_products_name($theID);
      }
    }
    if (!$error && $inCartName != '') {
      $error = true;
      $messageStack->add('product_info', sprintf(ERROR_ONE_PER_CART_COMBINE, $inCartName));
    }
  }

  if ($error) {
    unset($_GET['action']);
  }
  break;
}
2 Nov 2013, 12:46 AM
#5
lindasdd avatar

lindasdd

Zen Follower

Join Date:
Jun 2007
Posts:
474
Plugin Contributions:
0

Re: Restrict Products in Cart Based on Other Products in Cart

I failed to mention I'm using v1.3.9h

2 Nov 2013, 2:26 PM
#6
lat9 avatar

lat9

Administrator

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

Re: Restrict Products in Cart Based on Other Products in Cart

The code I posted should work for Zen Cart 1.3.9 and 1.5.x ...