Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
FieldSorting example
'productNumber' => Uuid::
randomHex
(
)
,
'parentId' =>
$id
,
'stock' => 10,
'options' =>
[
[
'id' =>
$yellowId
]
,
[
'id' =>
$smallId
]
]
,
]
,
]
;
$this
->productRepository->
create
(
$products
,
$context
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'product.parentId',
$id
)
)
;
$criteria
->
addSorting
(
new
FieldSorting
(
'product.options.groupId'
)
)
;
$criteria
->
addSorting
(
new
FieldSorting
(
'product.options.id'
)
)
;
$criteria
->
setLimit
(
25
)
;
$criteria
->
setOffset
(
0
)
;
$criteria
->
setTotalCountMode
(
Criteria::TOTAL_COUNT_MODE_EXACT
)
;
$result
=
$this
->productRepository->
search
(
$criteria
,
$context
)
;
static
::
assertSame
(
1,
$result
->
getPage
(
)
)
;
static
::
assertSame
(
6,
$result
->
getTotal
(
)
)
;
static
::
assertCount
(
6,
$result
->
getEntities
(
)
)
;
}
$parentPropertyName
=
explode
(
'_',
$parentEntity
)
;
$parentPropertyName
=
array_map
(
'ucfirst',
$parentPropertyName
)
;
return
lcfirst
(
implode
(
'',
$parentPropertyName
)
)
. 'Id';
}
private
function
getCommits
(
string
$versionId
, WriteContext
$writeContext
)
: VersionCommitCollection
{
$criteria
=
new
Criteria
(
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'version_commit.versionId',
$versionId
)
)
;
$criteria
->
addSorting
(
new
FieldSorting
(
'version_commit.autoIncrement'
)
)
;
$commitIds
=
$this
->entitySearcher->
search
(
$this
->versionCommitDefinition,
$criteria
,
$writeContext
->
getContext
(
)
)
;
$readCriteria
=
new
Criteria
(
)
;
if
(
$commitIds
->
getTotal
(
)
> 0
)
{
$readCriteria
=
new
Criteria
(
$commitIds
->
getIds
(
)
)
;
}
$readCriteria
->
addAssociation
(
'data'
)
;
$readCriteria
->
getAssociation
(
'data'
)
private
function
createReviewCriteria
(
Request
$request
, SalesChannelContext
$context
)
: Criteria
{
$limit
=
(int)
$request
->
get
(
'limit', self::LIMIT
)
;
$page
=
(int)
$request
->
get
(
'p', self::DEFAULT_PAGE
)
;
$offset
=
$limit
*
(
$page
- 1
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
setLimit
(
$limit
)
;
$criteria
->
setOffset
(
$offset
)
;
$sorting
=
new
FieldSorting
(
'createdAt', 'DESC'
)
;
if
(
$request
->
get
(
'sort', 'points'
)
=== 'points'
)
{
$sorting
=
new
FieldSorting
(
'points', 'DESC'
)
;
}
$criteria
->
addSorting
(
$sorting
)
;
if
(
$request
->
get
(
'language'
)
=== self::FILTER_LANGUAGE
)
{
$criteria
->
addPostFilter
(
new
EqualsFilter
(
'languageId',
$context
->
getContext
(
)
->
getLanguageId
(
)
)
)
;
}
return
$this
->translations;
}
public
function
setTranslations
(
ProductCrossSellingTranslationCollection
$translations
)
: void
{
$this
->translations =
$translations
;
}
public
function
getSorting
(
)
: FieldSorting
{
return
new
FieldSorting
(
$this
->sortBy,
$this
->sortDirection
)
;
}
public
function
getType
(
)
: string
{
return
$this
->type;
}
public
function
setType
(
string
$type
)
: void
{
$this
->type =
$type
;
}
$sorting
=
[
]
;
$fields
=
$this
->fields;
usort
(
$fields
,
fn
(
$a
,
$b
)
=>
$b
[
'priority'
]
<=>
$a
[
'priority'
]
)
;
foreach
(
$fields
as
$field
)
{
$direction
=
mb_strtoupper
(
(string)
$field
[
'order'
]
)
=== FieldSorting::ASCENDING
? FieldSorting::ASCENDING
: FieldSorting::DESCENDING;
$sorting
[
]
=
new
FieldSorting
(
$field
[
'field'
]
,
$direction
,
(bool)
(
$field
[
'naturalSorting'
]
?? false
)
)
;
}
return
$sorting
;
}
public
function
getKey
(
)
: string
{
->
visibility
(
)
->
manufacturer
(
'm1'
)
->
build
(
)
,
]
;
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
->
create
(
$products
, Context::
createDefaultContext
(
)
)
;
$criteria
=
new
Criteria
(
[
$ids
->
get
(
'p1'
)
]
)
;
$criteria
->
addAssociation
(
'categories'
)
;
$criteria
->
addAssociation
(
'manufacturer'
)
;
$criteria
->
getAssociation
(
'categories'
)
->
addSorting
(
new
FieldSorting
(
'name', FieldSorting::ASCENDING
)
)
;
$criteria
->
addFields
(
[
'name', 'categories.name', 'manufacturer.name'
]
)
;
$values
=
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
->
search
(
$criteria
, Context::
createDefaultContext
(
)
)
;
$entity
=
$values
->
first
(
)
;
static
::
assertInstanceOf
(
PartialEntity::
class
,
$entity
)
;
static
::
assertSame
(
'p1',
$entity
->
get
(
'name'
)
)
;
static
::
assertNull
(
$entity
->
get
(
'type'
)
)
;
private
function
createCriteria
(
Request
$request
)
: Criteria
{
$limit
=
$request
->query->
get
(
'limit'
)
;
$limit
=
$limit
?
(int)
$limit
: self::LIMIT;
$page
=
$request
->query->
get
(
'p'
)
;
$page
=
$page
?
(int)
$page
: self::DEFAULT_PAGE;
$offset
=
$limit
*
(
$page
- 1
)
;
return
(
new
Criteria
(
)
)
->
addSorting
(
new
FieldSorting
(
'wishlists.updatedAt', FieldSorting::ASCENDING
)
)
->
addAssociation
(
'manufacturer'
)
->
addAssociation
(
'options.group'
)
->
setLimit
(
$limit
)
->
setOffset
(
$offset
)
->
setTotalCountMode
(
Criteria::TOTAL_COUNT_MODE_EXACT
)
;
}
}
return
new
ProductCrossSellingRouteResponse
(
$elements
)
;
}
private
function
loadCrossSellings
(
string
$productId
, SalesChannelContext
$context
)
: ProductCrossSellingCollection
{
$criteria
=
new
Criteria
(
)
;
$criteria
->
setTitle
(
'product-cross-selling-route'
)
;
$criteria
->
addAssociation
(
'assignedProducts'
)
->
addFilter
(
new
EqualsFilter
(
'product.id',
$productId
)
)
->
addFilter
(
new
EqualsFilter
(
'active', 1
)
)
->
addSorting
(
new
FieldSorting
(
'position', FieldSorting::ASCENDING
)
)
;
$this
->eventDispatcher->
dispatch
(
new
ProductCrossSellingCriteriaLoadEvent
(
$criteria
,
$context
)
)
;
/** @var ProductCrossSellingCollection $crossSellings */
$crossSellings
=
$this
->crossSellingRepository
->
search
(
$criteria
,
$context
->
getContext
(
)
)
->
getEntities
(
)
;
return
$crossSellings
;
}
public
function
load
(
Request
$request
, SalesChannelContext
$salesChannelContext
, CustomerEntity
$customer
)
: AddressListingPage
{
$page
=
$this
->genericLoader->
load
(
$request
,
$salesChannelContext
)
;
$page
= AddressListingPage::
createFrom
(
$page
)
;
$page
->
setSalutations
(
$this
->
getSalutations
(
$salesChannelContext
)
)
;
$page
->
setCountries
(
$this
->
getCountries
(
$salesChannelContext
)
)
;
$criteria
=
(
new
Criteria
(
)
)
->
addSorting
(
new
FieldSorting
(
'firstName', FieldSorting::ASCENDING
)
)
;
$page
->
setAddresses
(
$this
->listAddressRoute->
load
(
$criteria
,
$salesChannelContext
,
$customer
)
->
getAddressCollection
(
)
)
;
$page
->
setCart
(
$this
->cartService->
getCart
(
$salesChannelContext
->
getToken
(
)
,
$salesChannelContext
)
)
;
$page
->
setAddress
(
$page
->
getAddresses
(
)
->
get
(
$request
->
get
(
'addressId'
)
)
)
;
$this
->eventDispatcher->
dispatch
(
new
AddressListingPageLoadedEvent
(
$page
,
$salesChannelContext
,
$request
)
)
;
$this
->salesChannelContext->
getContext
(
)
)
;
return
$this
->
addProductToCart
(
$ids
->
get
(
'product'
)
, 1,
$cart
,
$this
->cartService,
$this
->salesChannelContext
)
;
}
private
function
changeTransactionStateToPaid
(
string
$orderId
)
: void
{
$transaction
=
$this
->orderTransactionRepository
->
search
(
(
new
Criteria
(
)
)
->
addFilter
(
new
EqualsFilter
(
'orderId',
$orderId
)
)
->
addSorting
(
new
FieldSorting
(
'createdAt', FieldSorting::DESCENDING
)
)
,
$this
->salesChannelContext->
getContext
(
)
)
->
first
(
)
;
static
::
assertInstanceOf
(
OrderTransactionEntity::
class
,
$transaction
)
;
$this
->orderTransactionStateHandler->
paid
(
$transaction
->
getId
(
)
,
$this
->salesChannelContext->
getContext
(
)
)
;
}
private
function
createTags
(
IdsCollection
$idsCollection
)
: void
{
$tags
=
[
[
'quantityStart' => 1,
'ruleId' =>
$ruleA
,
'price' =>
[
[
'currencyId' => Defaults::CURRENCY, 'gross' => 10, 'net' => 9, 'linked' => false
]
]
,
]
,
]
,
]
,
]
;
$this
->repository->
create
(
$data
, Context::
createDefaultContext
(
)
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
addSorting
(
new
FieldSorting
(
'product.prices.price', FieldSorting::ASCENDING
)
)
;
$criteria
->
addFilter
(
new
EqualsFilter
(
'product.ean',
$filterId
)
)
;
$context
=
$this
->
createContext
(
[
$ruleA
]
)
;
$products
=
$this
->repository->
searchIds
(
$criteria
,
$context
)
;
static
::
assertSame
(
[
$id2
,
$id3
,
$id
]
,
$products
->
getIds
(
)
)
;
/** * @return string[] */
private
function
getValidSalutationIds
(
)
: array
{
/** @var EntityRepository $repository */
$repository
=
$this
->
getContainer
(
)
->
get
(
'salutation.repository'
)
;
$criteria
=
(
new
Criteria
(
)
)
->
addSorting
(
new
FieldSorting
(
'salutationKey'
)
)
;
/** @var string[] $ids */
$ids
=
$repository
->
searchIds
(
$criteria
, Context::
createDefaultContext
(
)
)
->
getIds
(
)
;
return
$ids
;
}
private
function
createData
(
)
: void
{
$data
=
[
[
public
function
__construct
(
private
readonly iterable
$tasks
,
private
readonly EntityRepository
$scheduledTaskRepository
,
private
readonly ParameterBagInterface
$parameterBag
)
{
}
public
function
getAllTasks
(
Context
$context
)
: ScheduledTaskCollection
{
$criteria
=
new
Criteria
(
)
;
$criteria
->
addSorting
(
new
FieldSorting
(
'createdAt'
)
)
;
return
$this
->scheduledTaskRepository
->
search
(
$criteria
,
$context
)
->
getEntities
(
)
;
}
public
function
registerTasks
(
)
: void
{
$context
= Context::
createDefaultContext
(
)
;
$alreadyRegisteredTasks
=
$this
->scheduledTaskRepository
private
function
createCriteria
(
Request
$request
, SalesChannelContext
$context
)
: Criteria
{
$limit
=
(int)
$request
->
get
(
'limit', self::LIMIT
)
;
$page
=
(int)
$request
->
get
(
'p', self::DEFAULT_PAGE
)
;
$offset
=
$limit
*
(
$page
- 1
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
setLimit
(
$limit
)
;
$criteria
->
setOffset
(
$offset
)
;
$criteria
->
setTotalCountMode
(
Criteria::TOTAL_COUNT_MODE_EXACT
)
;
$sorting
=
new
FieldSorting
(
'createdAt', 'DESC'
)
;
if
(
$request
->
get
(
'sort', 'createdAt'
)
=== 'points'
)
{
$sorting
=
new
FieldSorting
(
'points', 'DESC'
)
;
}
$criteria
->
addSorting
(
$sorting
)
;
if
(
$request
->
get
(
'language'
)
=== self::FILTER_LANGUAGE
)
{
$criteria
->
addPostFilter
(
new
EqualsFilter
(
'languageId',
$context
->
getContext
(
)
->
getLanguageId
(
)
)
)
;
}
static
::
assertTrue
(
$criteria
->
hasAssociation
(
'prices'
)
)
;
$nested
=
$criteria
->
getAssociation
(
'prices'
)
;
static
::
assertCount
(
1,
$nested
->
getFilters
(
)
)
;
static
::
assertCount
(
1,
$nested
->
getSorting
(
)
)
;
}
public
function
testCriteriaToArray
(
)
: void
{
$criteria
=
(
new
Criteria
(
)
)
->
addSorting
(
new
FieldSorting
(
'order.createdAt', FieldSorting::DESCENDING
)
)
->
addSorting
(
new
CountSorting
(
'transactions.id', CountSorting::ASCENDING
)
)
->
addAssociation
(
'transactions.paymentMethod'
)
->
addAssociation
(
'deliveries.shippingMethod'
)
->
setLimit
(
1
)
->
setOffset
(
(
1 - 1
)
* 1
)
->
setTotalCountMode
(
100
)
;
$criteria
->
getAssociation
(
'transaction'
)
->
addSorting
(
new
FieldSorting
(
'createdAt', FieldSorting::DESCENDING
)
)
;
$criteriaArray
=
$this
->requestCriteriaBuilder->
toArray
(
$criteria
)
;