Conditional Delete From Cart

  • Posts: 25
  • Thank you received: 0
12 years 8 months ago #70835

Good Day,

Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?

Thank You

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

  • Posts: 13201
  • Thank you received: 2322
12 years 8 months ago #70872

Hi,

There is actually no way to do this. But if you edit the "product / show_default" view and add an action in a onClick on the add to cart button, and this action is to reset the cart_id in session, then I think that the product will be added in a new cart, so no other products inside.

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

  • Posts: 25
  • Thank you received: 0
12 years 8 months ago #70873

Hi again,

I am actually quite new to HikaShop and I have rather limited knowledge of php and joomla!.

I am willing to try your suggested method if you can tell me how to do it step by step.

thank you in advance.

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

  • Posts: 13201
  • Thank you received: 2322
12 years 8 months ago #70934

If you want to always clear the cart when you add a new product, then I think that it can be done in the file: "yourSite\component\com_hikashop\controllers\product.php".

And in the function "updatecart()" add the line:

$app->getUserState(HIKASHOP_COMPONENT.'.'.$cart_type_id,0);

under the line:
$cart_type_id = $cart_type.'_id';

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

  • Posts: 25
  • Thank you received: 0
12 years 8 months ago #70993

hi again,

I tried your suggestion but unfortunately it does not work.

Do you have any other suggestions?

Thank You again

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

  • Posts: 13201
  • Thank you received: 2322
12 years 8 months ago #71378

Hi,

Sorry, it's not "getUsetState" but "setUserState"

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

  • Posts: 25
  • Thank you received: 0
12 years 8 months ago #71691

hi again,

Thank you for replying again, however your suggestion did not work.
I do not understand the code fully but I think you are close to a solution.
I know the logic should read as follows:
when 'add to cart' is selected, if there are any items in the cart clean the cart, and add the new item.

Do you have any other suggestions.

Thank You again

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

  • Posts: 13201
  • Thank you received: 2322
12 years 8 months ago #71787

Hi,

I tried the solution on my end, and it's working fine.
Can you check in the file "administrator/components/com_hikashop/classes/cart.php" in the function "loadCart()" if the following line is commented.

if($app->getUserState(HIKASHOP_COMPONENT.'.'.$this->cart_type.'_new', '0') == '0'){

If it's commented, uncomment it (don't forget to uncomment the "}" closing the if condition).

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

  • Posts: 25
  • Thank you received: 0
12 years 8 months ago #71819

Hi again

I went to "mysite/administrator/components/com_hikashop/classes/cart.php" and I do not see the line:-

if($app->getUserState(HIKASHOP_COMPONENT.'.'.$this->cart_type.'_new', '0') == '0'){

The closest thing I see to this line is:-

if($app->getUserState(HIKASHOP_COMPONENT.'.'.$cart_type.'_id','0') == $cart_id){

I then looked for the "loadCart()" function and it does NOT exist in my 'cart.php' file.

Do you think it would help if you upload your product.php and cart.php files so I can try them?

Also, I realise there are other cart.php files in the 'com_hikashop\controllers', '\com_hikashop\views\product\tmpl' and 'com_hikashop\views\checkout\tmpl' directories. Do these also need to be modified?

Thank You again.

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

  • Posts: 13201
  • Thank you received: 2322
12 years 8 months ago #71830

Can you try to replace your function "loadCart()" by:

	function loadCart($cart_id=0,$reset=false){
		static $carts = array();
		if($reset){
			$carts = array();
			$this->cart_id = 0;
			$this->cart = null;
			return true;
		}

		$this->filters = array();
		$app = JFactory::getApplication();
		if(empty($cart_id) || $cart_id == 0){
			$this->cart_id = $app->getUserState( HIKASHOP_COMPONENT.'.'.$this->cart_type.'_id', 0, 'int' );
		} else {
			$this->cart_id=$cart_id;
		}
		if(!isset($this->cart) || is_null($this->cart)) $this->cart = new stdClass();
		$this->cart->cart_id = $this->cart_id;

		if(!empty($this->cart_id)){
			$this->filters[]='a.cart_id = '.(int)$this->cart_id;
		}else{
			$user = JFactory::getUser();
			if(!empty($user->id)){
				$this->filters[]='a.user_id = '.(int)$user->id;
			}
			$session = JFactory::getSession();
			if($session->getId()){
				$this->filters[]='a.session_id = '.$this->database->Quote($session->getId());
			}
		}
		$filter = "(".implode(' OR ',$this->filters).")";

		if($this->cart_type){
			$typeFilter='a.cart_type = '.$this->database->Quote($this->cart_type);
			$filter .= " AND ".$typeFilter;
		}
		if(!empty($carts[$filter])){
			$this->cart =& $carts[$filter];
		}else{
			// if new cart/wishlist, don't search for an existing entry
			if($app->getUserState(HIKASHOP_COMPONENT.'.'.$this->cart_type.'_new', '0') == '0'){
				//search for an existing cart if no cart entries were found
				if(!empty($filter)){
					$query='SELECT a.* FROM '.hikashop_table('cart').' AS a WHERE '.$filter.' ORDER BY a.cart_modified DESC LIMIT 1';
					$this->database->setQuery($query);
					$this->cart = $this->database->loadObject();
				}else{
					$this->cart = null;
				}
				$carts[$filter] =& $this->cart;
			}
		}
	}

If it still doesn't work, thanks to wait for the next release of HikaShop which will be there before the end of the month.

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

Time to create page: 0.084 seconds
Powered by Kunena Forum