douyu4822
2017-03-22 10:29
浏览 42
已采纳

从数组获取属性并替换

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
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmei8209 2017-03-22 10:51
    已采纳
    @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.

    打赏 评论

相关推荐 更多相似问题