Passed
Push — master ( 23b209...e4b48e )
by Aimeos
17:50 queued 08:55
created

Select::updateProduct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 33
rs 9.6666
cc 4
nc 4
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
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
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
34
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available
35
	 */
36
	public function addProduct( \Aimeos\MShop\Product\Item\Iface $product, float $quantity = 1,
37
		array $variant = [], array $config = [], array $custom = [], string $stocktype = 'default'
38
	) : \Aimeos\Controller\Frontend\Basket\Iface
39
	{
40
		if( $product->getType() !== 'select' )
41
		{
42
			$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype );
43
			return $this;
44
		}
45
46
		$attr = [];
47
		$quantity = $this->call( 'checkQuantity', $product, $quantity );
48
		$prices = $product->getRefItems( 'price', 'default', 'default' );
49
		$hidden = $product->getRefItems( 'attribute', null, 'hidden' );
50
51
		$orderBaseProductItem = \Aimeos\MShop::create( $this->context(), 'order/base/product' )->create();
52
		$orderBaseProductItem = $orderBaseProductItem->copyFrom( $product );
53
54
		$productItem = $this->getArticle( $product, $variant );
55
		$orderBaseProductItem->setProductCode( $productItem->getCode() )
56
			->setParentProductId( $product->getId() )
57
			->setProductId( $productItem->getId() );
58
59
		$this->call( 'checkAttributes', [$product, $productItem], 'custom', array_keys( $custom ) );
60
		$this->call( 'checkAttributes', [$product, $productItem], 'config', array_keys( $config ) );
61
62
		if( !( $subprices = $productItem->getRefItems( 'price', 'default', 'default' ) )->isEmpty() ) {
63
			$prices = $subprices;
64
		}
65
66
		if( $mediaItem = $productItem->getRefItems( 'media', 'default', 'default' )->first() ) {
67
			$orderBaseProductItem->setMediaUrl( $mediaItem->getPreview() );
68
		}
69
70
		$hidden->union( $productItem->getRefItems( 'attribute', null, 'hidden' ) );
71
72
		$orderProductAttrManager = \Aimeos\MShop::create( $this->context(), 'order/base/product/attribute' );
73
		$attributes = $productItem->getRefItems( 'attribute', null, 'variant' );
74
75
		foreach( $this->call( 'getAttributes', $attributes->keys()->toArray(), ['text'] ) as $attrItem ) {
76
			$attr[] = $orderProductAttrManager->create()->copyFrom( $attrItem )->setType( 'variant' );
77
		}
78
79
		$custAttr = $this->call( 'getOrderProductAttributes', 'custom', array_keys( $custom ), $custom );
80
		$confAttr = $this->call( 'getOrderProductAttributes', 'config', array_keys( $config ), [], $config );
81
		$hideAttr = $this->call( 'getOrderProductAttributes', 'hidden', $hidden->keys()->toArray() );
82
83
		$orderBaseProductItem->setQuantity( $quantity )->setStockType( $stocktype )
84
			->setAttributeItems( array_merge( $attr, $custAttr, $confAttr, $hideAttr ) )
85
			->setPrice( $this->call( 'calcPrice', $orderBaseProductItem, $prices, $quantity ) );
86
87
		$this->getController()->get()->addProduct( $orderBaseProductItem );
88
		$this->getController()->save();
89
90
		return $this;
91
	}
92
93
94
	/**
95
	 * Edits the quantity of a product item in the basket.
96
	 *
97
	 * @param int $position Position number (key) of the order product item
98
	 * @param float $quantity New quantiy of the product item
99
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
100
	 */
101
	public function updateProduct( int $position, float $quantity ) : \Aimeos\Controller\Frontend\Basket\Iface
102
	{
103
		$orderProduct = $this->get()->getProduct( $position );
104
105
		if( $orderProduct->getType() !== 'select' )
106
		{
107
			$this->getController()->updateProduct( $position, $quantity );
108
			return $this;
109
		}
110
111
		if( $orderProduct->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE )
112
		{
113
			$msg = $this->context()->translate( 'controller/frontend', 'Basket item at position "%1$d" cannot be changed' );
114
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $position ) );
115
		}
116
117
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
118
		$product = $manager->get( $orderProduct->getProductId(), ['price' => ['default']], true );
119
		$quantity = $this->call( 'checkQuantity', $product, $quantity );
120
121
		if( ( $prices = $product->getRefItems( 'price', 'default', 'default' ) )->isEmpty() )
122
		{
123
			$prices = $manager->get( $orderProduct->getParentProductId(), ['price' => ['default']], true )
124
				->getRefItems( 'price', 'default', 'default' );
125
		}
126
127
		$price = $this->call( 'calcPrice', $orderProduct, $prices, $quantity );
128
		$orderProduct = $orderProduct->setQuantity( $quantity )->setPrice( $price );
129
130
		$this->getController()->get()->addProduct( $orderProduct, $position );
131
		$this->getController()->save();
132
133
		return $this;
134
	}
135
136
137
	/**
138
	 * Returns the variant attributes and updates the price list if necessary.
139
	 *
140
	 * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item which is replaced if necessary
141
	 * @param array $variantAttributeIds List of product variant attribute IDs
142
	 * @return \Aimeos\MShop\Product\Item\Iface Product variant article
143
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If no product variant is found
144
	 */
145
	protected function getArticle( \Aimeos\MShop\Product\Item\Iface $productItem, array $variant ) : \Aimeos\MShop\Product\Item\Iface
146
	{
147
		$items = [];
148
		$context = $this->context();
149
150
		/** controller/frontend/basket/require-variant
151
		 * A variant of a selection product must be chosen
152
		 *
153
		 * Selection products normally consist of several article variants and
154
		 * by default exactly one article variant of a selection product can be
155
		 * put into the basket.
156
		 *
157
		 * By setting this option to false, the selection product including the
158
		 * chosen attributes (if any attribute values were selected) can be put
159
		 * into the basket as well. This makes it possible to get all articles
160
		 * or a subset of articles (e.g. all of a color) at once.
161
		 *
162
		 * This option replace the "client/html/basket/require-variant" setting.
163
		 *
164
		 * @param boolean True if a variant must be chosen, false if also the selection product with attributes can be added
165
		 * @since 2018.01
166
		 * @category Developer
167
		 * @category User
168
		 */
169
		$requireVariant = $context->config()->get( 'controller/frontend/basket/require-variant', true );
170
171
		foreach( $productItem->getRefItems( 'product', null, 'default' ) as $item )
172
		{
173
			foreach( $variant as $id )
174
			{
175
				if( $item->getListItem( 'attribute', 'variant', $id ) === null ) {
176
					continue 2;
177
				}
178
			}
179
180
			$items[] = $item;
181
		}
182
183
		if( count( $items ) > 1 )
184
		{
185
			$msg = $context->translate( 'controller/frontend', 'No unique article found for selected attributes and product ID "%1$s"' );
186
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) );
187
		}
188
189
		if( empty( $items ) && $requireVariant != false ) // count == 0
190
		{
191
			$msg = $context->translate( 'controller/frontend', 'No article found for selected attributes and product ID "%1$s"' );
192
			throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) );
193
		}
194
195
		return current( $items ) ?: $productItem;
196
	}
197
}
198