Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug where not all data for new line item was passed to the resolve line item process. #3884

Open
wants to merge 3 commits into
base: 5.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed a bug where new line items did not expose their submitted quantity to the `craft\commerce\services\LineItems::EVENT_POPULATE_LINE_ITEM` event. ([#3883](https://github.com/craftcms/commerce/issues/3883))
- Fixed a bug where soft-deleted variants were not being restored when the product was restored. ([#3815](https://github.com/craftcms/commerce/issues/3815))
- Fixed a bug where inventory movements did not update the purchasable’s per-store cached stock total.
- Fixed a bug where transfers that contained deleted inventory items could not be updated.
Expand Down
20 changes: 13 additions & 7 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,15 @@ public function actionUpdateCart(): ?Response
$options = $this->request->getParam('options', []); // TODO Commerce 4 should only support key value only #COM-55
$qty = (int)$this->request->getParam('qty', 1);

$params = compact('qty', 'note', 'purchasableId', 'options');

if ($qty > 0) {
// We only want a new line item if they cleared the cart
if ($clearLineItems) {
$lineItem = Plugin::getInstance()->getLineItems()->create($this->_cart, compact('purchasableId', 'options'));
$lineItem = Plugin::getInstance()->getLineItems()->create($this->_cart, params: $params);
} else {
$lineItem = Plugin::getInstance()->getLineItems()->resolveLineItem($this->_cart, $purchasableId, $options);
// we are passing everything into params but need to pass purchasableId and options for now until we refactor
$lineItem = Plugin::getInstance()->getLineItems()->resolveLineItem($this->_cart, $params['purchasableId'], $params['options'], params: $params);
}

// New line items already have a qty of one.
Expand Down Expand Up @@ -216,15 +219,18 @@ public function actionUpdateCart(): ?Response

// Ignore zero value qty for multi-add forms https://github.com/craftcms/commerce/issues/330#issuecomment-384533139
if ($purchasable['qty'] > 0) {
$params = [
'purchasableId' => $purchasable['id'],
'options' => $purchasable['options'],
'note' => $purchasable['note'],
'qty' => $purchasable['qty'],
];

// We only want a new line item if they cleared the cart
if ($clearLineItems) {
$lineItem = Plugin::getInstance()->getLineItems()->create($this->_cart, [
'purchasableId' => $purchasable['id'],
'options' => $purchasable['options'],
]);
$lineItem = Plugin::getInstance()->getLineItems()->create($this->_cart, params: $params);
} else {
$lineItem = Plugin::getInstance()->getLineItems()->resolveLineItem($this->_cart, $purchasable['id'], $purchasable['options']);
$lineItem = Plugin::getInstance()->getLineItems()->resolveLineItem($this->_cart, $params['purchasableId'], $params['options'], $params);
}

// New line items already have a qty of one.
Expand Down
11 changes: 9 additions & 2 deletions src/services/LineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getAllLineItemsByOrderId(int $orderId): array
* @return LineItem
* @throws \Exception
*/
public function resolveLineItem(Order $order, int $purchasableId, array $options = []): LineItem
public function resolveLineItem(Order $order, int $purchasableId, array $options = [], array $params = []): LineItem
{
$signature = LineItemHelper::generateOptionsSignature($options);

Expand All @@ -191,7 +191,14 @@ public function resolveLineItem(Order $order, int $purchasableId, array $options
if ($result) {
$lineItem = new LineItem($result);
} else {
$lineItem = $this->create($order, compact('purchasableId', 'options'));
$params = array_merge([
'qty' => 1,
'options' => $options,
'note' => '',
'purchasableId' => $purchasableId,
], $params);

$lineItem = $this->create($order, $params);
}

return $lineItem;
Expand Down
Loading