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.