Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
filterBySalesChannelId example
$productRepository
=
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
;
$criteria
=
new
Criteria
(
[
$productAId
]
)
;
$criteria
->
addAssociation
(
'visibilities'
)
;
/** @var ProductEntity $productA */
$productA
=
$productRepository
->
search
(
$criteria
, Context::
createDefaultContext
(
)
)
->
first
(
)
;
static
::
assertInstanceOf
(
ProductEntity::
class
,
$productA
)
;
static
::
assertInstanceOf
(
ProductVisibilityCollection::
class
,
$productA
->
getVisibilities
(
)
)
;
static
::
assertCount
(
1,
$productA
->
getVisibilities
(
)
)
;
static
::
assertNotNull
(
$productA
->
getVisibilities
(
)
->
filterBySalesChannelId
(
$salesChannelAId
)
->
first
(
)
)
;
$criteria
=
new
Criteria
(
[
$productABId
]
)
;
$criteria
->
addAssociation
(
'visibilities'
)
;
$productB
=
$productRepository
->
search
(
$criteria
, Context::
createDefaultContext
(
)
)
->
first
(
)
;
static
::
assertInstanceOf
(
ProductEntity::
class
,
$productB
)
;
static
::
assertInstanceOf
(
ProductVisibilityCollection::
class
,
$productB
->
getVisibilities
(
)
)
;
static
::
assertCount
(
2,
$productB
->
getVisibilities
(
)
)
;
static
::
assertNotNull
(
$productB
->
getVisibilities
(
)
->
filterBySalesChannelId
(
$salesChannelAId
)
->
first
(
)
)
;
$id
= Uuid::
randomHex
(
)
;
$this
->
upsertProduct
(
[
'id' =>
$id
,
'name' => 'foo',
]
,
$salesChannelId
)
;
$context
=
$salesChannelContext
->
getContext
(
)
;
$criteria
=
(
new
Criteria
(
[
$id
]
)
)
->
addAssociation
(
'seoUrls'
)
;
$product
=
$this
->productRepository->
search
(
$criteria
,
$context
)
->
first
(
)
;
$seoUrls
=
$product
->
getSeoUrls
(
)
->
filterBySalesChannelId
(
$salesChannelId
)
;
static
::
assertEmpty
(
$seoUrls
)
;
$this
->
upsertProduct
(
[
'id' =>
$id
,
'name' => 'foo',
'customFields' =>
[
'foo' => 'bar',
]
,
]
,
$salesChannelId
)
;
$criteria
=
(
new
Criteria
(
[
$id
]
)
)
->
addAssociation
(
'seoUrls'
)
;
]
,
]
, Context::
createDefaultContext
(
)
)
;
$product
=
$this
->productRepository->
search
(
$criteria
,
$salesChannelContext
->
getContext
(
)
)
->
first
(
)
;
static
::
assertNotNull
(
$product
->
getMainCategories
(
)
)
;
static
::
assertInstanceOf
(
MainCategoryCollection::
class
,
$product
->
getMainCategories
(
)
)
;
$mainCategories
=
$product
->
getMainCategories
(
)
;
static
::
assertCount
(
1,
$mainCategories
)
;
$mainCategory
=
$mainCategories
->
filterBySalesChannelId
(
$salesChannelId
)
->
first
(
)
;
static
::
assertInstanceOf
(
MainCategoryEntity::
class
,
$mainCategory
)
;
static
::
assertEquals
(
$salesChannelId
,
$mainCategory
->
getSalesChannelId
(
)
)
;
static
::
assertEquals
(
$categories
->
firstId
(
)
,
$mainCategory
->
getCategoryId
(
)
)
;
}
private
function
createTestProduct
(
)
: string
{
$id
= Uuid::
randomHex
(
)
;
$payload
=
[
'id' =>
$id
,
'name' => 'foo bar',