forked from Crinsane/LaravelShoppingcart
-
Notifications
You must be signed in to change notification settings - Fork 2
Example
Michael V edited this page Feb 22, 2023
·
2 revisions
Below is a little example of how to list the cart content in a table:
<?php
// Add some items.
Cart::add('192ao12', 'Product 1', 1, 9.99);
Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
// Set an additional cost
Cart::cost(CostType::Transaction, 0.10);
Cart::cost(CostType::Shipping, 5.00);
Cart::cost('somethingelse', 1.11);
?>
<!-- Display the content in a View. -->
<table>
<thead>
<tr>
<th>Product</th>
<th>Qty</th>
<th>Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach(Cart::content() as $item) :?>
<tr>
<td>
<p><strong><?= $item->name ?></strong></p>
<p><?= $item->options->has('size') ? $item->options->size : '' ?></p>
</td>
<td><input type="text" value="<?= $item->qty ?>"></td>
<td>$ <?= $item->price ?></td>
<td>$ <?= $item->total ?></td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan="2"> </td>
<td>Subtotal</td>
<td><?= Cart::subtotal(); ?></td>
</tr>
<tr>
<td colspan="2"> </td>
<td>Tax</td>
<td><?= Cart::tax(); ?></td>
</tr>
<tr>
<td colspan="2"> </td>
<td><?= CostType::Transaction->description() ?></td>
<td><?= Cart::cost(CostType::Transaction); ?></td>
</tr>
<tr>
<td colspan="2"> </td>
<td><?= CostType::Shipping->description() ?></td>
<td><?= Cart::cost(CostType::Shipping); ?></td>
</tr>
<tr>
<td colspan="2"> </td>
<td>Somethingelse cost</td>
<td><?= Cart::cost('somethingelse'); ?></td>
</tr>
<tr>
<td colspan="2"> </td>
<td>Total</td>
<td><?= Cart::total(); ?></td>
</tr>
</tfoot>
</table>