PHP code example of marshmallow / cart

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

    

marshmallow / cart example snippets


Route::middleware('cart')->group(function () {
    // storefront routes
});

use Marshmallow\Ecommerce\Cart\Contracts\Purchasable;
use Marshmallow\Ecommerce\Cart\Models\ShoppingCart;
use Marshmallow\Ecommerce\Cart\Support\Price;

class Product extends Model implements Purchasable
{
    public function getPurchasableKey(): int|string
    {
        return $this->getKey();
    }

    public function getPurchasableName(): string
    {
        return $this->name;
    }

    public function getPurchasablePrice(int $quantity = 1, ?ShoppingCart $cart = null): Price
    {
        return Price::fromGross($this->price_cents, 21.0, 'EUR');
    }

    public function isAvailableForPurchase(int $quantity, ?ShoppingCart $cart = null): bool
    {
        return $this->stock >= $quantity;
    }
}

use Marshmallow\Ecommerce\Cart\Facades\Cart;

$cart = Cart::get();
$line = $cart->add($product, quantity: 2);
$cart->add($product, quantity: 1, meta: ['size' => 'L']); // meta makes it a separate line

$cart->setQuantity($line, 5);        // 0 removes the line
$cart->remove($line);
$cart->clear();

$cart->applyDiscount($discount);      // throws DiscountException when not allowed
$cart->removeDiscount('CODE');        // one code; no argument clears them all
$cart->discounts();                   // the applied Discount models

$cart->getSubtotal();                 // product lines, gross cents
$cart->getTotalAmount();              // grand total incl. shipping, discount and fees
$cart->getTotalVatAmount();

$cart->confirm();   // freeze the cart for payment; throws while the customer can still act
$cart->reopen();    // after a failed or canceled payment

$url = $cart->startPayment($paymentType); // confirms first, then hands the customer to the provider

$order = $cart->convertToOrder(expectedTotalAmount: $paidCents); // idempotent on the cart id
$order = Order::createFromSnapshot($snapshot, $cart, $payment);   // from a snapshot you kept

$order->markAsCompleted();   // OrderStatusChanged
$order->markAsRefunded();    // OrderStatus::Refunded + OrderRefunded (once)
$cart = $order->toNewCart(); // re-order: fresh cart at current prices, skipping what vanished or sold out

$changed = $cart->refreshPrices(); // repriced lines; fires ItemPriceChanged per line

use Marshmallow\Ecommerce\Cart\Support\Price;

$price = Price::fromGross(12100, 21.0);   // € 121,00 incl. 21% VAT
$price->amountExcludingVat;               // 10000
$price->vatAmount();                      // 2100

Price::fromNet(10000, 21.0);              // same price, built from the net side
$price->multiply(3);                      // line total
$price->percentage(10);                   // basis for a 10% discount
$price->format();                         // "€ 121,00" in the configured locale

$cart->selectShippingMethod($method);   // null clears shipping (e.g. pickup); ShippingMethodSelected
$cart->setFee('Toeslag VISA', Price::fromGross(150, 21));
$cart->setFee('Toeslag VISA', null);    // remove the surcharge again; FeeChanged

// config/cart.php
'shopping_cart' => \App\Models\Shop\ShoppingCart::class,

namespace App\Models\Shop;

class ShoppingCart extends \Marshmallow\Ecommerce\Cart\Models\ShoppingCart
{
    public function hasExcludedShipping(): bool
    {
        return $this->productItems()->every(fn ($line) => $line->meta['digital'] ?? false);
    }
}

Schedule::command('ecommerce:clean-carts')->daily();
bash
composer vendor:publish --tag="cart-config"
php artisan vendor:publish --tag="cart-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="cart-upgrade-migrations"
php artisan migrate