-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add variant support to PropertyAdd plugin #252
base: master
Are you sure you want to change the base?
Changes from 8 commits
77f8aea
a74644e
b1f67c0
688a6bb
29a2fd8
3c25655
5dd0830
07ebf05
a6f0848
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,18 +121,13 @@ public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $acti | |
if( !is_array( $value ) ) | ||
{ | ||
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Order\Item\Base\Product\Iface::class, $value ); | ||
return $this->addAttributes( $value, $this->getProductItems( [$value->getProductId()] ), $types ); | ||
return $this->addAttributes( $value, $this->getProperties( [$value->getProductId()], [$value->getProductCode()], $types ) ); | ||
} | ||
|
||
$list = []; | ||
$ids = map( $value )->getProductId()->unique(); | ||
$codes = map( $value )->getProductCode()->unique(); | ||
|
||
foreach( $value as $orderProduct ) | ||
{ | ||
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Order\Item\Base\Product\Iface::class, $orderProduct ); | ||
$list[] = $orderProduct->getProductId(); | ||
} | ||
|
||
$products = $this->getProductItems( $list ); | ||
$products = $this->getProperties( $ids, $codes, $types ); | ||
|
||
foreach( $value as $key => $orderProduct ) { | ||
$value[$key] = $this->addAttributes( $orderProduct, $products, $types ); | ||
|
@@ -146,54 +141,50 @@ public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $acti | |
* Adds the product properties as attribute items to the order product item | ||
* | ||
* @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderProduct Order product containing attributes | ||
* @param \Aimeos\Map $products List of items implementing \Aimeos\MShop\Product\Item\Iface with IDs as keys and properties | ||
* @param string[] $types List of property types to add | ||
* @param \Aimeos\Map $products list of items implementing \Aimeos\MShop\Product\Item\Iface with IDs as keys and properties | ||
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Modified order product item | ||
*/ | ||
protected function addAttributes( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderProduct, | ||
\Aimeos\Map $products, array $types ) : \Aimeos\MShop\Order\Item\Base\Product\Iface | ||
\Aimeos\Map $properties ) : \Aimeos\MShop\Order\Item\Base\Product\Iface | ||
{ | ||
if( ( $product = $products->get( $orderProduct->getProductId() ) ) === null ) { | ||
return $orderProduct; | ||
} | ||
|
||
foreach( $types as $type ) | ||
{ | ||
$list = $product->getProperties( $type ); | ||
$properties->each(function ( $attributes, $type ) use ( &$orderProduct ) { | ||
|
||
if( !$list->isEmpty() ) | ||
{ | ||
if( ( $attrItem = $orderProduct->getAttributeItem( $type, 'product/property' ) ) === null ) { | ||
$attrItem = $this->orderAttrManager->create(); | ||
} | ||
if ( ($attrItem = $orderProduct->getAttributeItem($type, 'product/property')) === null ) { | ||
$attrItem = $this->orderAttrManager->create(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can shorten this to:
|
||
} | ||
|
||
$attrItem = $attrItem->setType( 'product/property' )->setCode( $type ) | ||
->setValue( count( $list ) > 1 ? $list->toArray() : $list->first() ); | ||
$attrItem = $attrItem->setType('product/property')->setCode($type) | ||
->setValue(count($attributes) > 1 ? $attributes->toArray() : $attributes->first()); | ||
|
||
$orderProduct = $orderProduct->setAttributeItem( $attrItem ); | ||
} | ||
} | ||
$orderProduct = $orderProduct->setAttributeItem($attrItem); | ||
}); | ||
|
||
return $orderProduct; | ||
} | ||
|
||
|
||
/** | ||
* Returns the product items for the given product IDs limited by the map of properties | ||
* Returns the product properties for the given product IDs and codes limited by the map of properties | ||
* | ||
* @param string[] $productIds List of product IDs | ||
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Product\Item\Iface with IDs as keys | ||
* @param string[] $filters Key/value pairs of product IDs and codes | ||
* @return \Aimeos\Map list of product properties | ||
*/ | ||
protected function getProductItems( array $productIds ) : \Aimeos\Map | ||
protected function getProperties( iterable $productIds, iterable $productCodes, array $types ) : \Aimeos\Map | ||
{ | ||
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' ); | ||
$manager = \Aimeos\MShop::create($this->getContext(), 'product'); | ||
$search = $manager->filter( true ); | ||
$expr = [ | ||
$search->compare( '==', 'product.id', array_unique( $productIds ) ), | ||
$search->getConditions(), | ||
]; | ||
$search->setConditions( $search->and( $expr ) ); | ||
|
||
return $manager->search( $search, ['product/property'] ); | ||
$search->add( $search->or( [ | ||
$search->is( 'product.id', '==', $productIds ), | ||
$search->is( 'product.code', '==', $productCodes ), | ||
] ) ); | ||
|
||
$items = $manager->search( $search, ['product/property'] ); | ||
|
||
return map( $types )->map( function ( $type ) use ( $items ) { | ||
if( !( $properties = $items->getProperties($type)->collapse() )->empty() ) { | ||
return [ $type => $properties ]; | ||
} | ||
} )->filter()->collapse(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be sufficient to get a product code index and the properties as list of items:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is
$type
comming from?&
isn't necessary for objectsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$type
is the key in the properties map, it's the type the user configures in the admin for the plugin.An example of the properties passed into
addAttributes
is below.