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/ */
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