Magento2: Not in value Category ids product collection issue

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Product collection issue



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');

products = $productCollection->addAttributeToSelect('*')
->addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))
->setOrder('price', 'ASC')
->load();









share|improve this question























  • What issue you are facing here?
    – aman
    1 hour ago










  • @aman going to php session close function
    – Masud Shaikh
    1 hour ago
















up vote
1
down vote

favorite












Product collection issue



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');

products = $productCollection->addAttributeToSelect('*')
->addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))
->setOrder('price', 'ASC')
->load();









share|improve this question























  • What issue you are facing here?
    – aman
    1 hour ago










  • @aman going to php session close function
    – Masud Shaikh
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Product collection issue



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');

products = $productCollection->addAttributeToSelect('*')
->addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))
->setOrder('price', 'ASC')
->load();









share|improve this question















Product collection issue



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');

products = $productCollection->addAttributeToSelect('*')
->addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))
->setOrder('price', 'ASC')
->load();






magento2 category-products






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Amit Bera♦

54k1366157




54k1366157










asked 1 hour ago









Masud Shaikh

7810




7810











  • What issue you are facing here?
    – aman
    1 hour ago










  • @aman going to php session close function
    – Masud Shaikh
    1 hour ago
















  • What issue you are facing here?
    – aman
    1 hour ago










  • @aman going to php session close function
    – Masud Shaikh
    1 hour ago















What issue you are facing here?
– aman
1 hour ago




What issue you are facing here?
– aman
1 hour ago












@aman going to php session close function
– Masud Shaikh
1 hour ago




@aman going to php session close function
– Masud Shaikh
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










To fetch product collection by categories you can try this code:



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');

$categories = [2,3,5];//category ids array

$collection = $productCollection ->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['nin' => $categories]);
$collection->setPageSize(10)
print_r($collection->getData());





share|improve this answer






















  • I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
    – Masud Shaikh
    1 hour ago










  • Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
    – aman
    1 hour ago










  • Its worked thanks @aman
    – Masud Shaikh
    1 hour ago

















up vote
1
down vote













First, you to have to check why the parameter is not working



If you check addCategoriesFilter function definition, then you will find it support a parameter which is an array. the parameter is a one-dimensional array which supports index as condition Type and value as filter value.Like



$categoriesFilter = 
[
'in' => [10,15],
'nin' => [12,18]
]


So,you have to change




addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))



to




addCategoriesFilter(array('nin' => $currentCategoryId))




 public function addCategoriesFilter(array $categoriesFilter)

foreach ($categoriesFilter as $conditionType => $values)
$categorySelect = $this->getConnection()->select()->from(
['cat' => $this->getTable('catalog_category_product')],
'cat.product_id'
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
$selectCondition = [
$this->mapConditionType($conditionType) => $categorySelect
];
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id', $selectCondition));

return $this;






share|improve this answer






















  • It is worked fine Thanks! @amit
    – Masud Shaikh
    1 hour ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242234%2fmagento2-not-in-value-category-ids-product-collection-issue%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










To fetch product collection by categories you can try this code:



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');

$categories = [2,3,5];//category ids array

$collection = $productCollection ->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['nin' => $categories]);
$collection->setPageSize(10)
print_r($collection->getData());





share|improve this answer






















  • I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
    – Masud Shaikh
    1 hour ago










  • Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
    – aman
    1 hour ago










  • Its worked thanks @aman
    – Masud Shaikh
    1 hour ago














up vote
3
down vote



accepted










To fetch product collection by categories you can try this code:



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');

$categories = [2,3,5];//category ids array

$collection = $productCollection ->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['nin' => $categories]);
$collection->setPageSize(10)
print_r($collection->getData());





share|improve this answer






















  • I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
    – Masud Shaikh
    1 hour ago










  • Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
    – aman
    1 hour ago










  • Its worked thanks @aman
    – Masud Shaikh
    1 hour ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted






To fetch product collection by categories you can try this code:



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');

$categories = [2,3,5];//category ids array

$collection = $productCollection ->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['nin' => $categories]);
$collection->setPageSize(10)
print_r($collection->getData());





share|improve this answer














To fetch product collection by categories you can try this code:



$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');

$categories = [2,3,5];//category ids array

$collection = $productCollection ->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['nin' => $categories]);
$collection->setPageSize(10)
print_r($collection->getData());






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









aman

55525




55525











  • I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
    – Masud Shaikh
    1 hour ago










  • Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
    – aman
    1 hour ago










  • Its worked thanks @aman
    – Masud Shaikh
    1 hour ago
















  • I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
    – Masud Shaikh
    1 hour ago










  • Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
    – aman
    1 hour ago










  • Its worked thanks @aman
    – Masud Shaikh
    1 hour ago















I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
– Masud Shaikh
1 hour ago




