Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
Delivery example
new
DeliveryPosition
(
Uuid::
randomHex
(
)
,
new
LineItem
(
'test', 'test'
)
,
1,
new
CalculatedPrice
(
0, 0,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
,
new
DeliveryDate
(
new
\
DateTime
(
)
,
new
\
DateTime
(
)
)
)
)
;
$shippingMethod
=
new
ShippingMethodEntity
(
)
;
$shippingMethod
->
setTaxType
(
ShippingMethodEntity::TAX_TYPE_AUTO
)
;
$delivery
=
new
Delivery
(
$positions
,
new
DeliveryDate
(
new
\
DateTime
(
)
,
new
\
DateTime
(
)
)
,
$shippingMethod
,
new
ShippingLocation
(
new
CountryEntity
(
)
, null, null
)
,
new
CalculatedPrice
(
5, 5,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
)
;
$this
->deliveryCalculator->
calculate
(
new
CartDataCollection
(
)
,
new
Cart
(
'test'
)
,
new
DeliveryCollection
(
[
$delivery
]
)
,
$context
)
;
$newCosts
=
$delivery
->
getShippingCosts
(
)
;
static
::
assertEquals
(
5,
$newCosts
->
getUnitPrice
(
)
)
;
// use shipping method delivery time as default
if
(
$shippingMethod
->
getDeliveryTime
(
)
!== null
)
{
$deliveryTime
= DeliveryTime::
createFromEntity
(
$shippingMethod
->
getDeliveryTime
(
)
)
;
}
$this
->
buildPositions
(
$collection
,
$positions
,
$deliveryTime
)
;
if
(
$positions
->
count
(
)
<= 0
)
{
return
null;
}
return
new
Delivery
(
$positions
,
$this
->
getDeliveryDateByPositions
(
$positions
)
,
$shippingMethod
,
$context
->
getShippingLocation
(
)
,
new
CalculatedPrice
(
0, 0,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
)
;
}
private
function
getDeliveryDateByPositions
(
DeliveryPositionCollection
$positions
)
: DeliveryDate
{
// this function is only called if the provided collection contains a deliverable line item
/** * add a discount delivery item to DeliveryCollection $deliveries */
private
function
addDiscountDeliveryItem
(
DeliveryCollection
$deliveries
, Delivery
$delivery
, CalculatedPrice
$price
)
: void
{
if
(
$price
->
getTotalPrice
(
)
>= 0
)
{
return
;
}
$delivery
=
new
Delivery
(
$delivery
->
getPositions
(
)
,
$delivery
->
getDeliveryDate
(
)
,
$delivery
->
getShippingMethod
(
)
,
$delivery
->
getLocation
(
)
,
$price
)
;
$deliveries
->
add
(
$delivery
)
;
}
/** * if we have a discount with scope delivery we add a lineItem in cart with price 0 * * @throws CartException */
$deliveryPositionCollection
->
add
(
new
DeliveryPosition
(
Uuid::
randomHex
(
)
,
$lineItem
,
$lineItem
->
getQuantity
(
)
,
$calculatedPrice
,
$deliveryDate
)
)
;
}
$cart
->
setDeliveries
(
new
DeliveryCollection
(
[
new
Delivery
(
$deliveryPositionCollection
,
$deliveryDate
,
new
ShippingMethodEntity
(
)
,
new
ShippingLocation
(
new
CountryEntity
(
)
, null, null
)
,
$calculatedPrice
)
,
]
)
)
;
return
$cart
;
}
}
private
static
function
coverCart
(
string
$id
, ?string
$thumbnailString
, bool
$skipCover
= false
)
: Cart
{
$cart
=
(
new
Cart
(
'test'
)
)
->
add
(
self::
coverItem
(
$id
,
$thumbnailString
,
$skipCover
)
)
;
$cart
->
addDeliveries
(
self::
itemDelivery
(
self::
coverItem
(
$id
,
$thumbnailString
,
$skipCover
)
)
)
;
return
$cart
;
}
private
static
function
itemDelivery
(
LineItem
$lineItem
)
: DeliveryCollection
{
$delivery
=
new
Delivery
(
new
DeliveryPositionCollection
(
[
new
DeliveryPosition
(
$lineItem
->
getId
(
)
,
$lineItem
,
1,
new
CalculatedPrice
(
1.0, 1.0,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
,
new
DeliveryDate
(
new
\
DateTimeImmutable
(
)
,
new
\
DateTimeImmutable
(
)
)
)
,
]
)
,
$deliveryPositionCollection
->
add
(
new
DeliveryPosition
(
Uuid::
randomHex
(
)
,
$lineItem
,
$lineItem
->
getQuantity
(
)
,
$calculatedPrice
,
$deliveryDate
)
)
;
}
$cart
->
setDeliveries
(
new
DeliveryCollection
(
[
new
Delivery
(
$deliveryPositionCollection
,
$deliveryDate
,
new
ShippingMethodEntity
(
)
,
new
ShippingLocation
(
new
CountryEntity
(
)
, null, null
)
,
$calculatedPrice
)
,
]
)
)
;
return
$cart
;
}
}
$deliveryTime
=
$this
->
generateDeliveryTimeDummy
(
)
;
$cart
=
new
Cart
(
'test'
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$shippingMethod
=
new
ShippingMethodEntity
(
)
;
$shippingMethod
->
setId
(
'1'
)
;
$shippingMethod
->
setAvailabilityRuleId
(
'1'
)
;
$shippingMethod
->
setDeliveryTime
(
$deliveryTime
)
;
$shippingMethod
->
setActive
(
true
)
;
$deliveryDate
=
new
DeliveryDate
(
new
\
DateTime
(
)
,
new
\
DateTime
(
)
)
;
$delivery
=
new
Delivery
(
new
DeliveryPositionCollection
(
)
,
$deliveryDate
,
$shippingMethod
,
new
ShippingLocation
(
new
CountryEntity
(
)
, null, null
)
,
new
CalculatedPrice
(
5, 5,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
)
;
$cart
->
setDeliveries
(
new
DeliveryCollection
(
[
$delivery
]
)
)
;
$context
->
expects
(
static
::
once
(
)
)
->
method
(
'getRuleIds'
)
->
willReturn
(
[
'1'
]
)
;
$validator
=
new
DeliveryValidator
(
)
;
$errors
=
new
ErrorCollection
(
)
;
$countryState
->
setName
(
'country-state-name'
)
;
$shippingMethod
=
new
ShippingMethodEntity
(
)
;
$shippingMethod
->
setId
(
'shipping-method-id'
)
;
$shippingLocation
=
new
ShippingLocation
(
$country
, null, null
)
;
if
(
!
$withoutAddress
)
{
$shippingLocation
=
new
ShippingLocation
(
$country
,
$countryState
,
$this
->
getCustomerAddress
(
)
)
;
}
$deliveryCollection
=
new
DeliveryCollection
(
)
;
$delivery
=
new
Delivery
(
new
DeliveryPositionCollection
(
)
,
new
DeliveryDate
(
new
\
DateTimeImmutable
(
)
,
new
\
DateTimeImmutable
(
)
)
,
$shippingMethod
,
$shippingLocation
,
new
CalculatedPrice
(
1, 1,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
)
;
$deliveryCollection
->
add
(
$delivery
)
;
return
$deliveryCollection
;
}
100,
100,
$taxes
,
new
TaxRuleCollection
(
)
,
1
)
;
$lineItem
->
setPrice
(
$calculatedPrice
)
;
$cart
->
add
(
$lineItem
)
;
$deliveries
=
new
DeliveryCollection
(
[
new
Delivery
(
new
DeliveryPositionCollection
(
[
new
DeliveryPosition
(
$this
->ids->
get
(
'delivery-position-1'
)
,
$lineItem
,
1,
clone
$calculatedPrice
,
new
DeliveryDate
(
new
\
DateTimeImmutable
(
)
,
new
\
DateTimeImmutable
(
)
)
)
,
]
)
,
new
DeliveryDate
(
new
\
DateTimeImmutable
(
)
,
new
\
DateTimeImmutable
(
)
)
,
new
ShippingMethodEntity
(
)
,
$deliveryTime
=
$this
->
generateDeliveryTimeDummy
(
)
;
$shippingMethod
=
new
ShippingMethodEntity
(
)
;
$shippingMethod
->
setId
(
'1'
)
;
$shippingMethod
->
setName
(
'Express'
)
;
$shippingMethod
->
addTranslated
(
'name', 'Express'
)
;
$shippingMethod
->
setDeliveryTime
(
$deliveryTime
)
;
$shippingMethod
->
setAvailabilityRuleId
(
Uuid::
randomHex
(
)
)
;
$shippingMethod
->
setTaxType
(
ShippingMethodEntity::TAX_TYPE_AUTO
)
;
$deliveryDate
=
new
DeliveryDate
(
new
\
DateTime
(
)
,
new
\
DateTime
(
)
)
;
$delivery
=
new
Delivery
(
new
DeliveryPositionCollection
(
)
,
$deliveryDate
,
$shippingMethod
,
new
ShippingLocation
(
new
CountryEntity
(
)
, null, null
)
,
new
CalculatedPrice
(
10, 10,
new
CalculatedTaxCollection
(
)
,
new
TaxRuleCollection
(
)
)
)
;
$originalCart
->
setDeliveries
(
new
DeliveryCollection
(
[
$delivery
]
)
)
;
$calculatedCart
=
new
Cart
(
'calculated'
)
;
$taxes
,
new
TaxRuleCollection
(
)
,
1
)
;
$price
->
assign
(
[
'calculatedTaxes' =>
$taxes
]
)
;
$lineItem
->
setPrice
(
$price
)
;
$cart
->
add
(
$lineItem
)
;
$deliveries
=
new
DeliveryCollection
(
[
new
Delivery
(
new
DeliveryPositionCollection
(
[
new
DeliveryPosition
(
$this
->ids->
get
(
'delivery-position-1'
)
,
$lineItem
,
1,
$price
,
static
::
createMock
(
DeliveryDate::
class
)
)
,
]
)
,
static
::
createMock
(
DeliveryDate::
class
)
,
static
::
createMock
(
ShippingMethodEntity::
class
)
,
$deliveryPositions
->
add
(
$deliveryPosition
)
;
}
if
(
$orderDelivery
->
getShippingMethod
(
)
=== null
||
$orderDelivery
->
getShippingOrderAddress
(
)
=== null
||
$orderDelivery
->
getShippingOrderAddress
(
)
->
getCountry
(
)
=== null
)
{
continue
;
}
$cartDelivery
=
new
Delivery
(
$deliveryPositions
,
$deliveryDate
,
$orderDelivery
->
getShippingMethod
(
)
,
new
ShippingLocation
(
$orderDelivery
->
getShippingOrderAddress
(
)
->
getCountry
(
)
,
$orderDelivery
->
getShippingOrderAddress
(
)
->
getCountryState
(
)
,
null
)
,
$orderDelivery
->
getShippingCosts
(
)
)
;
$cartDelivery
->
addExtension
(
self::ORIGINAL_ID,
new
IdStruct
(
$orderDelivery
->
getId
(
)
)
)
;