-
Notifications
You must be signed in to change notification settings - Fork 111
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 3 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,19 @@ 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 = []; | ||
|
||
foreach( $value as $orderProduct ) | ||
{ | ||
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Order\Item\Base\Product\Iface::class, $orderProduct ); | ||
$list[] = $orderProduct->getProductId(); | ||
} | ||
\Aimeos\MW\Common\Base::checkClass(\Aimeos\MShop\Order\Item\Base\Product\Iface::class, $orderProduct); | ||
$list['code'][] = $orderProduct->getProductCode(); | ||
$list['id'][] = $orderProduct->getProductId(); | ||
} | ||
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:
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 still need to keep the checks for orderProduct(s)? ie. can do you want me to also remove the loop? 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. Don't think they are really necessary 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 is sorcery...it's pretty cool to be able to call a method on an array of objects directly from the list. 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. jQuery style ;-) |
||
|
||
$products = $this->getProductItems( $list ); | ||
$products = $this->getProductItems( $list['id'], $list['code'] ); | ||
|
||
foreach( $value as $key => $orderProduct ) { | ||
$value[$key] = $this->addAttributes( $orderProduct, $products, $types ); | ||
|
@@ -157,9 +158,21 @@ protected function addAttributes( \Aimeos\MShop\Order\Item\Base\Product\Iface $o | |
return $orderProduct; | ||
} | ||
|
||
if($products->count() > 1) { | ||
$variant = $products->find( | ||
function ( \Aimeos\MShop\Product\Item\Iface $item ) use ( $orderProduct ) : bool { | ||
return $item->getCode() === $orderProduct->getProductCode(); | ||
} | ||
); | ||
} | ||
|
||
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. It's more efficient if you use 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. Nice! Im used to using laravel collections and find myself digging through your docs for functions that are similar that I can use. 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 Map methods are very close to Laravel, only if standard PHP uses another name ( |
||
foreach( $types as $type ) | ||
{ | ||
$list = $product->getProperties( $type ); | ||
|
||
if( $variant ?? false) { | ||
$list->union( $variant->getProperties( $type ) ); | ||
} | ||
|
||
if( !$list->isEmpty() ) | ||
{ | ||
|
@@ -181,15 +194,15 @@ 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[] $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 $productCodes ) : \Aimeos\Map | ||
{ | ||
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' ); | ||
$search = $manager->filter( true ); | ||
$expr = [ | ||
$search->compare( '==', 'product.id', array_unique( $productIds ) ), | ||
$search->compare( '==', 'product.code', array_unique( $productCodes ) ), | ||
$search->getConditions(), | ||
]; | ||
$search->setConditions( $search->and( $expr ) ); | ||
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 don't use both, IDs and codes 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. My bad, I never copied my changes over, im copying my edits from editor to the browser. Im trying to find more info on how I can run the test suite using phing, I just setup the environment so it has some data to work against. |
||
|
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.
Would be better to use
$ids
and$codes
instead of a two-dimensional array