ΠΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ ΡΠ°Π±ΠΎΡΡ Ρ ΠΊΠΎΡΠ·ΠΈΠ½ΠΎΠΉ ΠΏΠΎΠΊΡΠΏΠ°ΡΠ΅Π»Ρ
, (*1)
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°
composer require mindy/cart --prefer-dist
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅
ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΊΠΎΡΠ·ΠΈΠ½Ρ
ΠΠ° ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π΄ΠΎΡΡΡΠΏΠ½ΠΎ 2 Ρ
ΡΠ°Π½ΠΈΠ»ΠΈΡΠ° SymfonySessionStorage ΠΈ NativeSessionStorage., (*2)
// Symfony
$session = new Session(new MockArraySessionStorage());
$cart = new Cart(new SymfonySessionStorage($session));
// Native $_SESSION
$cart = new Cart(new NativeSessionStorage());
ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΏΡΠΎΡΡΠΎΠ³ΠΎ ΠΊΠ»Π°ΡΡΠ° ΡΠΎΠ²Π°ΡΠ°, (*3)
<?php
declare(strict_types=1);
use Mindy\Cart\ProductInterface;
class SimpleProduct implements ProductInterface
{
/**
* @var float
*/
protected $price;
/**
* @var string
*/
protected $uniqueId;
/**
* SimpleProduct constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}
/**
* {@inheritdoc}
*/
public function getPrice(): float
{
return (float) $this->price;
}
/**
* {@inheritdoc}
*/
public function getUniqueId(): string
{
return $this->uniqueId;
}
}
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅, (*4)
<?php
// ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
$product = new SimpleProduct(['price' => 100, 'uniqueId' => 'foo']);
$quantity = 2;
$options = ['cpu' => 'xeon', 'memory' => '4'];
$cart->add($product, $quantity, $options);
assert(1, count($cart->all()));
$cart->add($product, $quantity, $options);
assert(2, count($cart->all()));
$cart->add($product, $quantity, $options, true); // ΠΠ°ΠΌΠ΅Π½Π° ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
assert(1, count($cart->all()));
// ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
$cart->has($product, $options);
// Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
$cart->remove($product, $options);
// ΠΠΎΠΈΡΠΊ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
$position = $cart->find($product, $options);
// ΠΡΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ
$cart->all();
// ΠΡΠΈΡΡΠΊΠ°
$cart->clear();
// ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π°
$cart->setQuantity($key, 5);
// ΠΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π°
$cart->setQuantity($key, 5, true);
// ΠΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π° - Π°Π»ΡΡΠ΅ΡΠ½Π°ΡΠΈΠ²Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ
$position = $cart->get($key);
$position->setQuantity($position->getQuantity() + 2);
$cart->replace($key, $position);
License
, (*5)