get product price based on customer code - php

My Product table looks like
ID Type Product Code Product Name Price
1 all customers 56620000 Product A 219.68 €
2 all customers 56621000 Product B 4,351.91 €
3 all customers 56622000 Product C 110.98 €
4 155000 56622000 Product C 100.00 €
5 all customers 56626000 Product D 2,410.38 €
6 all customers 56772000 Product E 100.00 €
7 160000 56772000 Product E 90.00 €
If you notice row 3,4 and 6,7 has same product code but with different type and price. That means a product can have a price for all customer as well as for few specific customers. If a customer with customer id 155000 performs a search with product code 56622000, that customer will get price 100.00 € (see row number 4) not 110.98 € (see row number 3). But if that same customer performs a search with product code 56772000, he/she will get price 100.00 €(see row number 6) not 90.00 €(see row number 7). Because there is no specific price for that user for product code 56772000.
My Query: How to execute this operation using PHP and MySql from a single table.?

SELECT *
FROM product
WHERE ProductCode = 'x' AND
Type IN ('y', 'All Customers')
ORDER BY FIELD(Type, 'y', 'All Customers')
LIMIT 1
SQLFiddle Demo
Replace x and y with your desired value shown in the demo.

SELECT *
FROM Products
WHERE ProductCode = 'product_id' AND Type IN ('customer_id', 'All Customers')
ORDER BY Type
LIMIT 1

SELECT * FROM
(SELECT * FROM product
WHERE `Type` = '155000' and ProductCode = '56622000'
LIMIT 1
UNION
SELECT * FROM product
WHERE `Type` = 'all customers' and ProductCode = '56622000'
LIMIT 1) a
LIMIT 1;
SQL FIDDLE DEMO
Thanks for #491243 for the fiddle code.

Related

Change magento2 price priority (Original price , Special price , Tier Prices , Group prices)

We know that magento2 has multiple prices type , for example special price, tier prices, group prices , if one prodcut has multiple price , the magento2 will show the minimum price .
Scenario 1:
Regular Price = 250
Special Price = 200
Group Price (NOT LOGGED) = 180
*** Result in product page: 180
Scenario 2:
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180
*** Result in product page: 120
So my question is how to ignore the minimum price , i want to set the group price highest priority , like below sample , i want to show price is 180
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180

Calculation Of tax with different product taxes rates php sql

I have following table "Cart" & contains the following columns in mysql:
CartID, ProductID, ProductPrice, ProductTax (%), ProductQty
ProductTax is in Percentage but value stored as number/int.
How do i calculate total tax of all products in the cart.
So each product must use its own "ProductTax" Rate ie. in %tage .
(1) example contain 1 product in cart.
ProductPrice: 200 & ProductTax 18%. & ProductQTY:1
So "totaltax" here is: 36.
(2) example contains 2 products in cart
1- ProductPrice: 300 & ProductTax 18% & ProductQTY: 2
2- ProductPrice: 400 & ProductTax 20% & ProductQTY: 1
So for above example "totaltax" is : 108+80= 188
To calculate total tax of all products in the cart:
SELECT SUM((ProductPrice * ProductQty)* ProductTax/100) AS value_sum
FROM Cart
This solves it! – Webgen 10 secs ago edit

mysql union or join just one query

