I want to update VM2 prices based on weight.
The thing is we have field product_weight
in table #__virtuemart_products
and field product_prices
in table #__virtuemart_product_prices
, and I want to update only the ones who have the 'tax_id'=4
in those.
I got my $priceg
(that is a price / per gram of gold/silver) that I got from pulling prices of precious metals with a parser.
And the query as follows:
if($priceg > 0 )
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('a.product_price', 'a.product_tax_id', 'b.virtuemart_product_id', 'b.product_weight')))
->from($db->quoteName('#__virtuemart_product_prices', 'a'))
->join('INNER', $db->quoteName('#__virtuemart_products', 'b') . ' ON (' . $db->quoteName('a.virtuemart_product_id') . ' = ' . $db->quoteName('b.virtuemart_product_id') . ')')
->where($db->quoteName('a.product_tax_id') . ' = 4')
->order($db->quoteName('a.virtuemart_product_id') . ' DESC');
$db->setQuery($query);
// Load the results as a list of objects/array or something(not sure which one fits better).
$results = $db->loadResultArray();
$updquery = $db->getQuery(true);
foreach($results as $newresults){ //i'm trying to update each price with mine
$weight = $results->product_weight; //here lies my problem, its ok for 1 record, doesnt go thru all
$pricenew = "$priceg * $weight";
$nuquery = ("UPDATE `#__virtuemart_product_prices` SET `product_price`=".$pricenew);
$db->setQuery($nuquery);
$updresult = $db->query();
}
}
}
Any help and directions much appreciated!
EDIT: preformatted var_dump($results) gives this:
array(260) {
[0]=>
array(4) {
[0]=>
string(8) "31.16577"
[1]=>
string(1) "4"
[2]=>
string(3) "286"
[3]=>
string(6) "7.3200"
}
.
. //i have 259 products with ID=4
.
[259]=>
array(4) {
[0]=>
string(8) "31.16577"
[1]=>
string(1) "4"
[2]=>
string(2) "17"
[3]=>
string(8) "100.0000"
}
}
As for now there are no errors, just doesnt go thru all $results and takes just the last one $newresult. The code being(updated):
if($price > 0 )
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('a.product_price', 'a.product_tax_id', 'b.virtuemart_product_id', 'b.product_weight')))
->from($db->quoteName('#__virtuemart_product_prices', 'a'))
->join('INNER', $db->quoteName('#__virtuemart_products', 'b') . ' ON (' . $db->quoteName('a.virtuemart_product_id') . ' = ' . $db->quoteName('b.virtuemart_product_id') . ')')
->where($db->quoteName('a.product_tax_id') . ' = 4')
->order($db->quoteName('a.virtuemart_product_id') . ' DESC');
$db->setQuery($query);
// Load the results as a list of objects/array or something(not sure which one fits better).
$results = $db->loadRowList();
$updquery = $db->getQuery(true);
foreach($results as $newresults){
$weight = $newresults['3'];
$pricenew = "$price * $weight";
$nuquery = $db->getQuery(true)
->update($db->quoteName('#__virtuemart_product_prices'))
->set($db->quoteName('product_price') . ' = ' . $db->quote($pricenew))
->where($db->quoteName('virtuemart_product_id') . ' = ' . $db->quote('virtuemart_product_id'));
$db->setQuery($nuquery);
$updresult = $db->query();
}
}
thx again for quick responses!