Author Topic: Displaying lowest price based off quantity in category view  (Read 416 times)

creatifpresence

  • Beginner
  • *
  • Posts: 13
  • A beginner
Displaying lowest price based off quantity in category view
« 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

creatifpresence

  • Beginner
  • *
  • Posts: 13
  • A beginner
Re: Displaying lowest price based off quantity in product and category views
« Reply #1 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

Code: [Select]
<?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>


PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10275
  • VirtueMart Version: 3+
Re: Displaying lowest price based off quantity in product and category views
« Reply #2 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'];



I do NOT do development work for hire.

creatifpresence

  • Beginner
  • *
  • Posts: 13
  • A beginner
Re: Displaying lowest price based off quantity in category views
« Reply #3 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 :)

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10275
  • VirtueMart Version: 3+
Re: Displaying lowest price based off quantity in category view
« Reply #4 on: January 15, 2017, 15:35:39 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.
I do NOT do development work for hire.

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 8878
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Displaying lowest price based off quantity in category view
« Reply #5 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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/