Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of rhandalthor
rhandalthor🇷🇺

MySQL / PHP problem Joomla's Virtuemart
I have been having problems saving price for products using Virtuemart in Joomla! . Somehow the price keeps on disappearing on the inititial apply changes or on save. I think it is a MySQL syntaxis issue wrongly created using PHP, but I have not been able to figure it out yet. One error I get every now and then is:
Warning: number_format() expects parameter 1 to be double, string given in /opt/local/apache2/htdocs/joomla/administrator/components/com_virtuemart/classes/currency/class_currency_display.php  on line 116

That line in class_currency_display.php has
$res=number_format($nb,$decimals,$this->decimal,$this->thousands);

Why does this error show up?

For everything related to this price issue see also http://forum.virtuemart.net/index.php?topic=58471.0 where I have added a lot of data trying to work it out. I have added the whole function as well as a code snippet.


function getValue($nb, $decimals=''){
		$res = "";
        $this->thousands = '';
		// Warning ! number_format function performs implicit rounding
		// Rounding is not handled in this DISPLAY class
		// that's why you have to use the right decimal value.
		// Workaround :number_format accepts either 1, 2 or 4 parameters.
		// this cause problem when no thousands separator is given : in this
		// case, an unwanted ',' is displayed.
		// That's why we have to do the work ourserlve.
		// Note : when no decimal il given (i.e. 3 parameters), everything works fine
		if( $decimals === '') {
			$decimals = $this->nbDecimal;
		}
		if ($this->thousands != ''){
			$res=number_format($nb,$decimals,$this->decimal,$this->thousands);
		} else {
			// If decimal is equal to defaut thousand separator, apply a trick
			if ($this->decimal==','){
				$res=number_format($nb,$decimals,$this->decimal,'|');
				$res=str_replace('|','',$res);			
			} else {
				// Else a simple substitution is enough
				$res=number_format($nb,$decimals,$this->decimal,$this->thousands);
				$res=str_replace(',','',$res);
			}
		}
		return($res);
	}

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Member_5340450Member_5340450

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of rhandalthorrhandalthor🇷🇺

ASKER

That could be the case. But I wonder why. Please explain.
And it still does not explain the price disappearing - see Virtuemart thread -  which probably is a secondary issue which is not connected as PHP errors do not show up after initial price insertion when the price is stored, but not displayed. I would love to solve that issue as well. the correct price for a e-commerce site is of the essence of course.

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of rhandalthorrhandalthor🇷🇺

ASKER

Well I will see if I can do that easily using MacPorts. Never downgraded before. But the still the other "price disappearing" issue remains? Anybody any ideas based upon the earlier posted thread?

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of rhandalthorrhandalthor🇷🇺

ASKER

@ Joomla_php Working on a downgrade. Will be a bit tough as all PHP 5.2.x versions have been removed.
The live site has PHP 5.2.0-8+etch11 and still has the same issue with the price disappearing when creating a new Virtuemart product as the local server using PHP 5.3 so it must be caused by another issue (PHP or MYSQL).

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of rhandalthorrhandalthor🇷🇺

ASKER

Doing research- see thread on Virtuemart forum -  I found the file that deals with the product price. Added it as code. Somehow a shopper_group_id= different from the shopper_group_id used to add the product is asjed for to display the price stored. Well at least I am 75% certain of that now....
<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id: ps_product_price.php 1660 2009-02-22 17:05:02Z tkahl $
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
 * This class handles product prices
 *
 */
class vm_ps_product_price extends vmAbstractObject  {
	var $key = 'product_price_id'; 
	var $_table_name = '#__{vm}_product_price';

