Jason Paddle
Jason Paddle

Reputation: 1103

Getting property from array and replace

I have this array:

object(stdClass)#458 (2) {
  ["1"]=>
  object(stdClass)#456 (3) {
    ["title"]=>
    string(3) "Test Product 1"
    ["quantity"]=>
    int(1)
    ["price"]=>
    int(1221)
  }
  ["shipping"]=>
  object(stdClass)#457 (3) {
    ["quantity"]=>
    int(1)
    ["title"]=>
    string(13) "Free Delivery"
    ["price"]=>
    int(0)
  }
}

I'm displaying this on the page like this.

@foreach($order->getOrderData($order->order_details) as $itemId => 
     <?php $total += ($item->price * $item->quantity); ?>
     <h4 class="media-heading">{{ $item->title }}</h4>
      {{ $item->quantity }}
      ${{ $item->price * $item->quantity }} / €{{ $order['total_eur'] }}
@endforeach

So far everything works perfect except the addition value which I call from database $order['total_eur']. This value is saved when order is placed so I then show total in USD and total in EUR.

The problem here is that ${{ $item->price * $item->quantity }} contain also shipping and when shipping is Free e.g. $ 0 is shown on page

Test Product 1   $122 /  €113      
Free Delivery    $0 / €113 

What I'm trying to do is to replace somehow when the delivery is free e.g. ["shipping"]=>["price"] = 0 to show 0 instead. to be like this:

Test Product 1   $122 /  €113      
Free Delivery    $0 / €0 

I can access it like this

<?php $shipping = $order->getOrderData($order->order_details)->shipping->price; ?>

But I can't figured it out how can I change it in the foreach loop.

I hope I explained this clear and understandable. Can anyone suggest something that may work?

Update: I've tried this simple if inside foreach but still showing both eur price on both

${{ $item->price * $item->quantity }} 
  @if( ! $order->getOrderData($order->order_details)->shipping) 
      {{ $order->getOrderData($order->order_details)->shipping->price }} 
  @else 
      €{{ $order['total_eur'] }} 
  @endif

Upvotes: 0

Views: 75

Answers (1)

Saravanan Sampathkumar
Saravanan Sampathkumar

Reputation: 3261

@foreach($order->getOrderData($order->order_details) as $itemId => $item)
     <?php $total += ($item->price * $item->quantity); ?>
     <h4 class="media-heading">{{ $item->title }}</h4>
      {{ $item->quantity }}
      @if ($itemId == "shipping" && $item->price == 0) 
        $0 / €0
      @else
        ${{ $item->price * $item->quantity }} / €{{ $order['total_eur'] }}
      @endif
@endforeach

Here we are checking the array whether it is shipping item and price is set to zero. If so we are displaying zero €, else we are displaying the actual cost.

Upvotes: 1

Related Questions