如何在Laravel中将产品颜色信息传递到结帐页面

huangapple go评论63阅读模式
英文:

How to pass the product color information to the checkout in Laravel

问题

I understand that you want assistance with passing the product color attribute to the checkout page for a Laravel 9 and Livewire project. Here's the translated code part:

product-details.blade.php

<h2>{{ $product->name }}</h2>
<div class="price-box pt-20">
    <span class="new-price new-price-2">&#8358;
        <span class="text-danger">
            {{ number_format((float) $product->price, 2) }}
        </span>
    </span>
</div>
<div>
    <span class="new-price">
        <p>
            <span class="float-left">
                <label for="color">选择颜色:</label>
            </span>
            <select class="nice-select" name="color">
                @foreach ($colors as $color)
                    <option value="{{ $color }}">{{ $color }}</option>
                @endforeach
            </select>
        </p>
    </span>
    <br>
</div>
<br>
<div style="margin-bottom: 50px;"></div>
<div class="product-desc">
    <p>
        <span>{!! $product->description !!}</span>
    </p>
</div>

checkout.blade.php

@if ($cart_count > 0)
<div class="col-lg-6 col-12">
    <div class="your-order">
        <h3>您的订单详情</h3>
        <div class="your-order-table table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th class="cart-product-name">产品</th>
                        <th class="cart-product-total">总价</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach(\Gloudemans\Shoppingcart\Facades\Cart::content() as $product)
                    <tr class="cart_item">
                        <td class="cart-product-name">
                            {{ $product->name }} | {{ $product->colour }}
                            <strong class="product-quantity"> × {{$product->qty}}</strong>
                        </td>
                        <td class="cart-product-total">
                            <span class="amount">&#8358;{{$product->price}}</span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
                <tfoot>
                    <tr class="cart-subtotal">
                        <th>购物车小计</th>
                        <td>
                            <span class="amount">&#8358; {{ $subtotal }}</span>
                        </td>
                    </tr>
                    <tr class="cart-subtotal">
                        <th>购物车税费</th>
                        <td>
                            <span class="amount">&#8358; {{ $tax }}</span>
                        </td>
                    </tr>
                    <tr class="order-total">
                        <th>订单总额</th>
                        <td>
                            <strong>
                                <span class="amount">&#8358; {{ $total }}</span>
                            </strong>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
@endif

Please note that I've only translated the code parts you provided without additional explanations or responses. If you have any specific questions or need further assistance, please feel free to ask.

英文:

Please I need help on passing the product color attribute to the checkout page for a project I am working on.
I am working with Laravel 9 and Livewire.

I have been able to display the product colors from the DB information in the product details page, but I have no clue on how to pass the selected product color when the product is added to cart to the checkout page for order confirmation.

This is what the product details looks like

product-details.blade.php

&lt;h2&gt;{{ $product-&gt;name }}&lt;/h2&gt;
&lt;div class=&quot;price-box pt-20&quot;&gt;
&lt;span class=&quot;new-price new-price-2&quot;&gt;&amp;#8358;
&lt;span class=&quot;text-danger&quot;&gt;
{{ number_format((float) $product-&gt;price, 2) }}
&lt;/span&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;&quot;&gt;
&lt;span class=&quot;new-price&quot;&gt;
&lt;p&gt;
&lt;span class=&quot;float-left&quot;&gt;
&lt;label for=&quot;color&quot;&gt; Select Colour: &lt;/label&gt;
&lt;/span&gt;
&lt;select class=&quot;nice-select&quot; name=&quot;color&quot;&gt;
@foreach ($colors as $color)
&lt;option value=&quot;{{ $color }}&quot;&gt;{{ $color }}&lt;/option&gt;
@endforeach
&lt;/select&gt;
&lt;/p&gt;
&lt;/span&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div style=&quot;margin-bottom: 50px;&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;product-desc&quot;&gt;
&lt;p&gt;
&lt;span&gt;{!! $product-&gt;description !!}&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;

checkout.blade.php

