Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
BeforeCartMergeEvent example
public
function
testReturnsCorrectProperties
(
)
: void
{
$customerCart
=
new
Cart
(
'customerCart'
)
;
$guestCart
=
new
Cart
(
'customerCart'
)
;
$mergeableLineItems
=
new
LineItemCollection
(
)
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$context
= Context::
createDefaultContext
(
)
;
$salesChannelContext
->
method
(
'getContext'
)
->
willReturn
(
$context
)
;
$event
=
new
BeforeCartMergeEvent
(
$customerCart
,
$guestCart
,
$mergeableLineItems
,
$salesChannelContext
)
;
static
::
assertSame
(
$customerCart
,
$event
->
getCustomerCart
(
)
)
;
static
::
assertSame
(
$guestCart
,
$event
->
getGuestCart
(
)
)
;
static
::
assertSame
(
$mergeableLineItems
,
$event
->
getMergeableLineItems
(
)
)
;
static
::
assertSame
(
$salesChannelContext
,
$event
->
getSalesChannelContext
(
)
)
;
static
::
assertSame
(
$context
,
$event
->
getContext
(
)
)
;
}
if
(
!
$customerContext
->
getDomainId
(
)
)
{
$customerContext
->
setDomainId
(
$currentContext
->
getDomainId
(
)
)
;
}
return
$this
->
enrichCustomerContext
(
$customerContext
,
$currentContext
,
$currentContext
->
getToken
(
)
,
$customerId
)
;
}
private
function
mergeCart
(
Cart
$customerCart
, Cart
$guestCart
, SalesChannelContext
$customerContext
)
: Cart
{
$mergeableLineItems
=
$guestCart
->
getLineItems
(
)
->
filter
(
fn
(
LineItem
$item
)
=>
(
$item
->
getQuantity
(
)
> 0 &&
$item
->
isStackable
(
)
)
|| !
$customerCart
->
has
(
$item
->
getId
(
)
)
)
;
$this
->eventDispatcher->
dispatch
(
new
BeforeCartMergeEvent
(
$customerCart
,
$guestCart
,
$mergeableLineItems
,
$customerContext
)
)
;
$errors
=
$customerCart
->
getErrors
(
)
;
$customerCart
->
setErrors
(
new
ErrorCollection
(
)
)
;
$customerCartClone
=
clone
$customerCart
;
$customerCart
->
setErrors
(
$errors
)
;