-
Notifications
You must be signed in to change notification settings - Fork 112
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 6 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->getProductItems( [$value->getProductId()], [$value->getProductCode()]), $types ); | ||
} | ||
|
||
$list = []; | ||
$ids = map( $value )->getProductId()->toArray(); | ||
$codes = map( $value )->getProductCode()->toArray(); | ||
|
||
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->getProductItems( $ids, $codes ); | ||
|
||
foreach( $value as $key => $orderProduct ) { | ||
$value[$key] = $this->addAttributes( $orderProduct, $products, $types ); | ||
|
@@ -157,10 +152,19 @@ protected function addAttributes( \Aimeos\MShop\Order\Item\Base\Product\Iface $o | |
return $orderProduct; | ||
} | ||
|
||
if( $products->count() > 1 ) { | ||
$variant = $products->col( null, 'product.code' ) | ||
->get( $orderProduct->getProductCode() ); | ||
} | ||
|
||
foreach( $types as $type ) | ||
{ | ||
$list = $product->getProperties( $type ); | ||
|
||
if( $variant ?? false ) { | ||
$list->union( $variant->getProperties( $type ) ); | ||
} | ||
|
||
if( !$list->isEmpty() ) | ||
{ | ||
if( ( $attrItem = $orderProduct->getAttributeItem( $type, 'product/property' ) ) === null ) { | ||
|
@@ -181,18 +185,19 @@ protected function addAttributes( \Aimeos\MShop\Order\Item\Base\Product\Iface $o | |
/** | ||
* Returns the product items for the given product IDs limited by the map of properties | ||
* | ||
* @param string[] $productIds List of product IDs | ||
* @param string[] $productsIds List of product IDs | ||
* @param string[] $productCodes List of product codes | ||
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Product\Item\Iface with IDs as keys | ||
*/ | ||
protected function getProductItems( array $productIds ) : \Aimeos\Map | ||
protected function getProductItems( array $productIds, array $productCodes ) : \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 ) ); | ||
|
||
$search->add( $search->or( [ | ||
$search->is( 'product.id', '==', array_unique( $productIds ) ), | ||
$search->is( 'product.code', '==', array_unique( $productCodes ) ), | ||
] ) ); | ||
|
||
return $manager->search( $search, ['product/property'] ); | ||
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. We could save a lot of 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. The method should probably change to reflect what it returns then.
I guess we would also want to filter the attributes by the types configured here also, which means passing the types to method. Do you not think passing an array of code/ids into 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. We can also pass IDs and codes separately and return a map of product.code as keys and property items as values. 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. Do we need to return the map keyed as you described? The changes ive made locally within the now labelled I map over the types and return a new map that includes the properties. It returns something like the following: Aimeos\Map {#1780
#list: array:3 [
"package-weight" => Aimeos\Map {#1778
#list: array:1 [
182 => "0.4"
]
}
"package-net-weight" => Aimeos\Map {#1777
#list: array:1 [
190 => "0.4"
]
}
"package-dangerous" => Aimeos\Map {#1772
#list: array:2 [
191 => "0"
192 => "0"
]
}
]
} This is then passed to The tests are still passing for me with these changes - but could be a false positive due to coverage. I can publish my changes If you like? 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. Yes, that should work |
||
} | ||
|
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.
You can use
->unique()
already here and pass the Map objects if you change the method signature of getProductsItems() toiterable
instead ofarray
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.
I did do that initially, but my mistake was type hinting as Aimeos\Map, which would have failed for the call we make on the
addAttributes
method outside of the loop.