How can i add product to specific category from my own controller

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!
awaidqureshi
Posts: 86
Joined: 12 Jan 2019, 15:17

How can i add product to specific category from my own controller

Post by awaidqureshi » 16 Dec 2019, 15:06

How can i add product to specific category from my own controller

I m using aimeos 2019.10.*

Please guide how can i can do that in new version

Previously i was using this code to do that


$data = array(
'product' => array(
array(
'code' => $productCode,
'list-type' => 'default',
'list-position' => 0,
'list-config' => [],
'list-start' => null,
'list-end' => null,
'list-status' => 1
)
)
);
$this->addProducts($catalogItem->getId(), $data['product'], 'catalog');


and Add function is doing this:
$context = app()->make('\Aimeos\Shop\Base\Context')->get();
$productManager = \Aimeos\MShop::create($context, 'product');
$listManager = \Aimeos\MShop::create($context, $domain . '/lists');


$listItem = $listManager->createItem();
$listItem->setParentId($parentid);
$listItem->setDomain('product');


$codes = [];

foreach ($data as $entry) {
$codes[$entry['code']] = null;
}

$search = $productManager->createSearch()->setSlice(0, 0x7fffffff);
$search->setConditions($search->compare('==', 'product.code', array_keys($codes)));
$products = $productManager->searchItems($search);

foreach ($products as $product) {
$codes[$product->getCode()] = $product->getId();
}

foreach ($data as $entry) {
if (!isset($codes[$entry['code']])) {
throw new \RuntimeException(sprintf('No product for code "%1$s" found', $entry['code']));
}
$listItem->setId(null);
$listItem->setType(7);
$listItem->setDateStart($entry['list-start']);
$listItem->setDateEnd($entry['list-end']);
$listItem->setConfig($entry['list-config']);
$listItem->setPosition($entry['list-position']);
$listItem->setStatus($entry['list-status']);
$listItem->setRefId($codes[$entry['code']]);

$listManager->saveItem($listItem, false);

Thanks

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

Re: How can i add product to specific category from my own controller

Post by aimeos » 16 Dec 2019, 18:40

This looks fishy:

Code: Select all

$listItem->setType(7);
The rest should be OK because there have been no changes in that area.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

awaidqureshi
Posts: 86
Joined: 12 Jan 2019, 15:17

Re: How can i add product to specific category from my own controller

Post by awaidqureshi » 17 Dec 2019, 06:32

aimeos wrote: 16 Dec 2019, 18:40 This looks fishy:

Code: Select all

$listItem->setType(7);
The rest should be OK because there have been no changes in that area.
what should be in this
$listItem->setType(?)

thanks

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

Re: How can i add product to specific category from my own controller

Post by aimeos » 17 Dec 2019, 15:16

It should be either "default" or "promotion".
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

awaidqureshi
Posts: 86
Joined: 12 Jan 2019, 15:17

Re: How can i add product to specific category from my own controller

Post by awaidqureshi » 18 Dec 2019, 06:23

aimeos wrote: 17 Dec 2019, 15:16 It should be either "default" or "promotion".
Thanks for the support
Issue resolved with this

Post Reply