Get category of bought products (default and select)

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
columbo
Advanced
Posts: 151
Joined: 09 Oct 2019, 09:42

Get category of bought products (default and select)

Post by columbo » 15 Jul 2024, 20:56

Hi,

I'm trying to create a 'my bought products' page where all products ever bought by a customer are grouped by 1st level category names. The selection of the default-articles works fine so far.

But I'm having problems with selection-articles:
Selection-products are bought as "default-product", but there is no category assignment.
The category is assgined on select-product level.

I have already made several attempts (see A and B below) but eihter category name is missing or the default-product of the selection-product.

How can I query the products (default and selection) with category, price and media?

Code: Select all

//get customer id
$custid = \Aimeos\Controller\Frontend::create($context, 'customer')->get();

//query orders from current customer
$manager = \Aimeos\MShop::create($context, 'order' , 'Standard');
$filter = $manager->filter()->add('order.customerid', '==', $custid);
$resultOrders = $manager->search($filter)->getId();

//query products from oders
$managerOrderProducts = \Aimeos\MShop::create($context, 'order/product', 'Standard' );
$filterOrderProducts = $managerOrderProducts->filter()->add('order.product.parentid', '==', $resultOrders);
$resultOrderProducts = $managerOrderProducts->search($filterOrderProducts); //


//get catalog, price and media domains
$managerProducts = \Aimeos\MShop::create($context, 'product' );
$filterProducts = $managerProducts->filter();

//attempt A
$filterProducts->add('product.id', '==', $resultOrderProducts->getProductId());

//attempt B:		
$filterProducts->add( $filter->or( [    
    $filter->and( [
        $filter->is( 'product.id', '==', $resultOrderProducts->getProductId() ),
        $filter->is( 'product.type', '==', 'default' ),
    ] ),
    $filter->and( [
        $filter->is( 'product.id', '==', $resultOrderProducts->getParentProductId() ),
        $filter->is( 'product.type', '==', 'select' ),
    ] ),    
] ) );

$resultProducts = $managerProducts->search($filterProducts, ['catalog', 'price', 'media']);


// group favorite items by 1st level category
$listItems = $resultProducts->groupBy( function( $item, $key ) {
    $name ='';
    if (($productItem = $item->getRefItems('catalog')) !== null) {
        $name = $item->getRefItems( 'catalog')->where( 'level', '==', '1' )->getName()->first();				 
    }
    return $name;			 
})->ksort();


Thank you!

User avatar
aimeos
Administrator
Posts: 8378
Joined: 01 Jan 1970, 00:00

Re: Get category of bought products (default and select)

Post by aimeos » 17 Jul 2024, 11:45

Use variant B and add 'product' to the list of items that should be fetched so you get all variant articles for the selection products too. The more efficient way would be variant A and fetch the corresponding selection products in a second query.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 151
Joined: 09 Oct 2019, 09:42

Re: Get category of bought products (default and select)

Post by columbo » 17 Jul 2024, 20:44

Thank you for your reply, but unfortunately it doesn't work yet.
added 'product' to the selected domains:

Code: Select all

...
$filterProducts->add( $filter->or( [    
	$filter->and( [
		$filter->is( 'product.id', '==', $resultOrderProducts->getProductId() ),
		$filter->is( 'product.type', '==', 'default' ),
	] ),
	$filter->and( [
		$filter->is( 'product.id', '==', $resultOrderProducts->getParentProductId() ),
		$filter->is( 'product.type', '==', 'select' ),
	] ),    
] ) );

$resultProducts = $managerProducts->search($filterProducts, ['product','catalog', 'price', 'media']);
Selection item (product.type: select):
code: 1000

with 2 variants (product.type: default):
code: 1000-1 and
code: 1000-2

item with code 1000-1 is the one that was purchased and should be listed

For product code 1000-1 media and price data are present, but catalog data are still missing.
What is missing or what am I doing wrong?

User avatar
aimeos
Administrator
Posts: 8378
Joined: 01 Jan 1970, 00:00

Re: Get category of bought products (default and select)

Post by aimeos » 19 Jul 2024, 08:29

The categories are only in the selection product with code "1000"
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply