Skip to content

Commit a6f0848

Browse files
author
Mart Dingley
committed
Move logic from getProperties into addAttributes
1 parent 07ebf05 commit a6f0848

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/mshoplib/src/MShop/Plugin/Provider/Order/PropertyAdd.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $acti
121121
if( !is_array( $value ) )
122122
{
123123
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Order\Item\Base\Product\Iface::class, $value );
124-
return $this->addAttributes( $value, $this->getProperties( [$value->getProductId()], [$value->getProductCode()], $types ) );
124+
return $this->addAttributes( $value, $this->getProperties( [$value->getProductId()], [$value->getProductCode()], $types ), $types );
125125
}
126126

127127
$ids = map( $value )->getProductId()->unique();
@@ -145,19 +145,25 @@ public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $acti
145145
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Modified order product item
146146
*/
147147
protected function addAttributes( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderProduct,
148-
\Aimeos\Map $properties ) : \Aimeos\MShop\Order\Item\Base\Product\Iface
148+
\Aimeos\Map $properties, array $types ) : \Aimeos\MShop\Order\Item\Base\Product\Iface
149149
{
150-
$properties->each(function ( $attributes, $type ) use ( &$orderProduct ) {
150+
if( ( $properties = $properties->get( $orderProduct->getProductCode() ) ) === null ) {
151+
return $orderProduct;
152+
}
151153

152-
if ( ($attrItem = $orderProduct->getAttributeItem($type, 'product/property')) === null ) {
153-
$attrItem = $this->orderAttrManager->create();
154-
}
154+
foreach( $types as $type )
155+
{
156+
if( !$properties->isEmpty() )
157+
{
158+
$attrItem = $orderProduct->getAttributeItem( $type, 'product/property' )
159+
?: $this->orderAttrManager->create();
155160

156-
$attrItem = $attrItem->setType('product/property')->setCode($type)
157-
->setValue(count($attributes) > 1 ? $attributes->toArray() : $attributes->first());
161+
$attrItem = $attrItem->setType( 'product/property' )->setCode( $type )
162+
->setValue( count( $properties ) > 1 ? $properties->toArray() : $properties->first() );
158163

159-
$orderProduct = $orderProduct->setAttributeItem($attrItem);
160-
});
164+
$orderProduct = $orderProduct->setAttributeItem( $attrItem );
165+
}
166+
}
161167

162168
return $orderProduct;
163169
}
@@ -179,12 +185,8 @@ protected function getProperties( iterable $productIds, iterable $productCodes,
179185
$search->is( 'product.code', '==', $productCodes ),
180186
] ) );
181187

182-
$items = $manager->search( $search, ['product/property'] );
183-
184-
return map( $types )->map( function ( $type ) use ( $items ) {
185-
if( !( $properties = $items->getProperties($type)->collapse() )->empty() ) {
186-
return [ $type => $properties ];
187-
}
188-
} )->filter()->collapse(1);
188+
return $manager->search( $search, ['product/property'] )
189+
->col( null, 'product.code' )
190+
->getProperties( implode( ',', $types ) );
189191
}
190192
}

0 commit comments

Comments
 (0)