I need to find a better way to get the discount for each article price in our web shop depending on which pricelist or pricelist/discount list a customer has. I think this is possible to do in just one query instead of the 5 I have today, but I really do not know where to start.
All our customers have a pricelist and some have both pricelist and one extra discount list. Today we have about 25 different pricelists and about 100 extra discount lists.
All the pricelists are structured in the same manner; they have a price group and a discount in percent.
For example pricelist 01 could look like
A 20
B 35
C 20
The extra discount list is structured in a different manner and can have a fixed price or percentage. It also has three different priority levels: discount based on the article code (has priority 1), based on category (has priority 2) and based on price group (has priority 3).
Discount list 0013 could look like:
In the article tab
PL-2344-444 40 (%)
P-0455-23 200 (SEK)
In the category tab
C12 50 (%)
N12 35 (%)
Today I have three different queries to see if I get a hit in the discount list:
First I check to see if I get a hit in priority 1: (FIXED returns f and PERCENTAGE r)
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013'
If the above returns 0, I do the second query, priority 2:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013'
And the last one priority 3:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013'
If none of the extra discount lists returns 0 I get the discount from the pricelist
SELECT DISCOUNT FROM PUPRIREG WHERE PRICELIST = '01' AND PRICEGROUP = 'F'
I call the function like follows
$discount = discount($articlecode, $category, $pricegroup);
function discount($articlecode, $category, $pricegroup){
$articlecode = sanitizingData($articlecode);
$category = sanitizingData($category);
$pricegroup = sanitizingData($pricegroup);
// do priority 1
// prio 2
// prio 3
// pricelist
return $discount;
}
I would be so happy if someone could show me how to do this. I am using mysqli and php.
Many thanks
Best regards linda
You can do the queries with a union:
(SELECT DISCOUNT, FIXED, PERCENTAGE, 1 priority FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 2 priority FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 3 priority FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, 0 fixed, 0 percentage, 4 priority FROM PUPRIREG
WHERE PRICELIST = '01' AND PRICEGROUP = 'F')
order by priority;
The additional artificial priority column and order by ensures, that you get the discounts properly sorted.

MYSQL sort function, depending on the arithmetic operation using the database field values as array keys

I have table of products, and there are 2 fields: price, currency. Possible values in currency fields are 1,2,3. 1 for MDL, 2 for USD, and 3 for EUR. I have to sort my products by price in mdl currency. So i have an array with rates:
$v = array(1,11.38,15.8);
Help my please with my query, I've tried something like this, but I've got errors:
$results = $this->Product
->query("SELECT `id`,`price`,`currency` FROM products
ORDER BY price*$v[currency] DESC");
Hmm... I`ll try to explain, through an example.
My table:
id|price|currency
_________________
1 | 500 | 2
2 | 300 | 3
It shows that first products price is saved in USD, and second products price is saved in EUR. But i have to sort them in MDL valute. So i get an array of rates for each value:
$rates = array([1] = > 1, [2] => 11.50, [3] => 15.50);
So i have to order my products by the result of formula: price*value rate
in first case: 500*$rates['currency value from db, in our case 2] = 500 * 11.50 etc.
Thanks in advance.
Because of the extended example on this problem I have edited this query.
Lets assume that the currencies are alse stored in some table, lets say currency (if not, it should be anyway).
Table currency should be as follows:
ID VALUE CODE
-----------------------------
1 1 USD
2 11.38 EUR
3 15.8 MDL
Then the query should be:
SELECT p.`id`, p.`price`, p.`price` * c.`value` AS 'ratio'
FROM products p
LEFT JOIN currency c ON c.`id` = p.`currency`
ORDER BY `ratio` DESC
By this query You select the currency value from the table currency depending on the currency ID from products table and finaly the results are ordered by the ration price * currency value.
I understand that maybe You have the currencies only hardcoded as array within some config, but it really would be better to put the currencies into the DB (if it is not).
You can`t use data base column name as array keys, because mysql is later instance than php. In php you simply generate query string that is passed to database managment system.
Your query should look like this:
$results = $this->Product->query
(
"SELECT `id`,`price`,
CASE `currency`
WHEN '1' THEN $v[0]
WHEN '2' THEN $v[1]
WHEN '3' THEN $v[2]
END AS 'ratio'
FROM products
ORDER BY price*ratio DESC
"
);

"Order by price" returns a weird order in MySQL

I have a local-only project which I'm working on where I have a table with id, title and price fields.
Example info:
ID || Title || Price
1 - Title 1 - 8.00
2 - Title 2 - 75.00
3 - Title 3 - 70.00
When I try to ORDER BY price it comes back like this:
8.00
75.00
70.00
Statement:
$query = mysql_query("Select * From table ORDER BY price DESC");
What am I doing wrong?
Your price column must have a character CHAR() or VARCHAR() type rather than a numeric type. Cast it as a DECIMAL in the ORDER BY:
Select * From table ORDER BY CAST(price AS DECIMAL(10,2)) DESC
The real fix for this would be to change the price data type to a proper numeric type.

Categories