计算折扣和数量的新价格

时间:2016-09-28 14:20:22

标签: php mysql

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

示例:数据库

中的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:产品的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但是如何?

这个元素没有处理条件。

  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

谢谢

1 个答案:

答案 0 :(得分:1)

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;
    }