1. Go to this page and download the library: Download daikazu/flexicart 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/ */
// Remove a specific item
Cart::removeItem('item_id');
// Clear all items from the cart
Cart::clear();
// Get all items
$items = Cart::items();
// Get a specific item
$item = Cart::item('item_id');
// Get cart counts
$totalItems = Cart::count(); // Total quantity of all items
$uniqueItems = Cart::uniqueCount(); // Number of unique items
// Check if cart is empty
$isEmpty = Cart::isEmpty();
// Get cart totals
$subtotal = Cart::subtotal(); // Subtotal before conditions
$total = Cart::total(); // Final total after all conditions
$taxableSubtotal = Cart::getTaxableSubtotal(); // Subtotal of taxable items only
use Daikazu\Flexicart\Conditions\Types\PercentageCondition;
use Daikazu\Flexicart\Conditions\Types\FixedCondition;
use Daikazu\Flexicart\Enums\ConditionTarget;
// Add a 10% discount to the cart using condition class
$discount = new PercentageCondition(
name: '10% Off Sale',
value: -10, // Negative for discount
target: ConditionTarget::SUBTOTAL
);
Cart::addCondition($discount);
// Add a $5 shipping fee using condition class
$shipping = new FixedCondition(
name: 'Shipping Fee',
value: 5.00,
target: ConditionTarget::SUBTOTAL
);
Cart::addCondition($shipping);
// Add condition using array format with specific condition class
$memberDiscount = PercentageCondition::make([
'name' => 'Member Discount',
'value' => -15,
'target' => ConditionTarget::SUBTOTAL
]);
Cart::addCondition($memberDiscount);
// Add condition to a specific item
$itemDiscount = new PercentageCondition(
name: 'Item Discount',
value: -20,
target: ConditionTarget::ITEM
);
Cart::addItemCondition('item_id', $itemDiscount);
// Add multiple conditions at once
Cart::addConditions([
new FixedCondition('Processing Fee', 2.50, ConditionTarget::SUBTOTAL),
new PercentageCondition('Bulk Discount', -5, ConditionTarget::SUBTOTAL)
]);
// Remove a specific condition from the cart
Cart::removeCondition('10% Off Sale');
// Remove a condition from a specific item
Cart::removeItemCondition('item_id', 'Item Discount');
// Clear all cart conditions
Cart::clearConditions();
// Sequential calculation (each discount applies to the result of previous discounts)
'compound_discounts' => true,
// Parallel calculation (all discounts apply to the original price)
'compound_discounts' => false,
use Daikazu\Flexicart\Price;
// Create from numeric value
$price = Price::from(29.99);
// Create with specific currency
$price = Price::from(29.99, 'EUR');
// Create from Money object
$money = \Brick\Money\Money::of(29.99, 'USD');
$price = new Price($money);
$price = Price::from(1234.56);
// Get formatted string
$formatted = $price->formatted(); // "$1,234.56"
// Get raw numeric value
$amount = $price->toFloat(); // 1234.56
// Get string representation
$string = (string) $price; // "$1,234.56"
// Get minor value (cents)
$cents = $price->getMinorValue(); // 123456
// Manually persist cart data
Cart::persist();
// Get raw cart data for custom storage
$cartData = Cart::getRawCartData();
// Get a specific cart
$cart = Cart::getCartById('user_123_cart');
// Switch to a different cart
$guestCart = Cart::getCartById('guest_cart');
namespace App\Cart\Conditions;
use Daikazu\Flexicart\Conditions\Condition;
use Daikazu\Flexicart\Enums\ConditionType;
use Daikazu\Flexicart\Price;
class BuyOneGetOneCondition extends Condition
{
public ConditionType $type = ConditionType::PERCENTAGE;
public function calculate(?Price $price = null): Price
{
// Custom calculation logic
// For example, buy one get one 50% off
if ($this->attributes->quantity >= 2) {
$discount = $price->multipliedBy(0.5);
return $discount->multipliedBy(-1); // Negative for discount
}
return Price::from(0);
}
public function formattedValue(): string
{
return 'Buy One Get One 50% Off';
}
}
namespace App\Cart\Storage;
use Daikazu\Flexicart\Contracts\StorageInterface;
class RedisCartStorage implements StorageInterface
{
public function get(string $key): array
{
// Implement Redis get logic
}
public function put(string $key, array $data): void
{
// Implement Redis put logic
}
public function forget(string $key): void
{
// Implement Redis forget logic
}
public function flush(): void
{
// Implement Redis flush logic
}
}