first commit
This commit is contained in:
242
class/class.CartItem.php
Normal file
242
class/class.CartItem.php
Normal file
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
use KupShop\CatalogBundle\Entity\Wrapper\ProductWrapper;
|
||||
use KupShop\CatalogBundle\ProductList\ProductCollection;
|
||||
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
||||
|
||||
/**
|
||||
* Class representing one item in cart.
|
||||
*
|
||||
* @author Joe
|
||||
*/
|
||||
class CartItemBase extends TemplateAccess
|
||||
{
|
||||
use DatabaseCommunication;
|
||||
|
||||
public $id;
|
||||
public $id_product;
|
||||
public $id_variation;
|
||||
public $note;
|
||||
public $pieces;
|
||||
public $pieces_added;
|
||||
public $product;
|
||||
|
||||
/**
|
||||
* CartItemBase constructor.
|
||||
*/
|
||||
public function __construct($product)
|
||||
{
|
||||
$this->product = $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CartItem[]
|
||||
*/
|
||||
public static function createFromSpec($spec)
|
||||
{
|
||||
$items = sqlQueryBuilder()
|
||||
->select('c.*')
|
||||
->from('cart', 'c')
|
||||
->where($spec)
|
||||
->execute()->fetchAllAssociative();
|
||||
|
||||
if (!$items) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$products = static::getProductCollection($items);
|
||||
|
||||
$result = [];
|
||||
foreach ($items as $data) {
|
||||
$itemKey = $data['id_variation'] ? $data['id_product'].'/'.$data['id_variation'] : $data['id_product'];
|
||||
// fallback pro jistotu
|
||||
if (!($product = ($products[$itemKey] ?? false))) {
|
||||
$product = \Variation::createProductOrVariation($data['id_product'], $data['id_variation']);
|
||||
$product->createFromDB();
|
||||
}
|
||||
|
||||
$item = new static($product);
|
||||
$item->loadFromArray($data);
|
||||
$result[$data['id']] = $item;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function createFromId($id)
|
||||
{
|
||||
return static::createFromSpec(\Query\Operator::equals(['c.id' => $id]));
|
||||
}
|
||||
|
||||
public function loadFromArray($data)
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function fetchProduct()
|
||||
{
|
||||
if ($this->id < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$productWrapper = ServiceContainer::getService(ProductWrapper::class);
|
||||
|
||||
return $productWrapper->setObject($this->product);
|
||||
}
|
||||
|
||||
public function fetchAvailability()
|
||||
{
|
||||
$dbcfg = Settings::getDefault();
|
||||
|
||||
$product = $this->product;
|
||||
|
||||
$InStore = max(0, $product->inStore);
|
||||
|
||||
// Dostupnost
|
||||
$availability = 0; // Vyprodano
|
||||
|
||||
if ($dbcfg->prod_subtract_from_store == 'Y') {
|
||||
if ($this->pieces <= $InStore) {
|
||||
$availability = 1;
|
||||
} // Skladem
|
||||
|
||||
if ($availability == 0 && findModule('products_suppliers')) {
|
||||
$inSuppliers = $product->getInStoreSuppliers($this->id_variation);
|
||||
$product->in_store_suppliers = intval($inSuppliers);
|
||||
|
||||
if ($inSuppliers > 0) {
|
||||
// Je dostatek u dodavatele
|
||||
$InStore += $inSuppliers;
|
||||
if ($this->pieces <= $InStore) {
|
||||
$availability = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($product->deliveryTime == 0) {
|
||||
$availability = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$product->prepareDeliveryText();
|
||||
|
||||
return $availability;
|
||||
}
|
||||
|
||||
public function fetchNote()
|
||||
{
|
||||
$product = $this->product;
|
||||
|
||||
return $product->parseNote($this->note);
|
||||
}
|
||||
|
||||
public function fetchTitle()
|
||||
{
|
||||
$title = $this['product']->title;
|
||||
|
||||
if ($this->id_variation > 0) {
|
||||
$title = Variations::fillInProductTitle($this->id_variation, $title);
|
||||
}
|
||||
|
||||
return $title;
|
||||
}
|
||||
|
||||
public function fetchProductTitle()
|
||||
{
|
||||
return $this['product']->title;
|
||||
}
|
||||
|
||||
public function fetchVariationTitle()
|
||||
{
|
||||
if ($this->id_variation > 0) {
|
||||
return $this['product']->variationTitle;
|
||||
}
|
||||
}
|
||||
|
||||
public function fetchPrice()
|
||||
{
|
||||
$product = $this->product;
|
||||
|
||||
$price = $this->fetchPiecePrice();
|
||||
|
||||
$pieces = toDecimal($this['pieces']);
|
||||
|
||||
return formatPrice($price['value_without_vat']->mul($pieces), $product->vat);
|
||||
}
|
||||
|
||||
public function fetchPiecePrice()
|
||||
{
|
||||
$product = $this->product;
|
||||
|
||||
$pieces = toDecimal($this['pieces']);
|
||||
|
||||
$price = $product->getPrice($this->id_variation, $this->fetchNote(), $pieces);
|
||||
|
||||
$IDcategory = $product['priceLevelSections'] ?? null;
|
||||
$IDproducer = $product->idProducer ?? -1;
|
||||
|
||||
return formatCustomerPrice($price, $product->discount, $product->vat, $product->id, $IDcategory, $IDproducer);
|
||||
}
|
||||
|
||||
public function fetchProducer()
|
||||
{
|
||||
$this['product']->fetchProducer();
|
||||
|
||||
return $this['product']->producer['title'];
|
||||
}
|
||||
|
||||
public function fetchId_producer()
|
||||
{
|
||||
$this['product']->fetchProducer();
|
||||
|
||||
return $this['product']->producer['id'];
|
||||
}
|
||||
|
||||
public function fetchProduct_pieces()
|
||||
{
|
||||
return $this->pieces;
|
||||
}
|
||||
|
||||
public function fetchPieces()
|
||||
{
|
||||
return $this->pieces_added ?: $this->pieces;
|
||||
}
|
||||
|
||||
public function fetchFreeShipping()
|
||||
{
|
||||
$product = $this->product;
|
||||
|
||||
return isset($product->campaign_codes['Z']);
|
||||
}
|
||||
|
||||
public function fetchQuantity()
|
||||
{
|
||||
return $this['pieces'];
|
||||
}
|
||||
|
||||
protected static function getProductCollection(array $cartItems): ProductCollection
|
||||
{
|
||||
$products = [];
|
||||
foreach ($cartItems as $item) {
|
||||
$products[$item['id_product']] = $products[$item['id_product']] ?? null;
|
||||
if ($item['id_variation']) {
|
||||
$products[$item['id_product']][] = $item['id_variation'];
|
||||
}
|
||||
}
|
||||
|
||||
$productList = ServiceContainer::getService(\KupShop\CatalogBundle\ProductList\ProductList::class);
|
||||
$productList->setVariationsAsResult(true);
|
||||
|
||||
$productList->andSpec(\Query\Product::productsAndVariationsIds($products));
|
||||
|
||||
return $productList->getProducts();
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($subclass)) {
|
||||
class CartItem extends CartItemBase
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user