Count items in shopping cart from a custom module?

  • Posts: 29
  • Thank you received: 1
8 years 6 months ago #239492

Hi,

Searched for this a lot and couldn't find anything...

I'm developing a custom module for embedding in my H.S. pages.

How can I obtain within that module the count of items in my shopping cart?
(PHP code that can be embedded in the module html)

Thanks.

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

  • Posts: 13201
  • Thank you received: 2322
8 years 6 months ago #239515

Hi,

To get the content of the cart, you can use that kind of code:

$cartClass = hikashop_get('class.cart');
$fullCart = $cartClass->loadFullCart();
$nbProducts = 0;
foreach($fullCart->products as $product){
    $nbProducts += $product->cart_product_quantity;
}

The following user(s) said Thank You: bshomer

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

  • Posts: 29
  • Thank you received: 1
8 years 6 months ago #239575

Cool!
Thanks very much Xavier.
I now have a cool icon for empty and full shopping bag with the number of selected items on top.



It uses the modals extension from Regular Labs to display a modal window of the shopping cart.
I still have two issues.

Here's the PHP code of the module:
<?php

$cartClass = hikashop_get('class.cart'); 
$fullCart = $cartClass->loadFullCart(); 
$nbProducts = 0; 

foreach($fullCart->products as $product){ 
 $nbProducts += $product->cart_product_quantity; 
}

if ($nbProducts == 0){
	echo "{modal /index.php?options=com_content&view=article&id=2}" .
	     "<div class='emptyCartIcon'></div>{/modal}";
	}
else {
	echo "{modal /index.php?options=com_content&view=article&id=2}" .
	     "<div class='fullCartIcon'><h6>" .
	     $nbProducts .
	     "</h6></div>{/modal}";	
	}
?>

Here's the css (should anyone wish to implement it)
.emptyCartIcon{
	margin-right: 2em;
	margin-left: 2em;
	width:40px;
	height:50px;
	background: url(/images/shoppingBagEmpty.png);
}

.fullCartIcon{
	margin-right: 2em;
	margin-left: 2em;
	position: relative;
	width:40px;
	height:50px;
	background: url(/images/shoppingBagFull.png);
}

.fullCartIcon h6{
	display: inline-block;
	position: absolute;
	bottom: 1px;
	right: 1px;
	background-color: purple;
	color: white;
	font-size: 0.7em;
	width: 0.7em;
	font-weight: bold;
	text-align: center;
    -moz-border-radius:0.25em;
    -webkit-border-radius:0.25em;
    border-radius:0.25em;	
}

My Issues:
1. The icon is updated only upon page reload. This doesn't always happen.
Is there some sort of a listener I can hook to (Ajax or other), so the icon will receive an update when the cart content is changed?

2. When loading "regular" Joomla pages, not related to Hikashop, the code freezes everything and the page will not display.
I guess it is a scope issue. When a H.S. page loads, PHP gets the scope of H.S. With regular pages this doesn't happen.
Any advice on how to overcome this???

By the way, I think such an icon at the top of the shop's page should be an integral part of Hikashop.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 6 months ago #239579

Hi,

1. We don't have yet a event system for the cart system (we're working on one for HikaShop 3.0). So for now, you would have to edit the file "quantity" of the view "product" via the menu Display>Views and add a call to a javascript function of yours for the onclick of the add to cart button. You can add it to the $this->ajax variable.

2. You need to include the main HikaShop helper before your PHP code:

		if(!defined('DS'))
			define('DS', DIRECTORY_SEPARATOR);
		if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php'))
			return true;

The following user(s) said Thank You: bshomer

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

  • Posts: 29
  • Thank you received: 1
8 years 6 months ago #239748

Thanks!!!
The HikaShop helper code did the job alright! Works great!

I didn't quite understand where exactly can I place my Javascript call. Where is the onclick call of Add To Cart in quantity.php?

I think I will simply invoke a document reload with the onclick. (Any better idea?)
I would really appreciate a more detailed explanation as I'm really new to Hikashop.

THANKS!

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 6 months ago #239753

Hi,

After the line:

$this->ajax = 'return hikashopModifyQuantity(\''.$this->row->product_id.'\',field,1,0,\'cart\','.$module_id.')';
You can add such code:
$this->ajax = 'myjavascriptfunction();'.$this->ajax;
in the product/quantity.php view file.
The $this->ajax variable is added automatically to the onclick of the add to cart button.

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

Time to create page: 0.076 seconds
Powered by Kunena Forum