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

时间:2018-10-22 14:01:57

标签: php codeigniter codeigniter-3

我发现自己遇到了一个问题,我试图让用户有机会获得折扣并对其激活的产品应用折扣。

可以说,我发布了一个附加了Discount_id的新产品。

然后,任何知道特定discount_id代码的用户都应获得从商品原始价格中减去的折扣金额($),并应在购物车中对其进行更新。

这是我要创建的函数,但是我太困惑了:

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

                }

            }
    }

这是我要使用的模型中的方法:

/*
  *
  * 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);
  }

到目前为止,即使折扣(百分比)不是100%,我得到的行为还是用户购物车价格中的“ 0”更新。

非常感谢您的帮助。

0 个答案:

没有答案