newlaravelcoder's avatar

Selecting Dynamic Input Data

Hey all. I'm new to the Laravel universe and I'm putting the finishing touches on an ecommerce app. The final frontier now is getting the dynamic shipping rate, and adding it to my overall order total, but I'm at a loss as to how to do this. The rates are coming through a foreach loop like this

   @foreach ($rates as $rate)
           <tr>
               <td>
                   <img src="{{ $rate->provider_image_75 }}" alt="">
                   {{ $rate->provider }} ({{ $rate->duration_terms }})
               </td>
               <td width="20%">
                   <input type="radio" class="pull-right" name="rate" value="{{ $rate->object_id }}">
                   ${{ $rate->amount }}
               </td>
           </tr>

The $cartTotal comes from the bumbummen99 shopping cart? Any advice?

0 likes
5 replies
Snapey's avatar

you need to present the user with a choice of options?

convert the list into a form with radios

newlaravelcoder's avatar

@Snapey but the options do show up through the foreach loop which is dynamic. There is an input tag with a radio type, but it doesn't go anywhere. Please see the code above. It's already wrapped in a form.

newlaravelcoder's avatar

After some tinkering I managed to come up with an iteration variable like so

<?php $i = 0; ?>
   @foreach ($rates as $rate)

   <tr>
       <td>
           <img src="{{ $rate->provider_image_75 }}" alt="">
           {{ $rate->provider }} ({{ $rate->duration_terms }})
       </td>
       <td width="20%">
           <input type="radio" class="pull-right" name="rate_{{$i}}" value="{{ $rate->object_id }}">
           ${{ $rate->amount }}
       </td>
   </tr>
   <?php $i++; ?>


Now the issue is, all the rates get selected as opposed to one. Because I had to put the chunk of code directly on the blade as opposed to the controller, I'm unsure how to fix this issue so only the selected rate is requested and added to the {{ $cartTotal }} variable. Any ideas? Thanks. Again, I'm new to Laravel and learning a ton about php development, so I ask for your patience.

Snapey's avatar

when you use a foreach loop you have access to a$loop variable with handy attributes like $loop->index

but anyway all your radios should have the same name like your original code

When you post the form to the controller you will know which radio was selected by the value of $request->rate

you then use this is to look up the price and add it to the total

newlaravelcoder's avatar

@Snapey I figured that, but in order to get this shipping api to work, I have directly in a php block on the blade, so I'm a bit unsure on how to execute what your'e saying. Like I said earlier, I'm new to Laravel and php development. After doing some research and putting things together.. I came across something like this

  <?php $i++;

            foreach($request->all() as $inputName => $input){

    if(\Str::startsWith($inputName, 'rate_'){
        
    }

}

I'm using this code directly on the stripe page. The cartotal on my StripeController looks like this

 public function StripeOrder(Request $request) {

 if (Session::has('coupon')) {

 $total_amount = Session::get('coupon')['total_amount'];

 }

 else {

 $total_amount = Cart::total();

 }

  \Stripe\Stripe::setApiKey('***');

 $token = $_POST['stripeToken'];

 $charge = \Stripe\Charge::create([
'amount' => $total_amount*100,
 'currency' => 'usd',
 'description' => 'Pots N Potions',
 'source' => $token,
 'metadata' => ['order_id' => uniqid()],
]);

The variable $total_amount corresponds to the $cartTotal. So I'm guessing that on the blade, that I can get the selected rate and add it directly to the $cartTotal. That's my thinking.

Please or to participate in this conversation.