Product - Delete Multiple Products

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!
mohal_04
Advanced
Posts: 108
Joined: 27 Mar 2018, 05:59

Product - Delete Multiple Products

Post by mohal_04 » 15 Aug 2018, 06:27

Laravel: 5.6
Aimeos: 2018.04
PHP: 7.1.18

Hi,

I need to delete more than 1 product from complex Aimeos DB schema. Can you please, guide me how I can do this by just one click? Or if there is any other method of deleting multiple products based on their ID or code?

Please, reply!

Thanks!

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

Re: Product - Delete Multiple Products

Post by aimeos » 15 Aug 2018, 15:31

You can easily delete multiple products by using deleteItems() of the product manager. This method accepts a list of product IDs.

If you want to clean up everything that is associated to the products (categories, images, prices, texts), you can use this code:

Code: Select all

$productItems = [...];

$catalogListManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' );

$search = $catalogListManager->createSearch()->setSlice( 0, 0x7fffffff );
$expr = [
    $search->compare( '==', 'catalog.lists.refid', array_keys( $productItems ) ),
    $search->compare( '==', 'catalog.lists.domain', 'product' ),
];
$search->setConditions( $search->combine( '&&', $expr ) );

$listItems = $catalogListManager->searchItems( $search );
$catalogListManager->deleteItems( array_keys( $listItems ) );

$productManager = \Aimeos\MShop\Factory::createManager( $context, 'product' );

foreach( $productItems as $item ) {
    $item->deleteListItems( $item->getListItems( ['media', 'price', 'text'], null, null, false ), true );
    $productManager->saveItem( $item ); // deletes are referenced media, price and text items
}

$productManager->deleteItems( array_keys( $productItems ) );
Beware: This does only work since 2018.07 (current stable version)
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

mohal_04
Advanced
Posts: 108
Joined: 27 Mar 2018, 05:59

Re: Product - Delete Multiple Products

Post by mohal_04 » 16 Aug 2018, 05:16

aimeos wrote:You can easily delete multiple products by using deleteItems() of the product manager. This method accepts a list of product IDs.

If you want to clean up everything that is associated to the products (categories, images, prices, texts), you can use this code:

Code: Select all

$productItems = [...];

$catalogListManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' );

$search = $catalogListManager->createSearch()->setSlice( 0, 0x7fffffff );
$expr = [
    $search->compare( '==', 'catalog.lists.refid', array_keys( $productItems ) ),
    $search->compare( '==', 'catalog.lists.domain', 'product' ),
];
$search->setConditions( $search->combine( '&&', $expr ) );

$listItems = $catalogListManager->searchItems( $search );
$catalogListManager->deleteItems( array_keys( $listItems ) );

$productManager = \Aimeos\MShop\Factory::createManager( $context, 'product' );

foreach( $productItems as $item ) {
    $item->deleteListItems( $item->getListItems( ['media', 'price', 'text'], null, null, false ), true );
    $productManager->saveItem( $item ); // deletes are referenced media, price and text items
}

$productManager->deleteItems( array_keys( $productItems ) );
Beware: This does only work since 2018.07 (current stable version)
Hi,

Thanks for the help! It may take me some time to understand all this but hopefully, I shall make it happen. I would suggest though, if you guys can give an option in future release to delete more than one product on Product List page then it would be great.

Thanks!

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

Re: Product - Delete Multiple Products

Post by aimeos » 16 Aug 2018, 08:14

Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

mohal_04
Advanced
Posts: 108
Joined: 27 Mar 2018, 05:59

Re: Product - Delete Multiple Products

Post by mohal_04 » 16 Aug 2018, 08:44

aimeos wrote:Yes, it's already on the list: https://github.com/aimeos/ai-admin-jqadm/issues/23
Great!

Post Reply