News:

Support the VirtueMart project and become a member

Main Menu

Show retail price to wholesale shopper group [solved]

Started by Marttyn, August 19, 2013, 03:55:30 AM

Previous topic - Next topic

Marttyn

I have a default price for every client on the store, and a wholesale price (adding a second price to every product, and assigning a shopper group).
Ive wanted the wholesalers to see the original price, and the wholesale price. But as im not applying any "discount", just showing a different price, there is no way to show both prices...

Ive been trying to do this whole weekend, and after 2 days of research and try and error ive found a solution!
Its needed a DB query for finding out what shopper group is the current user, and other query for finding out the desired price.
This is how its shown in my front shop:


The blue crossed price is the retail price, and the bigger one is the wholesale price.

First we need to find out what shopper group is current user, so i use this code to get the shopper group ID. Be sure to place it in your desired VIEW. If your view is "virtuemart" or "category" that has to create a page with multiple products, the you should put this code BEFORE the "foreach" so its called only once.
//get user ID
$usuario = JFactory::getUser();
$id_usuario = $usuario->id;

//get shopper group
$datos = JFactory::getDbo();
$query = 'SELECT `virtuemart_shoppergroup_id` FROM `#__virtuemart_vmuser_shoppergroups` WHERE `virtuemart_user_id` = ' . $id_usuario;
$datos->setQuery($query);
$grupo_comprador = $datos->loadResult();


This code first find out the user ID, if the user is not registered will return an empty value
Then we go to the "shoppergroups" table of DB, and find out what shopper group is assigned to this ID.
This way we have the shopper group ID.



Now we will find out the price.
//show crossed price
if ($grupo_comprador == 3){ //check if wholesaler
//get default price
$query = 'SELECT `product_price` FROM `#__virtuemart_product_prices` WHERE `virtuemart_shoppergroup_id` = 0 AND `virtuemart_product_id` = "'.$product->virtuemart_product_id.'" ';
$datos->setQuery($query);
$precio_default = $datos->loadResult();

//get VatTax
$total_vat_tax = 0;
foreach ($product->prices['VatTax'] as $vat_tax) {
$total_vat_tax = $total_vat_tax + $vat_tax[1];
}
$total_vat_tax = 1 + ($total_vat_tax / 100);
$precio_default = $precio_default * $total_vat_tax; //add vat tax to default price

echo '<div class="price-crossed">' . $this->currency->createPriceDiv( 'basePriceWithTax', 'COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX', $precio_default ) . "</div>";
}
else{
if ($product->prices['basePriceWithTax'] != $product->prices['salesPrice']) {
echo '<div class="price-crossed">' . $this->currency->createPriceDiv( 'basePriceWithTax', 'COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX', $product->prices ) . "</div>";
}
}

//show sales price
echo $this->currency->createPriceDiv( 'salesPrice', 'COM_VIRTUEMART_PRODUCT_SALESPRICE', $product->prices );


What we are doing here is:
Check if the user is wholesaler. In my case wholesaler shopper group ID is 3. Change to the one you have.

-IF THE USER IS WHOLESALER
Find the price for everyone ("0"): query to DB, find in product price table the price that has "0" as shopper group ID (price for everyone) , AND that match with product ID we are looking for.
We will get the price, without tax, variant or discount. In my case I need price with tax included so i get all VatTax rules and apply to the default price.
As i dont need other tax or discount to be show, but if you need just do the same with Tax or Discount as you need.
Then i create the crossed <div> for the price.

-IF NOT WHOLESALER
if base price with tax is different than sales price, then show the crossed <div> with the base price. otherwise, dont show. (this is for showing discounts, for every other user that is not wholesaler).

Bellow show the sales price for the current user.



If you need to get some info from the actual product prices you can use this in your PHP and will output an array information with the contained values:
echo "<PRE>";
print_r($product->prices);
echo "</PRE>";


to get all VatTax rules applied to the product use:
echo "<PRE>";
print_r($product->prices['VatTax']);
echo "</PRE>";


There are arrays inside arrays, so go inside them to get the values needed.

I attached the file i created for my virtuemart override view (default_products.php). Between lines 4 and 12 is the first modification made, and between lines 114 and 139 is the second modification.

Please use as GUIDE or REFFERENCE, but dont apply directly, as other modifications are made to this file that may not suit your needs.

Regards!

[attachment cleanup by admin]