Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/mshoplib/src/MShop/Plugin/Provider/Order/PropertyAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Copy link
Owner

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


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();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can shorten this to:

$ids = map( $value )->getProductId();
$codes = map( $value )->getProductCode();

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think they are really necessary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can shorten this to:

$ids = map( $value )->getProductId();
$codes = map( $value )->getProductCode();

This is sorcery...it's pretty cool to be able to call a method on an array of objects directly from the list.

Copy link
Owner

Choose a reason for hiding this comment

The 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 );
Expand All @@ -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();
}
);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more efficient if you use $map = $products->col( null, 'product.code' ); first and then access the products by code

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The 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 (array_column() instead of Laravel's pluck()), then the Map methods follow the PHP naming.

foreach( $types as $type )
{
$list = $product->getProperties( $type );

if( $variant ?? false) {
$list->union( $variant->getProperties( $type ) );
}

if( !$list->isEmpty() )
{
Expand All @@ -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 ) );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't use both, IDs and codes

Copy link
Author

Choose a reason for hiding this comment

The 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.

Expand Down