Bundle::addProduct()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 40
rs 9.1768
c 0
b 0
f 0
cc 5
nc 2
nop 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Basket\Decorator;
12
13
14
/**
15
 * Bundle product handling
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Bundle
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 $supplierid Unique supplier ID the product is from
34
	 * @param string|null $siteid Unique site ID the product is from or null for siteid of the product item
35
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
36
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available
37
	 */
38
	public function addProduct( \Aimeos\MShop\Product\Item\Iface $product, float $quantity = 1,
39
		array $variant = [], array $config = [], array $custom = [], string $stocktype = 'default', ?string $siteId = null
40
	) : \Aimeos\Controller\Frontend\Basket\Iface
41
	{
42
		if( $product->getType() !== 'bundle' )
43
		{
44
			$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype, $siteId );
45
			return $this;
46
		}
47
48
		$quantity = $this->call( 'checkQuantity', $product, $quantity );
49
		$this->call( 'checkAttributes', [$product], 'custom', array_keys( $custom ) );
50
		$this->call( 'checkAttributes', [$product], 'config', array_keys( $config ) );
51
52
		$prices = $product->getRefItems( 'price', 'default', 'default' );
53
		$hidden = $product->getRefItems( 'attribute', null, 'hidden' );
54
55
		$custAttr = $this->call( 'getOrderProductAttributes', 'custom', array_keys( $custom ), $custom );
56
		$confAttr = $this->call( 'getOrderProductAttributes', 'config', array_keys( $config ), [], $config );
57
		$hideAttr = $this->call( 'getOrderProductAttributes', 'hidden', $hidden->keys()->toArray() );
58
59
		$orderProductItem = \Aimeos\MShop::create( $this->context(), 'order' )
60
			->createProduct()
0 ignored issues
show
Bug introduced by
The method createProduct() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
			->/** @scrutinizer ignore-call */ createProduct()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
			->copyFrom( $product )
62
			->setQuantity( $quantity )
63
			->setStockType( $stocktype )
64
			->setSiteId( $siteId ?: $product->getSiteId() )
65
			->setAttributeItems( array_merge( $custAttr, $confAttr, $hideAttr ) )
66
			->setProducts( $this->getBundleProducts( $product, $quantity, $stocktype ) );
67
68
		$price = $this->call( 'calcPrice', $orderProductItem, $prices, $quantity );
69
		$orderProductItem
70
			->setPrice( $price )
71
			->setSiteId( $siteId ?: $price->getSiteId() )
72
			->setVendor( $this->getVendor( $siteId ?: $price->getSiteId() ) );
73
74
		$this->getController()->get()->addProduct( $orderProductItem );
75
		$this->getController()->save();
76
77
		return $this;
78
	}
79
80
81
	/**
82
	 * Adds the bundled products to the order product item.
83
	 *
84
	 * @param \Aimeos\MShop\Product\Item\Iface $product Bundle product item
85
	 * @param float $quantity Amount of products that should by added
86
	 * @param string $stocktype Unique code of the stock type to deliver the products from
87
	 * @return \Aimeos\MShop\Order\Item\Product\Iface[] List of order product item from bundle
88
	 */
89
	protected function getBundleProducts( \Aimeos\MShop\Product\Item\Iface $product, float $quantity, string $stocktype ) : array
90
	{
91
		$orderProducts = [];
92
		$orderManager = \Aimeos\MShop::create( $this->context(), 'order' );
93
94
		foreach( $product->getRefItems( 'product', null, 'default' ) as $item )
95
		{
96
			$prices = $item->getRefItems( 'price', 'default', 'default' );
97
			$orderProduct = $orderManager->createProduct()
98
				->copyFrom( $item )
99
				->setStockType( $stocktype )
100
				->setParentProductId( $product->getId() );
101
102
			$orderProducts[] = $orderProduct->setPrice( $this->call( 'calcPrice', $orderProduct, $prices, $quantity ) );
103
		}
104
105
		return $orderProducts;
106
	}
107
}
108