show product attributes discounted prices on selecting a product size ajax laravel

  ajax, jquery, laravel

i want my web app to update the price based on the size a user selects.i have been able to achieve this but I want to calculate the discount for each product size and change it dynamically,that is the function calculates the discount for each attribute size and displays it price on the browser.i have done some research and found a function that does this.. but its shows an error undefined index"productattr_size",how can i solve this..

the getproductprice function

public function getproductprice(Request $request)
    {
        if($request->ajax()){
            $data=$request->all();
       $getdiscountedattrprice=Merchadise::getdiscountedattrprice($data['product_id'],$data['productattr_size']);
            
            return  $getdiscountedattrprice;
            

        }
    }

the route

Route::post('/getproductprice', [Home_Controller::class,'getproductprice'])->name('getproductprice');

the script for updating on the view

$(document).ready(function(){
    // change price based on the attribute
    $('#getprice').change(function(){
        var productsize=$(this).val();
        if(productsize==""){
            alert("Please Select Size");
            return false;
        }
        var product_id=$(this).attr("product-id");
        $.ajax({
            url:'/getproductprice',
            data:{productsize:productsize,product_id:product_id},
            type:'POST',
            success: function(resp){
                if(resp['discounted_price']>0){
                    $(".getattrprice").html("<del>Sh."+resp['productattr_price']+"</del>
                     Sh."+resp['discounted_price']);
                }else{
                    $(".getattrprice").html("Sh."+resp['productattr_price']);
                }
                ,error: function(error){
                    console.error(error)
                }
            
        });
    });

});

the getdiscountedattrprice function in merchandise model

public static function getdiscountedattrprice($product_id,$productattr_size){
            $proattrprice=Productattribute::where(['product_id'=>$product_id,
                            'productattr_size'=>$productattr_size]) ->first();
            $prodetails=Merchadise::select('merch_price','product_discount','merchcat_id')
                        ->where('id',$product_id)->first();
            $catdetails=Merchadisecategory::select('category_discount')
                        ->where('id',$prodetails['merchcat_id'])->first();
             if($prodetails['product_discount']>0){
                        $discounted_price=$proattrprice['productattr_price']- 
                               ($proattrprice['productattr_price']*$prodetails['product_discount']/100);
                       $discount=$proattrprice['productattr_price']-$discounted_price;
               }elseif($catdetails['category_discount']>0){
                            $discounted_price=$proattrprice['productattr_price']- 
                                ($proattrprice['productattr_price']*$catdetails['category_discount']/100);
                           $discount=$proattrprice['productattr_price']-$discounted_price;
              }else{
                             $discounted_price=0;
                            $discount=0;
             }
            return array('merch_price'=>$proattrprice['productattr_price']
                 ,'discounted_price'=>$discounted_price,'discount'=>$discount);
     }
    

the productattr_price and productattr_size are columns in the product attributes table

Source: Laravel

Leave a Reply