I have tried this code got the error message Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 400592896 bytes) in Unknown on line 0
– Masud Shaikh
1 hour ago












Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
– aman
1 hour ago




Try the updated answer, as I limit the item to 10, seems it is printing lots of data so it went out of memory
– aman
1 hour ago












Its worked thanks @aman
– Masud Shaikh
1 hour ago




Its worked thanks @aman
– Masud Shaikh
1 hour ago












up vote
1
down vote













First, you to have to check why the parameter is not working



If you check addCategoriesFilter function definition, then you will find it support a parameter which is an array. the parameter is a one-dimensional array which supports index as condition Type and value as filter value.Like



$categoriesFilter = 
[
'in' => [10,15],
'nin' => [12,18]
]


So,you have to change




addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))



to




addCategoriesFilter(array('nin' => $currentCategoryId))




 public function addCategoriesFilter(array $categoriesFilter)

foreach ($categoriesFilter as $conditionType => $values)
$categorySelect = $this->getConnection()->select()->from(
['cat' => $this->getTable('catalog_category_product')],
'cat.product_id'
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
$selectCondition = [
$this->mapConditionType($conditionType) => $categorySelect
];
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id', $selectCondition));

return $this;






share|improve this answer






















  • It is worked fine Thanks! @amit
    – Masud Shaikh
    1 hour ago














up vote
1
down vote













First, you to have to check why the parameter is not working



If you check addCategoriesFilter function definition, then you will find it support a parameter which is an array. the parameter is a one-dimensional array which supports index as condition Type and value as filter value.Like



$categoriesFilter = 
[
'in' => [10,15],
'nin' => [12,18]
]


So,you have to change




addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))



to




addCategoriesFilter(array('nin' => $currentCategoryId))




 public function addCategoriesFilter(array $categoriesFilter)

foreach ($categoriesFilter as $conditionType => $values)
$categorySelect = $this->getConnection()->select()->from(
['cat' => $this->getTable('catalog_category_product')],
'cat.product_id'
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
$selectCondition = [
$this->mapConditionType($conditionType) => $categorySelect
];
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id', $selectCondition));

return $this;






share|improve this answer






















  • It is worked fine Thanks! @amit
    – Masud Shaikh
    1 hour ago












up vote
1
down vote










up vote
1
down vote









First, you to have to check why the parameter is not working



If you check addCategoriesFilter function definition, then you will find it support a parameter which is an array. the parameter is a one-dimensional array which supports index as condition Type and value as filter value.Like



$categoriesFilter = 
[
'in' => [10,15],
'nin' => [12,18]
]


So,you have to change




addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))



to




addCategoriesFilter(array('nin' => $currentCategoryId))




 public function addCategoriesFilter(array $categoriesFilter)

foreach ($categoriesFilter as $conditionType => $values)
$categorySelect = $this->getConnection()->select()->from(
['cat' => $this->getTable('catalog_category_product')],
'cat.product_id'
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
$selectCondition = [
$this->mapConditionType($conditionType) => $categorySelect
];
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id', $selectCondition));

return $this;






share|improve this answer














First, you to have to check why the parameter is not working



If you check addCategoriesFilter function definition, then you will find it support a parameter which is an array. the parameter is a one-dimensional array which supports index as condition Type and value as filter value.Like



$categoriesFilter = 
[
'in' => [10,15],
'nin' => [12,18]
]


So,you have to change




addCategoriesFilter('category_id', array(
array('nin' => $currentCategoryId),
))



to




addCategoriesFilter(array('nin' => $currentCategoryId))




 public function addCategoriesFilter(array $categoriesFilter)

foreach ($categoriesFilter as $conditionType => $values)
$categorySelect = $this->getConnection()->select()->from(
['cat' => $this->getTable('catalog_category_product')],
'cat.product_id'
)->where($this->getConnection()->prepareSqlCondition('cat.category_id', ['in' => $values]));
$selectCondition = [
$this->mapConditionType($conditionType) => $categorySelect
];
$this->getSelect()->where($this->getConnection()->prepareSqlCondition('e.entity_id', $selectCondition));

return $this;







share|improve this answer














share|improve this answer



share|improve this answer








edited 35 mins ago

























answered 1 hour ago









Amit Bera♦

54k1366157




54k1366157











  • It is worked fine Thanks! @amit
    – Masud Shaikh
    1 hour ago
















  • It is worked fine Thanks! @amit
    – Masud Shaikh
    1 hour ago















It is worked fine Thanks! @amit
– Masud Shaikh
1 hour ago




It is worked fine Thanks! @amit
– Masud Shaikh
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242234%2fmagento2-not-in-value-category-ids-product-collection-issue%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

List of Gilmore Girls characters

What does second last employer means? [closed]

One-line joke