gpt4 book ai didi

php - 根据折扣和数量计算新价格

转载 作者:行者123 更新时间:2023-11-28 23:24:59 25 4
gpt4 key购买 nike

我试图开发一个函数,允许计算数量折扣函数中的特定价格

示例:10% = $discount_customer[]在数据库中运行

$qty由用户添加

if $qty < 5 then $$discount_customer[] = 0 : 价格不变

if $qty > 5 et qty < 10 then $$discount_customer[] = 10% : 价格 -10%

...

if $qty > 10 then $$discount_customer[] = 15% : 价格 -15%

$id : 商品编号

$qty : 客户输入的订单数量

$products_price : 产品价格

public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');

$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());

$QprodutsQuantityDiscount->execute();

while ($QprodutsQuantityDiscount->fetch()) {

// quantity discount
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');

// Customer discount
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}

我想创建一个 foreach,但是怎么做呢?

该元素不采用 between 条件。

  foreach ($discount_quantity as $k => $quantity)  {

print_r('<pre>');
var_dump($quantity );
print_r('</pre>');


foreach ($discount_customer as $c => $discount) {

if ($qty > $quantity) {
$news_price = $products_price -(($products_price * $discount) /100);
}

print_r('<pre>');
print_r($news_price);
print_r('</pre>');

}

 return $newprice
}

enter image description here

enter image description here

谢谢

最佳答案

public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');

$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());

$QprodutsQuantityDiscount->execute();

while ($QprodutsQuantityDiscount->fetch()) {
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}

return $this->getPrctDiscount($qty, $products_price);
}

public function getPrctDiscount($qty, $products_price)
{
$discount = 0;
if ($qty > 5 && $qty < 10) {
$discount = 10;
}elseif ($qty > 10) {
$discount = 15;
}

$newPrice = $discount > 0
? $products_price - ($products_price * ($discount/100))
: $newPrice;

return $newPrice;
}

关于php - 根据折扣和数量计算新价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39750239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com