PHP code example of mykholy / shopping-cart

1. Go to this page and download the library: Download mykholy/shopping-cart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mykholy / shopping-cart example snippets


use Illuminate\Database\Eloquent\Model;
use Mykholy\ShoppingCart\Buyable;
use Mykholy\ShoppingCart\BuyableTrait;

class Product extends Model implements Buyable
{
    use BuyableTrait;
}

use Mykholy\ShoppingCart\Facades\Cart;

$product = Product::create(['name' => 'Pizza Slice', 'price' => 1.99]);
$quantity = 2;

Cart::add($product, $quantity);

Cart::content();
// or
Cart::items();

Cart::subtotal();

$item = Cart:content()->first();

Cart::update($item->id, $item->quantity + 5);

Cart::remove($item->id);

class Product extends Model implements Buyable
{
    // ...
    
    public function getOptions(): array
    {
        return [
            'size' => ['18 inch', '36 inch'],
            'color' => ['white', 'blue', 'black'],
        ];
    }
}

Cart::add($product, 3, ['color' => 'white']);

$item = Cart:content()->first();

// Update a single option
Cart::updateOption($item->id, 'color', 'black');

// Update multiple options at once
Cart::updateOptions($item->id, [
    'color' => 'black',
    'size' => '36 inch',
]);

class RegisterController
{
    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        Cart::attachTo($user);
    }
}

class LoginController
{
    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        Cart::loadUserCart($user);
    }
}

$rate = 13; // The tax rate as a percentage

Cart::tax($rate);

// config/shopping-cart.php

    'tax' => [
        'rate' => 6,
    ],

Cart::tax();

// config/shopping-cart.php

    'tax' => [
        'mode' => 'per-item',
    ],

use Mykholy\ShoppingCart\Taxable;

class Product extends Model implements Buyable, Taxable
{
    /**
     * Calculate the tax here based on a database column, or whatever you will.
     *
     * @return int|float
     */
    public function getTaxRate()
    {
        if ($this->tax_rate) {
            return $this->tax_rate;
        }

        if (! $this->taxable) {
            return 0;
        }

        return 8;
    }
bash
php artisan vendor:publish --provider="Mykholy\ShoppingCart\CartServiceProvider"
bash
php artisan migrate