"codeigniter 4" cart

<?php echo form_open('path/to/controller/update/method'); ?>

<table cellpadding="6" cellspacing="1" style="width:100%" border="0">

<tr>
        <th>QTY</th>
        <th>Item Description</th>
        <th style="text-align:right">Item Price</th>
        <th style="text-align:right">Sub-Total</th>
</tr>

<?php $i = 1; ?>

<?php foreach ($this->cart->contents() as $items): ?>

        <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

        <tr>
                <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
                <td>
                        <?php echo $items['name']; ?>

                        <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>

                                <p>
                                        <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>

                                                <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />

                                        <?php endforeach; ?>
                                </p>

                        <?php endif; ?>

                </td>
                <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
                <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
        </tr>

<?php $i++; ?>

<?php endforeach; ?>

<tr>
        <td colspan="2"> </td>
        <td class="right"><strong>Total</strong></td>
        <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>

</table>

<p><?php echo form_submit('', 'Update your Cart'); ?></p>

3.78
9
Tower 115 points

                                    function cart(bool $getShared = true)
{
    return \Config\Services::cart($getShared);
}

3.78 (9 Votes)
0
0
7
Felix Jordan 100 points

                                    public static function cart($getShared = true)
{
        if ($getShared) {
            return static::getSharedInstance('cart');
        }
        return new \App\Libraries\Cart();
 }

0
0
4.2
10

                                    
// Call the cart service using the helper function

$cart = cart();



// Insert an array of values

$cart-&gt;insert(array(

&nbsp; &nbsp;'id'&nbsp; &nbsp; &nbsp; =&gt; 'sku_1234ABCD',

&nbsp; &nbsp;'qty'&nbsp; &nbsp; &nbsp;=&gt; 1,

&nbsp; &nbsp;'price'&nbsp; &nbsp;=&gt; '19.56',

&nbsp; &nbsp;'name'&nbsp; &nbsp; =&gt; 'T-Shirt',

&nbsp; &nbsp;'options' =&gt; array('Size' =&gt; 'L', 'Color' =&gt; 'Red')

));



// Update an array of values

$cart-&gt;update(array(

&nbsp; &nbsp;'rowid'&nbsp; &nbsp;=&gt; '4166b0e7fc8446e81e16883e9a812db8',

&nbsp; &nbsp;'id'&nbsp; &nbsp; &nbsp; =&gt; 'sku_1234ABCD',

&nbsp; &nbsp;'qty'&nbsp; &nbsp; &nbsp;=&gt; 3,

&nbsp; &nbsp;'price'&nbsp; &nbsp;=&gt; '24.89',

&nbsp; &nbsp;'name'&nbsp; &nbsp; =&gt; 'T-Shirt',

&nbsp; &nbsp;'options' =&gt; array('Size' =&gt; 'L', 'Color' =&gt; 'Red')

));



// Get the total items

$cart-&gt;totalItems();



// Remove an item using its `rowid`

$cart-&gt;remove('4166b0e7fc8446e81e16883e9a812db8');

&nbsp;

// Clear the shopping cart

$cart-&gt;destroy();



// Get the cart contents as an array

$cart-&gt;contents();

4.2 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source