kurama
kurama

Reputation: 797

Calculate a new price in function the discount and quantity

I trying to develop a function allowing to calculate a specific price in function the quantity discount

Example : 10% = $discount_customer[] in function the database

$qty is add by an user

if $qty < 5 then $$discount_customer[] = 0 : price does'nt change

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

...

if $qty > 10 then $$discount_customer[] = 15% : price with -15%

$id : id of the product

$qty : order quantity inserted by a customer

$products_price : price of the product

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

I suppose to create a foreach but how ?

Thie element doesn't take the between condition.

  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

thank you

Upvotes: 1

Views: 535

Answers (1)

Algoleigol
Algoleigol

Reputation: 201

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

Upvotes: 1

Related Questions