Force a shopper to pick a product from a category

  • Posts: 202
  • Thank you received: 2
13 years 8 months ago #9623

I want to have a category in my store that every shopper MUST select a product from before submitting their cart. Is there a way to do this that you know of?

Please Log in or Create an account to join the conversation.

  • Posts: 82866
  • Thank you received: 13373
  • MODERATOR
13 years 8 months ago #9629

You will have to edit one of the views (a checkout view or the cart file of the view product) to not display the "proceed to checkout" or "next" button when a product from that category is not in the cart and display an error message.

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14597

nicolas,

I am ready to implement this. Assuming I have one category in particular I want to force, what would I use to find whether that particular category is in the cart?

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14602

I am going to add this to the front-end Checkout->Step view. What I was thinking was to include something like the following:

if($this->step=0) {

TEST IF CATEGORY ID 24 IS IN CART

If not, do not display the NEXT button

}

If it is

{echo $this->cart->displayButton(JText::_('Back'),'back',$this->params,hikashop::currentUrl(),'history.back();return false;','id="hikashop_checkout_next_button"');
}

I can get the IF's correct, if I just know what to use to see if that category exists in the cart.

I hope I am making sense!!!

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14606

Thinking about this, the ideal situation would be to have the check performed on the "Proceed to Checkout" function, with a pop-up message indicating the need to add a product from this category to the cart, then redirecting to that category instead of the checkout.

Please Log in or Create an account to join the conversation.

  • Posts: 82866
  • Thank you received: 13373
  • MODERATOR
13 years 7 months ago #14607

Not tested:

$ids=array();
foreach($this->rows as $i => $row){
 $ids[]=(int)$row->product_id;
}
$db =& JFactory::getDBO();
$db->setQuery('SELECT category_id FROM '.hikashop::table('product_category').' WHERE product_id IN ('.implode(',',$ids).') AND category_id=24');
$result = $db->loadResult();
if(!empty($result)){
 //category is there
}else{
 //category not in the cart
}

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14608

Thanks!!

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14615

The code you sent works PERFECTLY! I am having a small issue with the Step number. Before the changes you sent me, the area of step.php I am changing looked like this:

if($this->step==3){
echo $this->cart->displayButton(JText::_('PLACE_ORDER_BUTTON'),'next',$this->params,hikashop::completeLink('checkout&task=step&step='.$this->step+1),'document.forms[\'hikashop_checkout_form\'].submit(); return false; ','id="hikashop_checkout_next_button"');
}else{
echo $this->cart->displayButton(JText::_('NEXT'),'next',$this->params,hikashop::completeLink('checkout&task=step&step='.$this->step+1),'document.forms[\'hikashop_checkout_form\'].submit(); return false;','id="hikashop_checkout_next_button"');
}
if($this->step!=0){
echo $this->cart->displayButton(JText::_('Back'),'back',$this->params,hikashop::currentUrl(),'history.back();return false;','id="hikashop_checkout_next_button"');
}

I had it like that so that it would display a BACK button on all steps except the first (#0), and the NEXT button would be replaced with PLACE ORDER in the last step (#3).

I changed this section of code to:
if($this->step=0){

$ids=array(); 
foreach($this->rows as $i => $row){ 
 $ids[]=(int)$row->product_id; 
} 
$db =& JFactory::getDBO(); 
$db->setQuery('SELECT category_id FROM '.hikashop::table('product_category').' WHERE product_id IN ('.implode(',',$ids).') AND category_id=24'); 
$result = $db->loadResult(); 
		if(!empty($result)){ 
 			if($this->step==3){
			echo $this->cart->displayButton(JText::_('PLACE_ORDER_BUTTON'),'next',$this->params,hikashop::completeLink('checkout&task=step&step='.$this->step+1),'document.forms[\'hikashop_checkout_form\'].submit(); return false; ','id="hikashop_checkout_next_button"');
			}else{
			echo $this->cart->displayButton(JText::_('NEXT'),'next',$this->params,hikashop::completeLink('checkout&task=step&step='.$this->step+1),'document.forms[\'hikashop_checkout_form\'].submit(); return false;','id="hikashop_checkout_next_button"');
			}
			if($this->step!=1){
			echo $this->cart->displayButton(JText::_('Back'),'back',$this->params,hikashop::currentUrl(),'history.back();return false;','id="hikashop_checkout_next_button"');
			}
		}else{ 
 			echo '<div style="float: right; text-align: right; color: #ff0000; font-weight: bold;">'.JText::_('REQD_CAT_MISSING','Cart must include a Paper Stock').'</div>';
		}
}

I have the first IF looking to see if the checkout step is the first (#0). However, nothing displays at the bottom of the page in this case. Not the NEXT button or the text I want to display. If I change the first IF to:
if($this->step=1){

then it displays correctly on the first step in the checkout process, and works absolutely correctly. However, clicking NEXT causes an error on the nest step, because it is evaluating the IF statement again as step #1. It's like I somehow messed with the step numbers. Any idea why?

Please Log in or Create an account to join the conversation.

  • Posts: 82866
  • Thank you received: 13373
  • MODERATOR
13 years 7 months ago #14618

$this->step=1 is always true. You wanted to write $this->step==1 or $this->step!=1

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14619

Darn it - I saw that as I read through my post! I just couldn't get back on here quick enough!!! Sorry, and thanks!

Please Log in or Create an account to join the conversation.

  • Posts: 202
  • Thank you received: 2
13 years 7 months ago #14620

Done, and working GREAT!!

Please Log in or Create an account to join the conversation.

Time to create page: 0.075 seconds
Powered by Kunena Forum