Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getVariantListingConfig example
'configurator_group_config' =>
json_encode
(
[
]
, \JSON_THROW_ON_ERROR
)
,
]
,
[
'id' => Uuid::
fromHexToBytes
(
$ids
->
get
(
'product-1'
)
)
,
]
)
;
/** @var EntityRepository $productRepository */
$productRepository
= self::
getContainer
(
)
->
get
(
'product.repository'
)
;
/** @var ProductEntity $product */
$product
=
$productRepository
->
search
(
new
Criteria
(
[
$ids
->
get
(
'product-1'
)
]
)
, Context::
createDefaultContext
(
)
)
->
first
(
)
;
static
::
assertNotNull
(
$product
)
;
$config
=
$product
->
getVariantListingConfig
(
)
;
static
::
assertNotNull
(
$config
)
;
static
::
assertFalse
(
$config
->
getDisplayParent
(
)
)
;
static
::
assertEquals
(
$ids
->
get
(
'variant-1-2'
)
,
$config
->
getMainVariantId
(
)
)
;
static
::
assertEquals
(
[
]
,
$config
->
getConfiguratorGroupConfig
(
)
)
;
$this
->migration->
update
(
$this
->connection
)
;
/** @var ProductEntity $product */
$product
=
$productRepository
->
search
(
new
Criteria
(
[
$ids
->
get
(
'product-1'
)
]
)
, Context::
createDefaultContext
(
)
)
->
first
(
)
;
static
::
assertNotNull
(
$product
)
;
private
function
checkVariantListingConfig
(
string
$productId
, SalesChannelContext
$context
)
: ?string
{
/** @var SalesChannelProductEntity|null $product */
$product
=
$this
->productRepository->
search
(
new
Criteria
(
[
$productId
]
)
,
$context
)
->
first
(
)
;
if
(
$product
=== null ||
$product
->
getParentId
(
)
!== null
)
{
return
null;
}
if
(
(
$listingConfig
=
$product
->
getVariantListingConfig
(
)
)
=== null ||
$listingConfig
->
getDisplayParent
(
)
!== true
)
{
return
null;
}
return
$listingConfig
->
getMainVariantId
(
)
;
}
/** * @throws InconsistentCriteriaIdsException */
private
function
findBestVariant
(
string
$productId
, SalesChannelContext
$context
)
: string
{
return
strnatcmp
(
(string)
$a
->
getTranslation
(
'name'
)
,
(string)
$b
->
getTranslation
(
'name'
)
)
;
}
return
(
$a
->
getTranslation
(
'position'
)
??
$a
->
getPosition
(
)
?? 0
)
<=>
(
$b
->
getTranslation
(
'position'
)
??
$b
->
getPosition
(
)
?? 0
)
;
}
)
;
}
$collection
=
new
PropertyGroupCollection
(
$sorted
)
;
// check if product has an individual sorting configuration for property groups\
$config
=
$product
->
getVariantListingConfig
(
)
;
if
(
$config
)
{
$config
=
$config
->
getConfiguratorGroupConfig
(
)
;
}
if
(
!
$config
)
{
$collection
->
sortByPositions
(
)
;
return
$collection
;
}
$sortedGroupIds
=
array_column
(
$config
, 'id'
)
;
$this
->salesChannelContext->
getContext
(
)
)
;
$listing
=
$this
->
fetchListing
(
)
;
static
::
assertEquals
(
1,
$listing
->
getTotal
(
)
)
;
// only main product should be returned
$mainProduct
=
$listing
->
getEntities
(
)
->
first
(
)
;
static
::
assertNotNull
(
$mainProduct
)
;
static
::
assertEquals
(
$this
->productId,
$mainProduct
->
getId
(
)
)
;
static
::
assertEquals
(
$this
->mainVariantId,
$mainProduct
->
getVariantListingConfig
(
)
?->
getMainVariantId
(
)
)
;
static
::
assertTrue
(
$mainProduct
->
hasExtension
(
'search'
)
)
;
}
public
function
testMainProductIsHiddenIfVariantsOutOfStock
(
)
: void
{
$this
->
createProduct
(
[
]
)
;
$this
->systemConfigService->
set
(
'core.listing.hideCloseoutProductsWhenOutOfStock',
true,
$this
->salesChannelContext->
getSalesChannel
(
)
->
getId
(
)
)
;