Joomla Virtuemart – Disable The Session Cache on get_discount Hack


Several of our client complaint that Virtuemart are not displaying the discount price instantly, that cause them the problem, when they speak to there customer. People are not seeing the pricing correctly. We know that this is not a bug in virutemart but rather a extra add-on that the development team had added. But what if the client joomla’s session lifetime is setup to be 1500 minutes, then the price will never be updated. Trusted me, this happen a lot. So we have disable the session cache on the get_discount on virtuemart.

Here is the hack: Use at your own risk

From Client: “I see that if I change the Discounted Price for a product, then the price does not update on the frontend until I delete the cookies for the site. It is the same behaviour regardless if I change the discounted price using the “Discount Type” select field or just enter a value into the “Discounted Price” field. how would they see the content of their shopping basket etc….”

So what can we do to eliminate this issue for the future. First we are going to open up the files ps_product.php located under administrator/components/com_virtuemart/classes/ps_product.php, and do a search for function get_discount( $product_id ) {

Original Code

function get_discount( $product_id ) {
	global $mosConfig_lifetime;

	// We use the Session now to store the discount info for
	// each product. But this info can change regularly,
	// so we check if the session time has expired
	if( empty( $_SESSION['product_sess'][$product_id]['discount_info'] )
	|| (time() - $_SESSION['product_sess'][$product_id]['discount_info']['create_time'] )>$mosConfig_lifetime) {
		$db = new ps_DB;
		$starttime = time();
		$year = date('Y');
		$month = date('n');
		$day = date('j');
		// get the beginning time of today
		$endofday = mktime(0, 0, 0, $month, $day, $year) - 1440;

		// Get the DISCOUNT AMOUNT
		$q = "SELECT amount,is_percent FROM #__{vm}_product,#__{vm}_product_discount ";
		$q .= "WHERE product_id='$product_id' AND (start_date<='$starttime' OR start_date=0) AND (end_date>='$endofday' OR end_date=0) ";
		$q .= "AND product_discount_id=discount_id";
		$db->query( $q );
		if( $db->next_record() ) {
			$discount_info["amount"] = $db->f("amount");
			$discount_info["is_percent"] = $db->f("is_percent");
			$no_discount = false;
		}
		else {
			$discount_info["amount"] = 0;
			$discount_info["is_percent"] = 0;
			$no_discount = true;
		}
		if ($no_discount) {
			$q = "SELECT product_parent_id FROM #__{vm}_product WHERE product_id=$product_id";
			$db->query($q);
			if($db->next_record()) {
				$q = "SELECT amount,is_percent FROM #__{vm}_product,#__{vm}_product_discount ";
				$q .= "WHERE product_id='".$db->f("product_parent_id")."' AND (start_date<='$starttime' OR start_date=0) AND (end_date>='$endofday' OR end_date=0) ";
				$q .= "AND product_discount_id=discount_id";
				$db->query( $q );
				if( $db->next_record() ) {
					$discount_info["amount"] = $db->f("amount");
					$discount_info["is_percent"] = $db->f("is_percent");
				}
			}
		}                
		$discount_info['create_time'] = time();
		$_SESSION['product_sess'][$product_id]['discount_info'] = $discount_info;
		return $discount_info;
	}
	else
		return $_SESSION['product_sess'][$product_id]['discount_info'];
}

Replace With

function get_discount( $product_id ) {
	global $mosConfig_lifetime;

	// We use the Session now to store the discount info for
	// each product. But this info can change regularly,
	// so we check if the session time has expired
	// Hacked by DesignerSandbox
	// Remark this action, so it will run strict
	// if( empty( $_SESSION['product_sess'][$product_id]['discount_info'] )
	// || (time() - $_SESSION['product_sess'][$product_id]['discount_info']['create_time'] )>$mosConfig_lifetime) {
		$db = new ps_DB;
		$starttime = time();
		$year = date('Y');
		$month = date('n');
		$day = date('j');
		// get the beginning time of today
		$endofday = mktime(0, 0, 0, $month, $day, $year) - 1440;

		// Get the DISCOUNT AMOUNT
		$q = "SELECT amount,is_percent FROM #__{vm}_product,#__{vm}_product_discount ";
		$q .= "WHERE product_id='$product_id' AND (start_date<='$starttime' OR start_date=0) AND (end_date>='$endofday' OR end_date=0) ";
		$q .= "AND product_discount_id=discount_id";
		$db->query( $q );
		if( $db->next_record() ) {
			$discount_info["amount"] = $db->f("amount");
			$discount_info["is_percent"] = $db->f("is_percent");
			$no_discount = false;
		}
		else {
			$discount_info["amount"] = 0;
			$discount_info["is_percent"] = 0;
			$no_discount = true;
		}
		if ($no_discount) {
			$q = "SELECT product_parent_id FROM #__{vm}_product WHERE product_id=$product_id";
			$db->query($q);
			if($db->next_record()) {
				$q = "SELECT amount,is_percent FROM #__{vm}_product,#__{vm}_product_discount ";
				$q .= "WHERE product_id='".$db->f("product_parent_id")."' AND (start_date<='$starttime' OR start_date=0) AND (end_date>='$endofday' OR end_date=0) ";
				$q .= "AND product_discount_id=discount_id";
				$db->query( $q );
				if( $db->next_record() ) {
					$discount_info["amount"] = $db->f("amount");
					$discount_info["is_percent"] = $db->f("is_percent");
				}
			}
		}                
		$discount_info['create_time'] = time();
		$_SESSION['product_sess'][$product_id]['discount_info'] = $discount_info;
		return $discount_info;
	// }
	// else
	// 	return $_SESSION['product_sess'][$product_id]['discount_info'];
}

Be a link sponsor for this page now

[sponsorbox]


Related Posts Plugin for WordPress, Blogger...

I hope you have enjoyed this post, be sure to subscribe to my rss feed by click the subscribe button at the top.

Good Luck!

Be Sure to Subscribe to our feed or follow us on twitter to get the latest updates/patches
End of Article, Thanks for reading.
You can leave a response, or trackback from your own site.

8 Responses to “Joomla Virtuemart – Disable The Session Cache on get_discount Hack”

  1. Les Macbride says:

    Hi – just a quick note to say thank you for this article. Very great.

  2. Lee says:

    Thank you so much. I had been searching for the cause of this problem for weeks now.

  3. Shingi says:

    THANK YOU SOOOOOOOOOOOO MUCH !!!!

  4. Francesco says:

    Thanks so much from Italy! ;)

    I would ask you another question and sorry for the OT, but you are expert and maybe you can help me :(
    Do you know how to set a discount only for a user group, without discount to others user group? (Discount showed with old price with red line-through!)

  5. TurnTex says:

    I just found this hack while searching for a solution as to why my discounts would not show up. I basically gave up on it a year ago and just today decided to try to figure it out. Your hack worked MARVELOUSLY and was the solution to my problem. I now have functioning discounts! THANK YOU SO MUCH!!

  6. Michel says:

    I'm having the same problem of discounts not being visible in the frontend. I have tried the hack above but unfortunately no results. Any suggestions on how to fix this issue in VM 1.1.9 (Joomla 1.5.25)?

Leave a Reply