Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setInStock example
protected
$entityManager
;
public
function
__construct
(
ModelManager
$entityManager
)
{
$this
->entityManager =
$entityManager
;
}
public
function
addProductDetail
(
OrderDetail
$detail
)
: void
{
$product
=
$this
->
getProductFromDetail
(
$detail
)
;
if
(
$product
)
{
$product
->
setInStock
(
$product
->
getInStock
(
)
-
$detail
->
getQuantity
(
)
)
;
$this
->entityManager->
persist
(
$product
)
;
$this
->entityManager->
flush
(
$product
)
;
}
}
public
function
updateProductDetail
(
OrderDetail
$detail
, ?string
$oldProductNumber
= null, ?int
$oldQuantity
= null, ?string
$newProductNumber
= null, ?int
$newQuantity
= null
)
: void
{
$oldQuantity
=
$oldQuantity
=== 0 ||
$oldQuantity
> 0 ?
$oldQuantity
:
$detail
->
getQuantity
(
)
;
$newQuantity
=
$newQuantity
=== 0 ||
$newQuantity
> 0 ?
$newQuantity
:
$detail
->
getQuantity
(
)
;
// If the position product has been changed, the old product stock must be increased based on the (old) ordering quantity.
if
(
!
$esd
)
{
$this
->
View
(
)
->
assign
(
[
'success' => false,
'message' =>
sprintf
(
'ESD by id %s not found',
$esdId
)
,
]
)
;
return
;
}
$freeSerialsCount
=
$this
->
getFreeSerialCount
(
$esdId
)
;
$variant
=
$esd
->
getArticleDetail
(
)
;
$variant
->
setInStock
(
$freeSerialsCount
)
;
$esd
->
fromArray
(
$this
->
Request
(
)
->
getPost
(
)
)
;
$this
->
getManager
(
)
->
flush
(
)
;
$this
->
View
(
)
->
assign
(
[
'data' =>
$this
->
Request
(
)
->
getPost
(
)
,
'success' => true,
]
)
;
}
/** * Event listener function of the product backend module. * Will be fired when the user deletes ESD */
$entityManager
=
$this
->
get
(
ModelManager::
class
)
;
$products
=
$this
->
getProductsOfOrder
(
$orderModel
)
;
foreach
(
$products
as
$product
)
{
$position
=
$this
->
getOrderPositionByProduct
(
$product
,
$orderModel
)
;
if
(
!
$position
)
{
continue
;
}
$product
->
setInStock
(
$product
->
getInStock
(
)
-
$position
->
getQuantity
(
)
)
;
$entityManager
->
persist
(
$product
)
;
}
}
/** * Helper function to check if the stock is valid if the article is on sale */
private
function
isValidStock
(
ProductVariant
$variant
, int
$newStock
)
: bool
{