dongyong1400 2018-10-22 14:01
浏览 6

如何在CodeIgniter中配置个人折扣系统

I found myself facing a problem in which I try to give the users the opportunity to get and apply discounts to products that have them active.

Let say, I publish a new product which has a discount_id attached to it.

Then any user who knows the code for the specific discount_id should get the discount amount($) subtracted from the original price of the product item and it should be updated in their cart.

This is my function that I'm trying to create but I'm getting way too confused:

public function discountItemCodeVerification($id){

            // Verify discount exists
            $discount   =   $this->input->post('discount_code');
            $result     =   $this->PublicCart_model->verifyDiscountCode($discount);

            if(!$result){

                // Set message
                $this->session->set_flashdata('error', 'The discount has expired or is invalid');

                // Redirect
                redirect('cart/userCart');

            } else {

                // Get Last ID Data
                $postID =   $id;

                // Get Relationship Data
                $discountID     =   $result->discount_id;
                $discountType   =   $this->PublicCart_model->getDiscountID($discountID);

                // Verify Item has an Individual Discount Attached to Itself
                $individualDiscount = $this->PublicCart_model->verifyItemDiscountCode($postID, $discountID);

                if($discountType->type == 'percent' || !empty($individualDiscount)){

                    // Get data
                    $data['item']   =   $this->PublicStore_model->readPostID($id);

                    // HERE IS WHERE I NEED HELP
                    $postData    =   array(
                        // 'price' =>  round($data->price * ((100 - $result->amout) / 100 ), 2),
                        // 'price' =>  $data[item]->price * (( 100 - $result->amout) / 100),
                        'price' =>  $data['item']->price - ($data['item']->price * ($result->amount/100)),
                    );
                    // Update Cart Data
                    $this->PublicCart_model->updateCartItem($postID, $postData);

                    // Set message
                    $this->session->set_flashdata('success', 'A percentage type discount has been applied to your final price');

                    // Redirect
                    redirect('cart/userCart');

                } elseif($discountType->type == 'float' || !empty($individualDiscount)){

                    // Set message
                    $this->session->set_flashdata('success', 'A float type discount has been applied to your final price');

                    // Redirect
                    redirect('cart/userCheckout');

                }

            }
    }

and here's the methods in my model that I'm trying to use:

/*
  *
  * VERIFY CART DISCOUNTS AND GET ITS RELATIONSHIP
  *
  */
  public function verifyDiscountCode($discount){
    $query  =  $this->db->get_where($this->discounts, array(
      'code'  =>  $discount,
    ));
    return $query->row();
  }

  public function getDiscountID($discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }
  /*
  *
  * VERIFY INDIVIDUAL CART ITEM DISCOUNTS
  *
  */
  public function verifyItemDiscountCode($postID, $discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'post_id'     =>  $postID,
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }

  public function updateCartItem($postID, $postData){
    $this->db->select('*');
    $this->db->where('friend_id', $this->session->userdata('user_id'));
    $this->db->where('post_id', $postID);
    $this->db->where('type', $this->cartType);
    $this->db->update($this->relationship, $postData);
  }

As of right now the behavior that I get is the update to "0" in the user cart item price even when the discount(percentage) is not 100%.

Any help will be very much appreciated, thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)