PHP code example of freshbitsweb / laravel-cart-manager

1. Go to this page and download the library: Download freshbitsweb/laravel-cart-manager 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/ */

    

freshbitsweb / laravel-cart-manager example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use Freshbitsweb\LaravelCartManager\Traits\Cartable;

class Product extends Model
{
    use Cartable;
    // ...
}

// Add to cart
$cart = Product::addToCart($productId);

// Remove from cart
$cart = cart()->removeAt($cartItemIndex);

// Apply discount
$cart = cart()->applyDiscount($percentage);

// Fetch cart
$cart = cart()->toArray();

php artisan vendor:publish --tag=laravel-cart-manager-config

php artisan vendor:publish --tag=laravel-cart-manager-migrations
php artisan migrate

/**
 * Add to cart
 *
 * @return json
 */
 public function addToCart()
{
    return Product::addToCart(request('productId'));
}

/**
 * Increment cart item quantity
 *
 * @return json
 */
public function incrementCartItem()
{
    return cart()->incrementQuantityAt(request('cartItemIndex'));
}

/**
 * Decrement cart item quantity
 *
 * @return json
 */
public function decrementCartItem()
{
    return cart()->decrementQuantityAt(request('cartItemIndex'));
}

/**
 * Update user input quantity.
 *
 * @return json
 */
public function cartItemQuantitySet()
{
    return cart()->setQuantityAt(request('cartItemIndex'), request('cartQuantity'));
}


/**
 * Clear Cart
 *
 * @return json
 */
public function clearCart()
{
    return cart()->clear();
}

protected $listen = [
	'Freshbitsweb\LaravelCartManager\Events\CartCreated' => [
		'App\Listeners\LogCartCreated',
	],
];



namespace App\Listeners;

use Illuminate\Support\Facades\Log;
use Freshbitsweb\LaravelCartManager\Events\CartCreated;

class LogCartCreated
{
	/**
	 * Handle the event.
	 *
	 * @param CartCreated $event
	 * @return void
	 */
	public function handle(CartCreated  $event)
	{
		Log::info('cart', [$event->cartData]);
	}
}

protected function schedule(Schedule $schedule)
{
    $schedule->command('lcm_carts:clear_old')->daily();
}