Sanjoy Roy

[MCM, MCP, SCJP] – Senior PHP Programmer

osCommerce – Vouchers or coupons when assigned to specific products


Filename: includes/modules/order_total/ot_coupon.php

$c_restrict_to_products 		= $coupon_result['restrict_to_products'];
$logs .= 'c_restrict_to_products: '.$c_restrict_to_products;	   
$c_restrict_to_categories 	= $coupon_result['restrict_to_categories'];
$logs .= 'c_restrict_to_categories: '.$c_restrict_to_categories;

$valid_coupon_product_id_exists = 'false';
if($c_restrict_to_products !=''){		    	  
  $db_product_ids = split("[,]", $c_restrict_to_products);		   
  for ($i=0; $i products); $i++) { 
	$logs .= 'products_id: '.$order->products[$i]['id'];
	if (in_array($order->products[$i]['id'], $db_product_ids)) {
		 $valid_coupon_product_id_exists = 'true';
	}
  }		  			  
}else if($c_restrict_to_categories !=''){
  $db_category_ids = split("[,]", $c_restrict_to_categories);
  for ($i=0; $i products); $i++) { 			
	$category_id = $this->getProductsCategoryID($order->products[$i]['id']);
	$logs .= 'products_id: '.$order->products[$i]['id'];
	$logs .= 'category_id: '.$category_id;
	if (in_array($category_id, $db_category_ids)) {
		 $valid_coupon_product_id_exists = 'true';
	}
  }  		 	  
}
$logs .= 'valid_coupon_product_id_exists: '.$valid_coupon_product_id_exists;
if ($valid_coupon_product_id_exists == 'false') {		    
  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=ot_coupon&error=' . urlencode('This coupon is not valid for the product(s) you are purchasing.' ), 'SSL'));
}

function getProductsCategoryID($products_id){
$category_id 		= 0;
$p_parent_query		=	tep_db_query("select `products_parent_id` from `products` where products_id = '".$products_id."'");
$p_parent_result	=	tep_db_fetch_array($p_parent_query);
$products_parent_id = 	$p_parent_result['products_parent_id'];

if($products_parent_id == 0){
	$sql = "SELECT `categories_id` FROM `products_to_categories` where products_id = '".$products_id."'";
	$p_parent_queryX = tep_db_query($sql);
	$category_id = $p_parent_queryX['categories_id'];
}else{
	$sql = "SELECT `categories_id` FROM `products_to_categories` where products_id = '".$products_parent_id."'";
	$p_parent_queryY = tep_db_query($sql);	
	$category_id = $p_parent_queryY['categories_id'];
}
return $category_id;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: