Passed
Push — master ( 9661ae...971496 )
by Aimeos
15:03
created

Select   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 81
dl 0
loc 190
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateProduct() 0 37 4
B getArticle() 0 51 8
B addProduct() 0 64 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2023
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Basket\Decorator;
12
13
14
/**
15
 * Selection product handling
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Select
21
	extends \Aimeos\Controller\Frontend\Basket\Decorator\Base
22
	implements \Aimeos\Controller\Frontend\Basket\Iface, \Aimeos\Controller\Frontend\Common\Decorator\Iface
23
{
24
	/**
25
	 * Adds a product to the basket of the customer stored in the session
26
	 *
27
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product to add including texts, media, prices, attributes, etc.
28
	 * @param float $quantity Amount of products that should by added
29
	 * @param array $variant List of variant-building attribute IDs that identify an article in a selection product
30
	 * @param array $config List of configurable attribute IDs the customer has chosen from
31
	 * @param array $custom Associative list of attribute IDs as keys and arbitrary values that will be added to the ordered product
32
	 * @param string $stocktype Unique code of the stock type to deliver the products from
33
	 * @param string|null $siteId Unique ID of the site the product should be bought from or NULL for site the product is from
34
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
35
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available
36
	 */
37
	public function addProduct( \Aimeos\MShop\Product\Item\Iface $product, float $quantity = 1,
38
		array $variant = [], array $config = [], array $custom = [], string $stocktype = 'default', string $siteId = null
39
	) : \Aimeos\Controller\Frontend\Basket\Iface
40
	{
41
		if( $product->getType() !== 'select' )
42
		{
43
			$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype, $siteId );
44
			return $this;
45
		}
46
47
		$attr = [];
48
		$quantity = $this->call( 'checkQuantity', $product, $quantity );
49
		$prices = $product->getRefItems( 'price', 'default', 'default' );
50
		$hidden = $product->getRefItems( 'attribute', null, 'hidden' );
51
52
		$productItem = $this->getArticle( $product, $variant );
53
		$orderProductItem = \Aimeos\MShop::create( $this->context(), 'order/product' )
54
			->create()
55
			->copyFrom( $product )
56
			->setName( $productItem->getName() )
57
			->setScale( $productItem->getScale() )
58
			->setProductId( $productItem->getId() )
59
			->setParentProductId( $product->getId() )
60
			->setProductCode( $productItem->getCode() );
61
62
		$this->call( 'checkAttributes', [$product, $productItem], 'custom', array_keys( $custom ) );
63
		$this->call( 'checkAttributes', [$product, $productItem], 'config', array_keys( $config ) );
64
65
		if( !( $subprices = $productItem->getRefItems( 'price', 'default', 'default' ) )->isEmpty() ) {
66
			$prices = $subprices;
67
		}
68
69
		if( $mediaItem = $productItem->getRefItems( 'media', 'default', 'default' )->first() ) {
70
			$orderProductItem->setMediaUrl( $mediaItem->getPreview() );
71
		}
72
73
		$hidden->union( $productItem->getRefItems( 'attribute', null, 'hidden' ) );
74
75
		$orderProductAttrManager = \Aimeos\MShop::create( $this->context(), 'order/product/attribute' );
76
		$attributes = $productItem->getRefItems( 'attribute', null, 'variant' );
77
78
		foreach( $this->call( 'getAttributes', $attributes->keys()->toArray(), ['text'] ) as $attrItem ) {
79
			$attr[] = $orderProductAttrManager->create()->copyFrom( $attrItem )->setType( 'variant' );
80
		}
81
82
		$custAttr = $this->call( 'getOrderProductAttributes', 'custom', array_keys( $custom ), $custom );
83
		$confAttr = $this->call( 'getOrderProductAttributes', 'config', array_keys( $config ), [], $config );
84
		$hideAttr = $this->call( 'getOrderProductAttributes', 'hidden', $hidden->keys()->toArray() );
85
86
		$orderProductItem
87
			->setQuantity( $quantity )
88
			->setStockType( $stocktype )
89
			->setAttributeItems( array_merge( $attr, $custAttr, $confAttr, $hideAttr ) );
90
91
		$price = $this->call( 'calcPrice', $orderProductItem, $prices, $quantity );
92
		$orderProductItem
93
			->setPrice( $price )
94
			->setSiteId( $siteId ?: $price->getSiteId() )
95
			->setVendor( $this->getVendor( $siteId ?: $price->getSiteId() ) );
96
97
		$this->getController()->get()->addProduct( $orderProductItem );
98
		$this->getController()->save();
99
100
		return $this;
101
	}
102
103
104
	/**
105
	 * Edits the quantity of a product item in the basket.
106
	 *
107
	 * @param int $position Position number (key) of the order product item
108
	 * @param float $quantity New quantiy of the product item
109
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
110
	 */
111
	public function updateProduct( int $position, float $quantity ) : \Aimeos\Controller\Frontend\Basket\Iface
112
	{
113
		$orderProduct = $this->get()->getProduct( $position );
114
115
		if( $orderProduct->getType() !== 'select' )
116
		{
117
			$this->getController()->updateProduct( $position, $quantity );
118
			return $this;
119
		}
120
121
		$context = $this->context();
122
123
		if( $orderProduct->getFlags() & \Aimeos\MShop\Order\Item\Product\Base::FLAG_IMMUTABLE )
124
		{
125
			$msg = $context->translate( 'controller/frontend', 'Basket item at position "%1$d" cannot be changed' );
126
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $position ) );
127
		}
128
129
		$manager = \Aimeos\MShop::create( $context, 'product' );
130
		$product = $manager->get( $orderProduct->getProductId(), ['price' => ['default']], true );
131
		$product = \Aimeos\MShop::create( $context, 'rule' )->apply( $product, 'catalog' );
132
		$quantity = $this->call( 'checkQuantity', $product, $quantity );
133
134
		if( ( $prices = $product->getRefItems( 'price', 'default', 'default' ) )->isEmpty() )
135
		{
136
			$product = $manager->get( $orderProduct->getParentProductId(), ['price' => ['default']], true );
137
			$product = \Aimeos\MShop::create( $context, 'rule' )->apply( $product, 'catalog' );
138
			$prices = $product->getRefItems( 'price', 'default', 'default' );
139
		}
140
141
		$price = $this->call( 'calcPrice', $orderProduct, $prices, $quantity );
142
		$orderProduct = $orderProduct->setQuantity( $quantity )->setPrice( $price );
143
144
		$this->getController()->get()->addProduct( $orderProduct, $position );
145
		$this->getController()->save();
146
147
		return $this;
148
	}
149
150
151
	/**
152
	 * Returns the variant attributes and updates the price list if necessary.
153
	 *
154
	 * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item which is replaced if necessary
155
	 * @param array $variantAttributeIds List of product variant attribute IDs
156
	 * @return \Aimeos\MShop\Product\Item\Iface Product variant article
157
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If no product variant is found
158
	 */
159
	protected function getArticle( \Aimeos\MShop\Product\Item\Iface $productItem, array $variant ) : \Aimeos\MShop\Product\Item\Iface
160
	{
161
		$items = [];
162
		$context = $this->context();
163
164
		/** controller/frontend/basket/require-variant
165
		 * A variant of a selection product must be chosen
166
		 *
167
		 * Selection products normally consist of several article variants and
168
		 * by default exactly one article variant of a selection product can be
169
		 * put into the basket.
170
		 *
171
		 * By setting this option to false, the selection product including the
172
		 * chosen attributes (if any attribute values were selected) can be put
173
		 * into the basket as well. This makes it possible to get all articles
174
		 * or a subset of articles (e.g. all of a color) at once.
175
		 *
176
		 * This option replace the "client/html/basket/require-variant" setting.
177
		 *
178
		 * @param boolean True if a variant must be chosen, false if also the selection product with attributes can be added
179
		 * @since 2018.01
180
		 * @category Developer
181
		 * @category User
182
		 */
183
		$requireVariant = $context->config()->get( 'controller/frontend/basket/require-variant', true );
184
185
		foreach( $productItem->getRefItems( 'product', null, 'default' ) as $item )
186
		{
187
			foreach( $variant as $id )
188
			{
189
				if( $item->getListItem( 'attribute', 'variant', $id ) === null ) {
190
					continue 2;
191
				}
192
			}
193
194
			$items[] = $item;
195
		}
196
197
		if( count( $items ) > 1 )
198
		{
199
			$msg = $context->translate( 'controller/frontend', 'No unique article found for selected attributes and product ID "%1$s"' );
200
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) );
201
		}
202
203
		if( empty( $items ) && $requireVariant != false ) // count == 0
204
		{
205
			$msg = $context->translate( 'controller/frontend', 'No article found for selected attributes and product ID "%1$s"' );
206
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) );
207
		}
208
209
		return current( $items ) ?: $productItem;
210
	}
211
}
212