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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 31 additions & 40 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,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 );
Expand All @@ -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 ) {
Copy link
Owner

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 objects

Copy link
Author

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.

Array
(
    [package-width] => Aimeos\Map Object
        (
            [list:protected] => Array
                (
                    [7] => 15.0
                )

            [sep:protected] => /
        )

)


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();
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:

$attrItem = $orderProduct->getAttributeItem( $type, 'product/property' ) ?: $this->orderAttrManager->create();

}

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

Choose a reason for hiding this comment

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

return $manager->search( $search, ['product/property'] )->col( null, 'product.code' )->getProperties( $types );

Copy link
Author

Choose a reason for hiding this comment

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

getProperties requires a string not an array, does it accept comma separated values? If so we would need to implode $types.

}
}