News:

Looking for documentation? Take a look on our wiki

Main Menu

Applied discount rule doesn't give same result always

Started by vladast, January 04, 2017, 14:12:10 PM

Previous topic - Next topic

vladast

Hi, I found strange behavior with reading of product prices in VirtueMart. Images below are on different language than english and therefore I've added few markups for better followup.

What I wanted is to make a button that will invite users to login to get additional online discount of 10%.

Logic behind that is:
1. 10% discount will be applied for all logged in users. If user is not logged in, on non discounted products regular price will show up and button will invite to log in to get additional discount. Code // 2


   10% discount doesn't apply to already discounted products (products having price override).Code // 3

   This is handled by "dummy categories/control categories". "Promotion category" has been made and all products that are on discount are located in that control category as well as their regular category. Control category is hidden.
   10% discount rule is assigned to all other groups except "Promotion category"

2. When user is logged in, button text will change to "10% online discounted applied". Code // 4

   At the same time, code should recognize if this product is already discounted or not. If not then button text after login will be changed to "No online discount on this product" (picture 3)
   
Now we are getting to the actual problem. To be able to handle this button text switching, my code has to contain some calculations of the actual product prices.

Below you can find one version of my code. There are different ways of calculating this, using JUser form... I had 4-5 different versions/combinations with getting same result.

Problem that occurs is below code doesn't calculate or doesn't present correct things in some cases and therefore code is not able to show right button text. Strange thing is that this is hapening to some of the products within same category. All products in that category has same rules and inputs, different base price only. I've noticed that if base price without tax is specifically 140.00 or 1001.00, 4243.00, 4043.00, ctr. button text will show code from // 3 even if it should be // 4.
So problem is related to the base price without tax value (regular price that you are writing under product properties)

First I was thinking that problem is with my VM installation, but then I tried fresh new VM installation, and I've added same rule and only one product with one of the problematic prices and same result. For some reason there is some strange bug. If I just change price and put 145.00 or 1025.00... ctr code // 4 will apply and everything will work fine.
Not sure why this works in some products within same category and on some is not working even on fresh VM installation.

Do you guys have any thoughts on why this happening?

file that has been edited is override of html\com_virtuemart\productdetails\default.php

                    <?php
$negBasePriceXonlineDiscount = -($this->product->prices['basePriceWithTax'] * 0.1); //negative price x 0.1
$onlineDiscountAmount $this->product->prices['discountAmount']; // discount amount, usually reads/shows as negative
$BasePriceWithTaxXdevedeset $this->product->prices['basePriceWithTax'] * 0.9// sales price when 10% discount applied


// 1 if product doesn't have any price, nothing will show
if ($this->product->prices['salesPrice'] == false){
echo '';
}

// 2 if product is not discounted and no discount amount recognized, it will call to login to get 10% discount
elseif ($negBasePriceXonlineDiscount !== $onlineDiscountAmount && $this->product->prices['discountAmount'] >= 0){
echo '<a class="drop user tenpercent" />'; echo vmText::_('COM_VIRTUEMART_PRODUCT_DISCOUNT_LBL'); echo'</a>';
}

// 3 if product price with 10% discount included is different than sales price (that means there is price override), text will show that there is no 10% discount that can be applied to this already discounted product
elseif ($BasePriceWithTaxXdevedeset !== $this->product->prices['salesPrice']) {
echo '<a class="tenpercent" href="prodaja/faq#da-li-postoji-popust-za-online-porucivanje" />'; echo vmText::_('COM_VIRTUEMART_PRODUCT_NODISCOUNT_LBL'); echo'</a>';
}

// 4 if product price with 10% discount inluced is identical with the sales price (that means there is no product price override), text will show that 10% discount is applied
elseif ($BasePriceWithTaxXdevedeset === $this->product->prices['salesPrice']) {
echo '<a class="tenpercent"/>';echo vmText::_('COM_VIRTUEMART_PRODUCT_DISCOUNTIN_LBL');echo'</a>';
}
                    
?>



Joomla 3.6.5
VirtueMart 3.0.18
PHP 5.6.25
               

Studio 42

I think something is wrong on your if else and compared price.
Why the *0.1 and *0.9 to set price ?
And how you know that some products have 10% discount when user is not logged in ?

