Get products where multiselect value is false
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
how can I get products where the value of a multiselect is not set ?
I've got these so far :
$filter =
array(
array('eq' => array("378")), //multiselect with one value
array('eq' => array("378,379")), //multiselect with 2 values
array('eq' => false), //multiselect with no value, need to make this work
);
$collection->addAttributeToFilter( "multiselect", $filter );
magento2 attributes multiselect-attribute
add a comment |
how can I get products where the value of a multiselect is not set ?
I've got these so far :
$filter =
array(
array('eq' => array("378")), //multiselect with one value
array('eq' => array("378,379")), //multiselect with 2 values
array('eq' => false), //multiselect with no value, need to make this work
);
$collection->addAttributeToFilter( "multiselect", $filter );
magento2 attributes multiselect-attribute
add a comment |
how can I get products where the value of a multiselect is not set ?
I've got these so far :
$filter =
array(
array('eq' => array("378")), //multiselect with one value
array('eq' => array("378,379")), //multiselect with 2 values
array('eq' => false), //multiselect with no value, need to make this work
);
$collection->addAttributeToFilter( "multiselect", $filter );
magento2 attributes multiselect-attribute
how can I get products where the value of a multiselect is not set ?
I've got these so far :
$filter =
array(
array('eq' => array("378")), //multiselect with one value
array('eq' => array("378,379")), //multiselect with 2 values
array('eq' => false), //multiselect with no value, need to make this work
);
$collection->addAttributeToFilter( "multiselect", $filter );
magento2 attributes multiselect-attribute
magento2 attributes multiselect-attribute
asked yesterday
Morgan TartreauMorgan Tartreau
39312
39312
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try this:
$filter =
array(
array('eq' => array("378")), // multiselect with one value
array('eq' => array("378,379")), // multiselect with 2 values
array('null' => true), // multiselect with no value (null value)
);
$collection->addAttributeToFilter( "multiselect", $filter );
Update:
here is how it will work using the SearchCriteria
with the product repository (tested on the custom attribute available_shipping_methods
having a multiselect input type):
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilderFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
/**
* Class Search
*/
class Search extends Action
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* Search constructor.
*
* @param Context $context
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return MagentoFrameworkControllerResultInterface|ResponseInterface
* @throws MagentoFrameworkExceptionNotFoundException
*/
public function execute()
{
try {
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
$filters = [
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%shipping_shipping%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%flatrate_flatrate%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue(null)
->setConditionType('null')
->create()
];
$filterGroup = $this->filterGroupBuilder->setFilters($filters)
->create();
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$productList = $this->productRepository->getList($searchCriteria);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$resultPage = $this->resultFactory->create($this->resultFactory::TYPE_PAGE);
return $resultPage;
}
}
Result query looks like this (our conditions at the last line):
SELECT `e`.*, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) AS `available_shipping_methods`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`row_id` = `e`.`row_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1)
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`row_id` = `e`.`row_id`) AND (`at_visibility_default`.`attribute_id` = '99') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`row_id` = `e`.`row_id`) AND (`at_visibility`.`attribute_id` = '99') AND (`at_visibility`.`store_id` = 1)
INNER JOIN `catalog_product_entity_text` AS `at_available_shipping_methods_default` ON (`at_available_shipping_methods_default`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods_default`.`attribute_id` = '265') AND `at_available_shipping_methods_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_text` AS `at_available_shipping_methods` ON (`at_available_shipping_methods`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods`.`attribute_id` = '265') AND (`at_available_shipping_methods`.`store_id` = 1)
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id WHERE (((IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%shipping_shipping%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%flatrate_flatrate%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) IS NULL))) AND (e.created_in <= 1) AND (e.updated_in > 1)
and result itself contains products with desired attribute value and without value at all (null
).
PS: a comma-separated value of the multiselect attribute is the main problem, because of that we need to use %LIKE%
condition, what is not a good idea.
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269338%2fget-products-where-multiselect-value-is-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
$filter =
array(
array('eq' => array("378")), // multiselect with one value
array('eq' => array("378,379")), // multiselect with 2 values
array('null' => true), // multiselect with no value (null value)
);
$collection->addAttributeToFilter( "multiselect", $filter );
Update:
here is how it will work using the SearchCriteria
with the product repository (tested on the custom attribute available_shipping_methods
having a multiselect input type):
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilderFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
/**
* Class Search
*/
class Search extends Action
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* Search constructor.
*
* @param Context $context
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return MagentoFrameworkControllerResultInterface|ResponseInterface
* @throws MagentoFrameworkExceptionNotFoundException
*/
public function execute()
{
try {
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
$filters = [
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%shipping_shipping%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%flatrate_flatrate%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue(null)
->setConditionType('null')
->create()
];
$filterGroup = $this->filterGroupBuilder->setFilters($filters)
->create();
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$productList = $this->productRepository->getList($searchCriteria);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$resultPage = $this->resultFactory->create($this->resultFactory::TYPE_PAGE);
return $resultPage;
}
}
Result query looks like this (our conditions at the last line):
SELECT `e`.*, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) AS `available_shipping_methods`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`row_id` = `e`.`row_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1)
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`row_id` = `e`.`row_id`) AND (`at_visibility_default`.`attribute_id` = '99') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`row_id` = `e`.`row_id`) AND (`at_visibility`.`attribute_id` = '99') AND (`at_visibility`.`store_id` = 1)
INNER JOIN `catalog_product_entity_text` AS `at_available_shipping_methods_default` ON (`at_available_shipping_methods_default`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods_default`.`attribute_id` = '265') AND `at_available_shipping_methods_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_text` AS `at_available_shipping_methods` ON (`at_available_shipping_methods`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods`.`attribute_id` = '265') AND (`at_available_shipping_methods`.`store_id` = 1)
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id WHERE (((IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%shipping_shipping%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%flatrate_flatrate%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) IS NULL))) AND (e.created_in <= 1) AND (e.updated_in > 1)
and result itself contains products with desired attribute value and without value at all (null
).
PS: a comma-separated value of the multiselect attribute is the main problem, because of that we need to use %LIKE%
condition, what is not a good idea.
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
add a comment |
Try this:
$filter =
array(
array('eq' => array("378")), // multiselect with one value
array('eq' => array("378,379")), // multiselect with 2 values
array('null' => true), // multiselect with no value (null value)
);
$collection->addAttributeToFilter( "multiselect", $filter );
Update:
here is how it will work using the SearchCriteria
with the product repository (tested on the custom attribute available_shipping_methods
having a multiselect input type):
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilderFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
/**
* Class Search
*/
class Search extends Action
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* Search constructor.
*
* @param Context $context
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return MagentoFrameworkControllerResultInterface|ResponseInterface
* @throws MagentoFrameworkExceptionNotFoundException
*/
public function execute()
{
try {
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
$filters = [
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%shipping_shipping%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%flatrate_flatrate%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue(null)
->setConditionType('null')
->create()
];
$filterGroup = $this->filterGroupBuilder->setFilters($filters)
->create();
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$productList = $this->productRepository->getList($searchCriteria);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$resultPage = $this->resultFactory->create($this->resultFactory::TYPE_PAGE);
return $resultPage;
}
}
Result query looks like this (our conditions at the last line):
SELECT `e`.*, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) AS `available_shipping_methods`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`row_id` = `e`.`row_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1)
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`row_id` = `e`.`row_id`) AND (`at_visibility_default`.`attribute_id` = '99') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`row_id` = `e`.`row_id`) AND (`at_visibility`.`attribute_id` = '99') AND (`at_visibility`.`store_id` = 1)
INNER JOIN `catalog_product_entity_text` AS `at_available_shipping_methods_default` ON (`at_available_shipping_methods_default`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods_default`.`attribute_id` = '265') AND `at_available_shipping_methods_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_text` AS `at_available_shipping_methods` ON (`at_available_shipping_methods`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods`.`attribute_id` = '265') AND (`at_available_shipping_methods`.`store_id` = 1)
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id WHERE (((IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%shipping_shipping%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%flatrate_flatrate%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) IS NULL))) AND (e.created_in <= 1) AND (e.updated_in > 1)
and result itself contains products with desired attribute value and without value at all (null
).
PS: a comma-separated value of the multiselect attribute is the main problem, because of that we need to use %LIKE%
condition, what is not a good idea.
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
add a comment |
Try this:
$filter =
array(
array('eq' => array("378")), // multiselect with one value
array('eq' => array("378,379")), // multiselect with 2 values
array('null' => true), // multiselect with no value (null value)
);
$collection->addAttributeToFilter( "multiselect", $filter );
Update:
here is how it will work using the SearchCriteria
with the product repository (tested on the custom attribute available_shipping_methods
having a multiselect input type):
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilderFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
/**
* Class Search
*/
class Search extends Action
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* Search constructor.
*
* @param Context $context
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return MagentoFrameworkControllerResultInterface|ResponseInterface
* @throws MagentoFrameworkExceptionNotFoundException
*/
public function execute()
{
try {
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
$filters = [
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%shipping_shipping%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%flatrate_flatrate%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue(null)
->setConditionType('null')
->create()
];
$filterGroup = $this->filterGroupBuilder->setFilters($filters)
->create();
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$productList = $this->productRepository->getList($searchCriteria);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$resultPage = $this->resultFactory->create($this->resultFactory::TYPE_PAGE);
return $resultPage;
}
}
Result query looks like this (our conditions at the last line):
SELECT `e`.*, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) AS `available_shipping_methods`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`row_id` = `e`.`row_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1)
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`row_id` = `e`.`row_id`) AND (`at_visibility_default`.`attribute_id` = '99') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`row_id` = `e`.`row_id`) AND (`at_visibility`.`attribute_id` = '99') AND (`at_visibility`.`store_id` = 1)
INNER JOIN `catalog_product_entity_text` AS `at_available_shipping_methods_default` ON (`at_available_shipping_methods_default`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods_default`.`attribute_id` = '265') AND `at_available_shipping_methods_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_text` AS `at_available_shipping_methods` ON (`at_available_shipping_methods`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods`.`attribute_id` = '265') AND (`at_available_shipping_methods`.`store_id` = 1)
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id WHERE (((IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%shipping_shipping%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%flatrate_flatrate%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) IS NULL))) AND (e.created_in <= 1) AND (e.updated_in > 1)
and result itself contains products with desired attribute value and without value at all (null
).
PS: a comma-separated value of the multiselect attribute is the main problem, because of that we need to use %LIKE%
condition, what is not a good idea.
Try this:
$filter =
array(
array('eq' => array("378")), // multiselect with one value
array('eq' => array("378,379")), // multiselect with 2 values
array('null' => true), // multiselect with no value (null value)
);
$collection->addAttributeToFilter( "multiselect", $filter );
Update:
here is how it will work using the SearchCriteria
with the product repository (tested on the custom attribute available_shipping_methods
having a multiselect input type):
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilderFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
/**
* Class Search
*/
class Search extends Action
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* Search constructor.
*
* @param Context $context
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return MagentoFrameworkControllerResultInterface|ResponseInterface
* @throws MagentoFrameworkExceptionNotFoundException
*/
public function execute()
{
try {
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
$filters = [
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%shipping_shipping%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue('%flatrate_flatrate%')
->setConditionType('like')
->create(),
$this->filterBuilder->setField('available_shipping_methods')
->setValue(null)
->setConditionType('null')
->create()
];
$filterGroup = $this->filterGroupBuilder->setFilters($filters)
->create();
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$productList = $this->productRepository->getList($searchCriteria);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$resultPage = $this->resultFactory->create($this->resultFactory::TYPE_PAGE);
return $resultPage;
}
}
Result query looks like this (our conditions at the last line):
SELECT `e`.*, IF(at_status.value_id > 0, at_status.value, at_status_default.value) AS `status`, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`, IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) AS `available_shipping_methods`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`row_id` = `e`.`row_id`) AND (`at_status_default`.`attribute_id` = '97') AND `at_status_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`row_id` = `e`.`row_id`) AND (`at_status`.`attribute_id` = '97') AND (`at_status`.`store_id` = 1)
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default` ON (`at_visibility_default`.`row_id` = `e`.`row_id`) AND (`at_visibility_default`.`attribute_id` = '99') AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`row_id` = `e`.`row_id`) AND (`at_visibility`.`attribute_id` = '99') AND (`at_visibility`.`store_id` = 1)
INNER JOIN `catalog_product_entity_text` AS `at_available_shipping_methods_default` ON (`at_available_shipping_methods_default`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods_default`.`attribute_id` = '265') AND `at_available_shipping_methods_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_text` AS `at_available_shipping_methods` ON (`at_available_shipping_methods`.`row_id` = `e`.`row_id`) AND (`at_available_shipping_methods`.`attribute_id` = '265') AND (`at_available_shipping_methods`.`store_id` = 1)
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id WHERE (((IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%shipping_shipping%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) LIKE '%flatrate_flatrate%') OR (IF(at_available_shipping_methods.value_id > 0, at_available_shipping_methods.value, at_available_shipping_methods_default.value) IS NULL))) AND (e.created_in <= 1) AND (e.updated_in > 1)
and result itself contains products with desired attribute value and without value at all (null
).
PS: a comma-separated value of the multiselect attribute is the main problem, because of that we need to use %LIKE%
condition, what is not a good idea.
edited yesterday
answered yesterday
Siarhey UchukhlebauSiarhey Uchukhlebau
10k93058
10k93058
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
add a comment |
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
Ok I'll try, but I think I'll just add the two options for each of my concerned products (like 2 options == 0 options)
– Morgan Tartreau
yesterday
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269338%2fget-products-where-multiselect-value-is-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown