VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product pricing => Topic started by: creatifpresence on January 11, 2017, 16:29:14 PM

Title: Displaying lowest price based off quantity in category view
Post by: creatifpresence on January 11, 2017, 16:29:14 PM
Hello,

I've been looking for a solution to displaying lowest price based off quantity.

Edit: Found the solution and will post for others.

Virtuemart version 3.0.18
Joomla! 3.6.5
Title: Re: Displaying lowest price based off quantity in product and category views
Post by: creatifpresence on January 13, 2017, 00:31:08 AM
The attached solves this issue.

Hope this is useful to others searching how to display the lowest price not highest.

The following code is in essence the new your theme/html/com_virtuemart/sublayouts/prices.php

<?php
/**
 *
 * Show the product prices
 *
 * @package    VirtueMart
 * @subpackage
 * @author Max Milbers, Valerie Isaksen
 * @link http://www.virtuemart.net
 * @copyright Copyright (c) 2004 - 2014 VirtueMart Team. 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.
 * @version $Id: default_showprices.php 8024 2014-06-12 15:08:59Z Milbo $
 */
// Check to ensure this file is included in Joomla!
defined ('_JEXEC') or die('Restricted access');
$product $viewData['product'];
$currency $viewData['currency'];
?>


<?php
// ******* Get the lowest price from the database ********
$usermodel VmModel::getModel ('user');
$currentVMuser $usermodel->getCurrentUser ();
$currentVMuser->shopper_groups[] = '0'//This allows pricing for ALL Shopper groups to appear
$virtuemart_shoppergroup_ids $currentVMuser->shopper_groups;
$virtuemart_shoppergroup_id_str implode(',',$virtuemart_shoppergroup_ids);
$db JFactory::getDBO();
$sql "
SELECT
MIN(product_price)
FROM #__virtuemart_product_prices
WHERE
virtuemart_product_id = '"
.$product->virtuemart_product_id."'
AND
virtuemart_shoppergroup_id IN ("
.$virtuemart_shoppergroup_id_str.")
"
;
$db->setQuery($sql);
$result $db->loadRow();
// Override the salesPrice with the lowest price
$product->prices['salesPrice'] = $result[0];
// **********************************************************
?>


<div class="product-price" id="productPrice<?php echo $product->virtuemart_product_id ?>">
<?php
$isSale = (!empty($product->prices['discountAmount'])) ? 0;

if (!empty($product->prices['salesPrice'])) {
//echo '<div class="vm-cart-price">' . vmText::_ ('COM_VIRTUEMART_CART_PRICE') . '</div>';
}

if ($product->prices['salesPrice']<=and VmConfig::get ('askprice'1) and isset($product->images[0]) and !$product->images[0]->file_is_downloadable) {
$askquestion_url JRoute::_('index.php?option=com_virtuemart&view=productdetails&task=askquestion&virtuemart_product_id=' $product->virtuemart_product_id '&virtuemart_category_id=' $product->virtuemart_category_id '&tmpl=component'FALSE);
?>

<a class="ask-a-question bold" href="<?php echo $askquestion_url ?>" rel="nofollow" ><?php echo vmText::('COM_VIRTUEMART_PRODUCT_ASKPRICE'?></a>
<?php
} else {
//if ($showBasePrice) {
if ($isSale) {
echo '<div class="price-crossed" >'.$currency->createPriceDiv ('basePrice''COM_VIRTUEMART_PRODUCT_BASEPRICE'$product->prices) . "</div>";
}
//if (round($product->prices['basePrice'],$currency->_priceConfig['basePriceVariant'][1]) != $product->prices['basePriceVariant']) {
echo ''.$currency->createPriceDiv ('basePriceVariant''COM_VIRTUEMART_PRODUCT_BASEPRICE_VARIANT'$product->prices). "";
//}

//}
echo $currency->createPriceDiv ('variantModification''COM_VIRTUEMART_PRODUCT_VARIANT_MOD'$product->prices);
if (round($product->prices['basePriceWithTax'],$currency->_priceConfig['salesPrice'][1]) != $product->prices['salesPrice']) {
echo '' $currency->createPriceDiv ('basePriceWithTax''COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX'$product->prices) . "";
}
if (round($product->prices['salesPriceWithDiscount'],$currency->_priceConfig['salesPrice'][1]) != $product->prices['salesPrice']) {
echo $currency->createPriceDiv ('salesPriceWithDiscount''COM_VIRTUEMART_PRODUCT_SALESPRICE_WITH_DISCOUNT'$product->prices);
}
?>

<span class="from-price">From: </span>
<?php
echo 
$currency->createPriceDiv ('salesPrice''COM_VIRTUEMART_PRODUCT_SALESPRICE'$product->prices);
if ($product->prices['discountedPriceWithoutTax'] != $product->prices['priceWithoutTax']) {
echo $currency->createPriceDiv ('discountedPriceWithoutTax''COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX'$product->prices);
} else {
echo $currency->createPriceDiv ('priceWithoutTax''COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX'$product->prices);
}
echo $currency->createPriceDiv ('taxAmount''COM_VIRTUEMART_PRODUCT_TAX_AMOUNT'$product->prices);
$unitPriceDescription vmText::sprintf ('COM_VIRTUEMART_PRODUCT_UNITPRICE'vmText::_('COM_VIRTUEMART_UNIT_SYMBOL_'.$product->product_unit));
echo $currency->createPriceDiv ('unitPrice'$unitPriceDescription$product->prices);
}
?>

</div>

Title: Re: Displaying lowest price based off quantity in product and category views
Post by: PRO on January 14, 2017, 23:25:43 PM
I would do this differently, especially on the category page. Because you are running the same code over and over again for multiple products.


at the very least i would grab this data 1 time in
views/sublayouts/products.php


$usermodel = VmModel::getModel ('user');
$currentVMuser = $usermodel->getCurrentUser ();
$currentVMuser->shopper_groups[] = '0'; //This allows pricing for ALL Shopper groups to appear
$virtuemart_shoppergroup_ids = $currentVMuser->shopper_groups;
$virtuemart_shoppergroup_id_str = implode(',',$virtuemart_shoppergroup_ids);


then, throw the result into the
$viewData array

for example
echo shopFunctionsF::renderVmSubLayout('prices',array('product'=>$product,'currency'=>$currency,'userdata'=>$myresulthere));

I added this    ,'userdata'=>$myresulthere    to the echo in products.php calling the prices template

then in prices.php

if you notice at the top of the file
$product = $viewData['product'];
$currency = $viewData['currency'];

You just grab the info from the array you added
$userdata = $viewData['userdata'];



Title: Re: Displaying lowest price based off quantity in category views
Post by: creatifpresence on January 15, 2017, 14:26:58 PM
I paid for a pro to do this, so I don't know which method is better. Just thought I'd share for other people to use :)
Title: Re: Displaying lowest price based off quantity in category view
Post by: PRO on January 15, 2017, 15:35:39 PM
Quote from: creatifpresence on January 15, 2017, 14:26:58 PM
I paid for a pro to do this, so I don't know which method is better. Just thought I'd share for other people to use :)

Ok, no problem. I thought you coded it.
Title: Re: Displaying lowest price based off quantity in category view
Post by: Milbo on January 17, 2017, 15:23:12 PM
At least it is not that hard, because most of the functions are cached. like "getCurrentUser", but Pro wrote a nice explanation.

Should be added here https://docs.virtuemart.net/tutorials/templating-layouts/199-sublayouts.html and/or here https://docs.virtuemart.net/tutorials/development/224-php-basics-for-templater.html