vladast

QuoteI think something is wrong on your if else and compared price.
Could be that my code is too complicated and there are few ways to make it more simple, but code is working. I know that because same code is working on many other products, but on some (few) is not working properly. Like I wrote, same rules and inputs are added between all products, only base price is different and code is not working only on certain BasePriceWithTax values.
QuoteWhy the *0.1 and *0.9 to set price ?
*0.1 and *0.9 are not setting the price. Shown price is price set in backend (base price, tax, rules). Those values are used only to compare and to use it as a rule to determine correct input so button text could vary.
QuoteAnd how you know that some products have 10% discount when user is not logged in ?
There is a rule set on all products, after user is logged in 10% discount will apply. If user is not logged in, part of the code // 4 will be used and therefore button will say "Please login to get 10% discount". This applies only on products that doesn't have price override already set in backend. Products that have price override, regardless if you are logged in or not, button will say "No additional discount available on this product".

If someone wants to get this tested try at your site:

  • set rule 10% discount on BasePriceWithTax on logged in users only
  • add Base price of a product 140.00 or 1001.00, 4243.00, 4043.00 (those values doesn't work in my case)
  • add shown code in override of html\com_virtuemart\productdetails\default.php
  • add text override COM_VIRTUEMART_PRODUCT_DISCOUNT_LBL = Please login to get 10% online discount
  • add text override COM_VIRTUEMART_PRODUCT_NODISCOUNT_LBL = No additional 10% discount on this product
  • add text override COM_VIRTUEMART_PRODUCT_DISCOUNTIN_LBL = 10% online discount applied
  • add this to your template CSS
    Quotea.tenpercent{
      background: #2367b2;
      color:#fff;
      font-size: 16px;
      cursor: pointer;
    }

    a.tenpercent:hover{
       background:#083C77;
    }
After these steps code should work. As I said, button text will switch if you are logged in or not, on products without price override. Products that contains BasePrice values mentioned before, will not work. Maybe there are few more values that will not work, but I 'haven't check whole database.

Studio 42

If this not work for some value, then it's because your values are not correctly set.
It's not possible to have different behavior for some price only.
Perhpas you rounded values are not right, or you use bad roundies in the Virtuemart config for your case

vladast

I would agree with you if I haven't tried this code with fresh VM installation.
I've added same base price and I'm getting same result, wrong button text because code is reading it different for some reason, or there is a bug in VM.

Values, base price is just typical number... for example 140. Then error occurs.

Maybe I've got lost inside my code, and I can't see error so I would like if someone could test this code and see if there is any error with it.

Studio 42

Again check:
Perhpas you rounded values are not right, or you use bad roundies in the Virtuemart config for your case
Simply dump the values and compare.
If VM set price to 10.01 and compare with 10.0099 then of course it not work.
So else you change the price config in VM, else you round all your values with php see http://php.net/manual/en/function.round.php before compare

vladast

Not sure why do you mean by dumping values? I've tried to play with decimals in VM config and no improvement. At the same time, my code doesn't do any roundies, it just compare 2 results and math is simple there.
BasePriceWithoutTax X Tax = BasePriceWithTax
if BasePriceWithTax X 0.9 = SalesPrice, that means 10% discount is recognized.
If you use calculator for this you will get result.
At the same time if I "echo" any of the formulas above I will get same numbers shown, so I'm totally confused why its not working. My code is just comparing results VM sales price with some percentage.


Like I wrote, problem is just with some BasePriceWithoutTax values, and numbers listed above don't seems to be calculated properly. I'm Bit lost as I can't find the error and I would like someone to troubleshoot this what has been explained before. Same values don't work on fresh VM installation.


Studio 42

Quote from: vladast on January 08, 2017, 11:33:38 AM
Not sure why do you mean by dumping values? I've tried to play with decimals in VM config and no improvement.

Eg. in your file add

var_dump($BasePriceWithTaxXdevedeset ,$this->product->prices['salesPrice']);

so you see the diff from the 2 price and know if this is a roundy problem or not.
you use !== and === , this is a strick compare and any little difference eg compare string to deciaml is not valid and give the bad result.