Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getMainCategoryFilter example
return
new
OrFilter
(
array_map
(
static
fn
(
string
$id
)
=>
new
ContainsFilter
(
'path', '|' .
$id
. '|'
)
,
$ids
)
)
;
}
private
function
getMainCategory
(
ProductEntity
$product
, SalesChannelContext
$context
)
: ?CategoryEntity
{
$criteria
=
new
Criteria
(
)
;
$criteria
->
setLimit
(
1
)
;
$criteria
->
setTitle
(
'breadcrumb-builder::main-category'
)
;
if
(
(
$product
->
getMainCategories
(
)
=== null ||
$product
->
getMainCategories
(
)
->
count
(
)
<= 0
)
&&
$product
->
getParentId
(
)
!== null
)
{
$criteria
->
addFilter
(
$this
->
getMainCategoryFilter
(
$product
->
getParentId
(
)
,
$context
)
)
;
}
else
{
$criteria
->
addFilter
(
$this
->
getMainCategoryFilter
(
$product
->
getId
(
)
,
$context
)
)
;
}
$categories
=
$this
->categoryRepository->
search
(
$criteria
,
$context
->
getContext
(
)
)
->
getEntities
(
)
;
if
(
$categories
->
count
(
)
<= 0
)
{
return
null;
}
$firstCategory
=
$categories
->
first
(
)
;