/
Category.php
81 lines (67 loc) · 3.04 KB
/
Category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2016-2024
* @package Controller
* @subpackage Frontend
*/
namespace Aimeos\Controller\Frontend\Basket\Decorator;
/**
* Category check for basket controller
*
* @package Controller
* @subpackage Frontend
*/
class Category
extends Base
implements \Aimeos\Controller\Frontend\Basket\Iface, \Aimeos\Controller\Frontend\Common\Decorator\Iface
{
/**
* Adds a product to the basket of the customer stored in the session
*
* @param \Aimeos\MShop\Product\Item\Iface $product Product to add including texts, media, prices, attributes, etc.
* @param float $quantity Amount of products that should by added
* @param array $variant List of variant-building attribute IDs that identify an article in a selection product
* @param array $config List of configurable attribute IDs the customer has chosen from
* @param array $custom Associative list of attribute IDs as keys and arbitrary values that will be added to the ordered product
* @param string $stocktype Unique code of the stock type to deliver the products from
* @param string|null $siteId Unique ID of the site the product should be bought from or NULL for site the product is from
* @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
* @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available
*/
public function addProduct( \Aimeos\MShop\Product\Item\Iface $product, float $quantity = 1,
array $variant = [], array $config = [], array $custom = [], string $stocktype = 'default', string $siteId = null
) : \Aimeos\Controller\Frontend\Basket\Iface
{
if( $product->getListItems( 'catalog' )->isEmpty() )
{
$context = $this->context();
$manager = \Aimeos\MShop::create( $context, 'product' );
$filter = $manager->filter( true );
$func = $filter->make( 'product:has', ['product', 'default', $product->getId()] );
$filter->add( $filter->is( $func, '!=', null ) );
$prodIds = $manager->search( $filter )->keys()->all();
if( empty( $prodIds ) || !$this->checkCategory( $prodIds ) )
{
$msg = $context->translate( 'controller/frontend', 'Adding product with ID "%1$s" is not allowed' );
throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $product->getId() ) );
}
}
$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype, $siteId );
return $this;
}
/**
* Checks if the given product IDs are assigned to a category
*
* @param iterable $prodIds Unique product IDs to check for
* @return bool True if at least one product ID is assigned to a category, false if not
*/
protected function checkCategory( iterable $prodIds ) : bool
{
$manager = \Aimeos\MShop::create( $this->context(), 'product' );
$filter = $manager->filter( true )->slice( 0, 1 );
$func = $filter->make( 'product:has', ['catalog'] );
$filter->add( $func, '!=', null )->add( 'product.id', '==', $prodIds );
return !$manager->search( $filter )->isEmpty();
}
}