	/**
	 * Validates the Input Parameters on price add/update
	 *
	 * @param array $d
	 * @return boolean
	 */
	function validate(&$d) {
		global $vmLogger, $VM_LANG;
		$valid = true;
		
		if (!isset($d["product_price"]) || $d["product_price"] === '') {
			$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_MISSING',false) );
			$valid = false;
		}
		if (empty($d["product_id"])) {
			$vmLogger->err(  $VM_LANG->_('VM_PRODUCT_ID_MISSING',false) );
			$valid = false;
		}
		// convert all "," in prices to decimal points.
		if (stristr($d["product_price"],",")) {
			$d['product_price'] = floatval(str_replace(',', '.', $d["product_price"]));
		}

		if (!$d["product_currency"]) {
			$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_CURRENCY_MISSING',false) );
			$valid = false;
		}
		$d["price_quantity_start"] = intval(@$d["price_quantity_start"]);
		$d["price_quantity_end"] = intval(@$d["price_quantity_end"]);

		if ($d["price_quantity_end"] < $d["price_quantity_start"]) {
			$vmLogger->err(  $VM_LANG->_('VM_PRODUCT_PRICE_QEND_LESS',false) );
			$valid = false;
		}

		$db = new ps_DB;
		$q = "SELECT count(*) AS num_rows FROM #__{vm}_product_price WHERE";
		if (!empty($d["product_price_id"])) {
			$q .= " product_price_id != '".$d['product_price_id']."' AND";
		}
		$q .= " shopper_group_id = '".$d["shopper_group_id"]."'";
		$q .= " AND product_id = '".$d['product_id']."'";
		$q .= " AND product_currency = '".$d['product_currency']."'";
		$q .= " AND (('".$d['price_quantity_start']."' >= price_quantity_start AND '".$d['price_quantity_start']."' <= price_quantity_end)";
		$q .= " OR ('".$d['price_quantity_end']."' >= price_quantity_start AND '".$d['price_quantity_end']."' <= price_quantity_end))";
		$db->query( $q ); $db->next_record();

		if ($db->f("num_rows") > 0) {
			$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_ALREADY',false) );
			$valid = false;
		}
		return $valid;
	}
	/**
	 * Adds a new price record for a given product
	 *
	 * @param array $d
	 * @return boolean
	 */
	function add(&$d) {
		global $vmLogger, $VM_LANG;
		if (!$this->validate($d)) {
			return false;
		}
		if( $d["product_price"] === '') {
			$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_NOTENTERED',false) );
			return false;
		}
		$timestamp = time();
		if (empty($d["product_price_vdate"])) $d["product_price_vdate"] = '';
		if (empty($d["product_price_edate"])) $d["product_price_edate"] = '';

		$fields = array('product_id' => $d["product_id"],
								'shopper_group_id' => vmRequest::getInt('shopper_group_id'),
								'product_price' => vmRequest::getFloat('product_price'),
								'product_currency' => vmGet($d, 'product_currency' ),
								'product_price_vdate' => vmGet($d, 'product_price_vdate'),
								'product_price_edate' => vmGet($d, 'product_price_edate'),
								'cdate' => $timestamp,
								'mdate' => $timestamp,
								'price_quantity_start' => vmRequest::getInt('price_quantity_start'),
								'price_quantity_end' =>vmRequest::getInt('price_quantity_end')
						);
		$db = new ps_DB;
		$db->buildQuery('INSERT', '#__{vm}_product_price', $fields );
		
		if( $db->query() !== false ) {		
			$_REQUEST['product_price_id'] = $db->last_insert_id();
			$vmLogger->info( $VM_LANG->_('VM_PRODUCT_PRICE_ADDED',false));
			return true;
		}
		$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_ADDING_FAILED',false) );
		return false;
	}

	/**
	 * Updates a product price
	 *
	 * @param array $d
	 * @return boolean
	 */
	function update(&$d) {
		global $vmLogger, $VM_LANG;
		if (!$this->validate($d)) {
			return false;
		}
		if( $d["product_price"] === '') {
			return $this->delete( $d );
		}
		$timestamp = time();

		$db = new ps_DB;
		if (empty($d["product_price_vdate"])) $d["product_price_vdate"] = '';
		if (empty($d["product_price_edate"])) $d["product_price_edate"] = '';
		$fields = array(
								'shopper_group_id' => vmRequest::getInt('shopper_group_id'),
								'product_price' => vmRequest::getFloat('product_price'),
								'product_currency' => vmGet($d, 'product_currency' ),
								'product_price_vdate' => vmGet($d, 'product_price_vdate'),
								'product_price_edate' => vmGet($d, 'product_price_edate'),
								'mdate' => $timestamp,
								'price_quantity_start' => vmRequest::getInt('price_quantity_start'),
								'price_quantity_end' =>vmRequest::getInt('price_quantity_end')
						);
		$db = new ps_DB;
		$db->buildQuery('UPDATE', '#__{vm}_product_price', $fields, 'WHERE product_price_id=' .(int)$d["product_price_id"] );
		
		if( $db->query() !== false ) {
			$vmLogger->info( $VM_LANG->_('VM_PRODUCT_PRICE_UPDATED',false) );
			return true;
		}
		$vmLogger->err( $VM_LANG->_('VM_PRODUCT_PRICE_UPDATING_FAILED',false) );
		return false;
	}


	/**
	* Controller for Deleting Records.
	*/
	function delete(&$d) {

		$record_id = $d["product_price_id"];

		if( is_array( $record_id)) {
			foreach( $record_id as $record) {
				if( !$this->delete_record( $record, $d ))
				return false;
			}
			return true;
		}
		else {
			return $this->delete_record( $record_id, $d );
		}
	}
	/**
	* Deletes one Record.
	*/
	function delete_record( $record_id, &$d ) {
		global $db, $vmLogger, $VM_LANG;
		$q  = "DELETE FROM #__{vm}_product_price ";
		$q .= "WHERE product_price_id =".intval($record_id).' LIMIT 1';
		$db->query($q);
		$vmLogger->info( $VM_LANG->_('VM_PRODUCT_PRICE_DELETED',false) );
		return True;
	}


}

// Check if there is an extended class in the Themes and if it is allowed to use them
// If the class is called outside Virtuemart, we have to make sure to load the settings
// Thomas Kahl - Feb. 2009
if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
	include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
	// Load the theme-user_class as extended
	include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
	// Otherwise we have to use the original classname to extend the core-class
	class ps_product_price extends vm_ps_product_price {}
}

?>

Open in new window


Avatar of rhandalthorrhandalthor🇷🇺

ASKER

Will close this thread as it has died and I found a solution elsewhere. I removed the duplicate default shopper and made sure there is only one vendor as VM can only handle one et voila.

Avatar of rhandalthorrhandalthor🇷🇺

ASKER

No real appropriate solution given. Only tips on a PHP downgrade which wasn't needed. Thread sort of withered away even though I added more details. Found solution myself in the end.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.

PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.