@if ($cart_count&gt;0)
&lt;div class=&quot;col-lg-6 col-12&quot;&gt;
&lt;div class=&quot;your-order&quot;&gt;
&lt;h3&gt;Your order details&lt;/h3&gt;
&lt;div class=&quot;your-order-table table-responsive&quot;&gt;
&lt;table class=&quot;table&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&quot;cart-product-name&quot;&gt;Product&lt;/th&gt;
&lt;th class=&quot;cart-product-total&quot;&gt;Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
@foreach(\Gloudemans\Shoppingcart\Facades\Cart::content() as $product)
&lt;tr class=&quot;cart_item&quot;&gt;
&lt;td class=&quot;cart-product-name&quot;&gt;
{{ $product-&gt;name }} | {{ $product-&gt;colour }}
&lt;strong class=&quot;product-quantity&quot;&gt; &#215; {{$product-&gt;qty}}&lt;/strong&gt;
&lt;/td&gt;
&lt;td class=&quot;cart-product-total&quot;&gt;
&lt;span class=&quot;amount&quot;&gt;&amp;#8358;{{$product-&gt;price}}&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
@endforeach
&lt;/tbody&gt;
&lt;tfoot&gt;
&lt;tr class=&quot;cart-subtotal&quot;&gt;
&lt;th&gt;Cart Subtotal&lt;/th&gt;
&lt;td&gt;
&lt;span class=&quot;amount&quot;&gt;&amp;#8358; {{ $subtotal }}&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;cart-subtotal&quot;&gt;
&lt;th&gt;Cart Tax&lt;/th&gt;
&lt;td&gt;
&lt;span class=&quot;amount&quot;&gt;&amp;#8358; {{ $tax }}&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;order-total&quot;&gt;
&lt;th&gt;Order Total&lt;/th&gt;
&lt;td&gt;
&lt;strong&gt;
&lt;span class=&quot;amount&quot;&gt;&amp;#8358; {{ $total }}&lt;/span&gt;
&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tfoot&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Of course, I am using Gloudemans Shoppingcart.
This is what my product details livewire component looks like

ProductDetails.php

class ProductDetails extends Component
{
use WithPagination;
protected $paginationTheme = &#39;bootstrap&#39;;
public $productid, $category, $colors, $sizes;
public $product, $quantity, $productquantity;
public function render()
{
return view(&#39;livewire.product-details&#39;, [
&#39;productfromcats&#39;=&gt; Product::where(&#39;category&#39;, &#39;=&#39;, $this-&gt;category)
-&gt;orderByRaw(&#39;created_at DESC&#39;)
-&gt;paginate(8, [&#39;*&#39;], &#39;catsPage&#39;),
]);
}
public function mount($product_id)
{
$this-&gt;product = Product::where(&#39;product_id&#39;, &#39;=&#39;, $product_id)-&gt;get()-&gt;first();
$this-&gt;colors = $this-&gt;product-&gt;colour;
$this-&gt;sizes = $this-&gt;product-&gt;size;
$search = Cart::search(function ($cartItem, $rowId) use ($product_id) {
return $cartItem-&gt;id === $product_id;
});
if ($search-&gt;count() &gt; 0) {
$cartproduct = $search-&gt;first();
$this-&gt;productquantity = $cartproduct-&gt;qty;
} else {
$this-&gt;productquantity = 0;
}
}
public function addItemToCart($item, $qty)
{ 
$cartItem = Cart::add(
[
&#39;id&#39; =&gt; $item[&#39;id&#39;],
&#39;name&#39; =&gt; $item[&#39;name&#39;],
&#39;qty&#39; =&gt; $qty,
&#39;price&#39; =&gt; $item[&#39;price&#39;],
&#39;weight&#39; =&gt; 0,
&#39;options&#39; =&gt; [
&#39;size&#39; =&gt; $item[&#39;size&#39;],
&#39;colour&#39; =&gt; $item[&#39;colour&#39;],
&#39;images&#39; =&gt; $item[&#39;images&#39;]
]
]
);
$cartItem-&gt;associate(&#39;\App\Models\Product&#39;);
$this-&gt;productquantity = Cart::get($cartItem-&gt;rowId)-&gt;qty;
if (Cart::get($cartItem-&gt;rowId)-&gt;qty == 0) {
Cart::remove($cartItem-&gt;rowId);
}
$this-&gt;emit(&#39;cart_updated&#39;);
}
}

答案1

得分: 2

你正在尝试直接访问产品的颜色。

<td class="cart-product-name">
    {{ $product->name }} | {{ $product->colour }}
    <strong class="product-quantity"> × {{ $product->qty }}</strong>
</td>

而应该根据文档中所述,使用options 来访问颜色。

{{
    $product->options->has('colour')
    ? is_array($product->options->colour)
        ? ' | ' . implode(", ", $product->options->colour)
        : ' | ' . $product->options->colour
    : ''
}}
英文:

You are trying to access the colour of the product directly.

&lt;td class=&quot;cart-product-name&quot;&gt;
{{ $product-&gt;name }} | {{ $product-&gt;colour }}
&lt;strong class=&quot;product-quantity&quot;&gt; &#215; {{ $product-&gt;qty }}&lt;/strong&gt;
&lt;/td&gt;

Instead access the colour using the options as stated in the documentation

{{
$product-&gt;options-&gt;has(&#39;colour&#39;)
? is_array($product-&gt;options-&gt;colour)
? &#39; | &#39; . implode(&quot;, &quot;, $product-&gt;options-&gt;colour)
: &#39; | &#39; . $product-&gt;options-&gt;colour
: &#39;&#39;
}}

huangapple
  • 本文由 发表于 2023年7月18日 10:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定