sharmila
sharmila

Reputation: 175

How to apply the promotion code only once for a product in laravel

I need to apply the promotion code only once for a product, when I apply the same code for the same product it should not get applied.

Find below the code :

   public function apply_promo(Request $request)
{
    $name = $request->input();
    $code=$name['code'];
    $code_flags=0;
    $p_id='';
    $discount_value=0;
    $discount_type='';
    $Object = new Promotion();
    $response = $Object->GetPromotion();
    //print_r($response);exit;
    foreach($response['promotions']['promotion'] as $value)
    {
        if($value['code']==$code)
        {
            $code_flags=1;
            $p_id=$value['requires'];
            $discount_value=$value['value'];
            $discount_type=$value['type'];
        }
    }
    if($code_flags==1)
    {
        $p_id_array=explode(',',$p_id);
        $flags=0;
        $data=json_encode(Cart::content());
        $cartdata=json_decode($data);
    //echo "<pre>";print_r($cartdata);exit;
    foreach($cartdata as $key => $value)
    {
            if($value->options->package != 'domain')
                {   
                if(in_array($value->options->gid,$p_id_array))  
                    {
                        $flags=0;
                        $price=$value->price;
                            if($discount_type=='Percentage')
                                {
                                   $discount_p=(($price*$discount_value)/100);
                                   $price=$price-$discount_p;
                                   $value->options->discount_code = $code;
                                   $value->options->discount_price = $discount_p;
                                   $value->options->discount_percentage = $discount_value;
                                   Cart::update($value->rowId, ['price' => $price]);

                                }
                    }
                }


    }
    if($flags==0)
    {
        session()->put('promo_code', $code);
        \Session::flash('message');
        return redirect('cart?success');
    }
    }
    else{
        session()->put('promo_code', '');
        \Session::flash('message_error');
        return redirect('cart?error');


    }


}

I have tried using the above code, but here the code getting applied as many times as i submit the code. Kindly suggest me a solution to solve this.

Upvotes: 0

Views: 1128

Answers (1)

hossamGamal
hossamGamal

Reputation: 171

you can make table the have used promotions and their product , say we name it product_promotions have ( promo_id , product_id ) . then in product model create relation called promocodes (many to many)

public function promocodes(){
 return $this->belongsToMany(Promo::class)->withPivot(['promo_id']);
}

this relation will return all promos used by this products so we now able to check if promo used before by it using this code

$product->promocodes()->where('promo_id' , $your_promo_code_id)->exists();

Upvotes: 1

Related Questions