Page 1 of 1

Product - Delete Multiple Products

Posted: 15 Aug 2018, 06:27
by mohal_04
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!

Re: Product - Delete Multiple Products

Posted: 15 Aug 2018, 15:31
by aimeos
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)

Re: Product - Delete Multiple Products

Posted: 16 Aug 2018, 05:16
by mohal_04
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!

Re: Product - Delete Multiple Products

Posted: 16 Aug 2018, 08:14
by aimeos

Re: Product - Delete Multiple Products

Posted: 16 Aug 2018, 08:44
by mohal_04
aimeos wrote:Yes, it's already on the list: https://github.com/aimeos/ai-admin-jqadm/issues/23
Great!