Skip to content

Commit

Permalink
Merge branch 'bugfix/product-variant-revisions' of github.com:craftcm…
Browse files Browse the repository at this point in the history
…s/commerce into bugfix/product-variant-revisions
  • Loading branch information
lukeholder committed Oct 24, 2024
2 parents b1b8a80 + d2c02cf commit 8f0b679
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public string $schemaVersion = '5.2.0.4';
public string $schemaVersion = '5.2.0.6';

/**
* @inheritdoc
Expand Down
27 changes: 18 additions & 9 deletions src/base/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ public function afterSave(bool $isNew): void

if (!$this->propagating) {
$isOwnerDraftApplying = false;
$isOwnerRevisionApplying = false;

$owner = $this->getOwner();
$state = [
Expand All @@ -982,17 +983,23 @@ public function afterSave(bool $isNew): void
// If this is a nested element, check if the owner is a draft and is being applied
if ($this instanceof NestedElementInterface) {
$owner = $this->getOwner();
$isOwnerDraftApplying = $owner && $owner->getIsCanonical() && $owner->duplicateOf !== null && $owner->duplicateOf->getIsDraft();
$isOwnerDraftApplying = $owner
&& $owner->getIsCanonical()
&& $owner->duplicateOf !== null
&& $owner->duplicateOf->getIsDraft();

$isOwnerRevisionApplying = $owner
&& $owner->duplicateOf !== null
&& $owner->duplicateOf->getIsRevision();
}

if ($this->duplicateOf !== null && !$this->getIsRevision() && !$isOwnerDraftApplying) {

// TODO: remove this comment
// This code is being run when a revision is being restored and the variant is being put back as the main variant

$this->sku = PurchasableHelper::tempSku();
// Nullify inventory item so a new one is created
$this->inventoryItemId = null;
if (!$this->getIsRevision()) {
// Reset the purchasable's SKU when it is explicitly being duplicating
if ($this->duplicateOf !== null && !$isOwnerDraftApplying && !$isOwnerRevisionApplying) {
$this->sku = PurchasableHelper::tempSku();
// Nullify inventory item so a new one is created
$this->inventoryItemId = null;
}
}

$purchasable = PurchasableRecord::findOne($purchasableId);
Expand Down Expand Up @@ -1222,6 +1229,7 @@ protected function shippingCategoryFieldHtml(bool $static): string
'label' => Craft::t('commerce', 'Shipping Category'),
'options' => $options,
'value' => $this->shippingCategoryId,
'disabled' => $static,
]);
}

Expand Down Expand Up @@ -1251,6 +1259,7 @@ protected function taxCategoryFieldHtml(bool $static): string
'label' => Craft::t('commerce', 'Tax Category'),
'options' => $options,
'value' => $this->taxCategoryId,
'disabled' => $static,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ public function actionPurchasablesTable(): Response
->innerJoin(Table::PURCHASABLES_STORES . ' pstores', '[[purchasables.id]] = [[pstores.purchasableId]]')
->where(['elements.enabled' => true])
->andWhere(['pstores.storeId' => $store->id])
->andWhere(['elements.revisionId' => null])
->andWhere(['elements.draftId' => null])
->from(['purchasables' => Table::PURCHASABLES]);

// Are they searching for a SKU or purchasable description?
Expand Down
2 changes: 2 additions & 0 deletions src/fieldlayoutelements/PurchasableAllowedQtyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'value' => $element->minQty,
'placeholder' => Craft::t('commerce', 'Any'),
'title' => Craft::t('commerce', 'Minimum allowed quantity'),
'disabled' => $static,
]) .
Html::endTag('div') .
Html::tag('div', Craft::t('commerce', 'to'), ['class' => 'label light']) .
Expand All @@ -70,6 +71,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'value' => $element->maxQty,
'placeholder' => Craft::t('commerce', 'Any'),
'title' => Craft::t('commerce', 'Maximum allowed quantity'),
'disabled' => $static,
]) .
Html::endTag('div') .
Html::endTag('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
throw new InvalidArgumentException(static::class . ' can only be used in purchasable field layouts.');
}

return PurchasableHelper::availableForPurchaseInputHtml($element->availableForPurchase);
return PurchasableHelper::availableForPurchaseInputHtml($element->availableForPurchase, [
'disabled' => $static,
]);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/fieldlayoutelements/PurchasableDimensionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'class' => 'text',
'size' => 10,
'unit' => Plugin::getInstance()->getSettings()->dimensionUnits,
'disabled' => $static,
]), ['id' => 'length', 'label' => Craft::t('commerce', 'Length')]) .
Cp::fieldHtml(Cp::textHtml([
'id' => 'width',
Expand All @@ -85,6 +86,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'class' => 'text',
'size' => 10,
'unit' => Plugin::getInstance()->getSettings()->dimensionUnits,
'disabled' => $static,
]), ['id' => 'width', 'label' => Craft::t('commerce', 'Width')]) .
Cp::fieldHtml(Cp::textHtml([
'id' => 'height',
Expand All @@ -93,6 +95,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'class' => 'text',
'size' => 10,
'unit' => Plugin::getInstance()->getSettings()->dimensionUnits,
'disabled' => $static,
]), ['id' => 'height', 'label' => Craft::t('commerce', 'Height')]) .
Html::endTag('div');
}
Expand Down
1 change: 1 addition & 0 deletions src/fieldlayoutelements/PurchasableFreeShippingField.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'name' => 'freeShipping',
'small' => true,
'on' => $element->freeShipping,
'disabled' => $static,
]);
}

Expand Down
4 changes: 4 additions & 0 deletions src/fieldlayoutelements/PurchasablePriceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
}
}

$toggleContent = $static ? null : $toggleContent;

$currency = $element->getStore()->getCurrency();

return Html::beginTag('div', [
Expand All @@ -161,6 +163,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'currencyLabel' => $currency->getCode(),
'required' => true,
'errors' => $element->getErrors('basePrice'),
'disabled' => $static,
]), [
'id' => 'base-price',
'label' => Craft::t('commerce', 'Price'),
Expand All @@ -174,6 +177,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'currency' => $currency->getCode(),
'currencyLabel' => $currency->getCode(),
'errors' => $element->getErrors('basePromotionalPrice'),
'disabled' => $static,
]), [
'id' => 'promotional-price',
'label' => Craft::t('commerce', 'Promotional Price'),
Expand Down
1 change: 1 addition & 0 deletions src/fieldlayoutelements/PurchasablePromotableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'name' => 'promotable',
'small' => true,
'on' => $element->promotable,
'disabled' => $static,
]);
}

Expand Down
4 changes: 3 additions & 1 deletion src/fieldlayoutelements/PurchasableSkuField.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
if ($element instanceof Variant && $element->getOwner()->getType()->skuFormat !== null && !$element->id) {
$html .= Html::hiddenInput('sku', '');
} else {
$html .= PurchasableHelper::skuInputHtml($element->getSkuAsText());
$html .= PurchasableHelper::skuInputHtml($element->getSkuAsText(), [
'disabled' => $static,
]);
}

return $html;
Expand Down
115 changes: 62 additions & 53 deletions src/fieldlayoutelements/PurchasableStockField.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,72 +114,80 @@ public function inputHtml(ElementInterface $element = null, bool $static = false

$inventoryLevelTableRows .= Html::beginTag('tr') .
Html::beginTag('td') .
$inventoryLevel->getInventoryLocation()->name .
$inventoryLevel->getInventoryLocation()->name .
Html::endTag('td') .
Html::beginTag('td') .
Html::beginTag('div', ['class' => 'flex']) .
Html::tag('div', (string)$inventoryLevel->availableTotal, [
'id' => $updatedValueId,
]) .
Html::tag('div',Html::button(Craft::t('commerce', ''),
[
'class' => 'btn menubtn action-btn',
'id' => $editUpdateQuantityInventoryItemId,
])) .
Html::endTag('div') .
Html::beginTag('div', ['class' => 'flex']) .
Html::tag('div', (string)$inventoryLevel->availableTotal, [
'id' => $updatedValueId,
]) .
(!$static ? Html::tag('div',Html::button(Craft::t('commerce', ''),
[
'class' => 'btn menubtn action-btn',
'id' => $editUpdateQuantityInventoryItemId,
])) : '') .
Html::endTag('div') .
Html::endTag('td') .
Html::beginTag('td') .
(Craft::$app->getUser()->checkPermission('commerce-manageInventoryStockLevels') ?
Html::a(
Craft::t('commerce', 'Manage'),
UrlHelper::cpUrl('commerce/inventory/levels/' . $inventoryLevel->getInventoryLocation()->handle, [
'inventoryItemId' => $inventoryLevel->getInventoryItem()->id,
]),
[
'target' => '_blank',
'class' => 'btn small',
'id' => $editUpdateQuantityInventoryItemId,
'aria-label' => Craft::t('app', 'Open in a new tab'),
'data-icon' => 'external',
]
) : '') .
(!$static ? Html::beginTag('td') .
(Craft::$app->getUser()->checkPermission('commerce-manageInventoryStockLevels') ?
Html::a(
Craft::t('commerce', 'Manage'),
UrlHelper::cpUrl('commerce/inventory/levels/' . $inventoryLevel->getInventoryLocation()->handle, [
'inventoryItemId' => $inventoryLevel->getInventoryItem()->id,
]),
[
'target' => '_blank',
'class' => 'btn small',
'id' => $editUpdateQuantityInventoryItemId,
'aria-label' => Craft::t('app', 'Open in a new tab'),
'data-icon' => 'external',
]
) : '') : '') .
Html::endTag('td') .
Html::endTag('tr');
}

$inventoryLevelsTable = Html::beginTag('table', ['class' => 'data fullwidth', 'style' => 'margin-top:5px;']) .
Html::beginTag('thead') .
Html::beginTag('tr') .
Html::beginTag('th') .
Craft::t('commerce', 'Location') .
Html::endTag('th') .
Html::beginTag('tr') .
Html::beginTag('th') .
Craft::t('commerce', 'Available') .
Craft::t('commerce', 'Location') .
Html::endTag('th') .
Html::beginTag('th') .
Craft::t('commerce', 'Manage') .
Html::endTag('th') .
Html::endTag('tr') .
Html::beginTag('th') .
Craft::t('commerce', 'Available') .
Html::endTag('th') .


(!$static ? Html::beginTag('th') .
Craft::t('commerce', 'Manage') .
Html::endTag('th') : '') .



Html::endTag('tr') .
Html::endTag('thead') .

Html::beginTag('tbody') .
$inventoryLevelTableRows .
Html::beginTag('tr') .
Html::beginTag('td', ['colspan' => '2']) .
$availableStockLabel .
Html::beginTag('td') .
Html::a(
Craft::t('commerce', 'Edit'),
'#',
[
'class' => 'btn small',
'id' => $editInventoryItemId,
'aria-label' => Craft::t('app', 'Edit Inventory Item'),
'data-icon' => 'edit',
]
) .
Html::endTag('td') .
Html::endTag('td') .
Html::endTag('tr') .
$inventoryLevelTableRows .
Html::beginTag('tr') .
Html::beginTag('td', ['colspan' => '2']) .
$availableStockLabel .
Html::endTag('td') .

(!$static ? Html::beginTag('td') .
Html::a(
Craft::t('commerce', 'Edit'),
'#',
[
'class' => 'btn small',
'id' => $editInventoryItemId,
'aria-label' => Craft::t('app', 'Edit Inventory Item'),
'data-icon' => 'edit',
]
) .
Html::endTag('td') : '') .

Html::endTag('tr') .
Html::endTag('tbody') .
Html::endTag('table');

Expand All @@ -190,6 +198,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'small' => true,
'on' => $element->inventoryTracked,
'toggle' => $inventoryItemTrackedId,
'disabled' => $static,
];

return Html::beginTag('div') .
Expand Down
1 change: 1 addition & 0 deletions src/fieldlayoutelements/PurchasableWeightField.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function inputHtml(ElementInterface $element = null, bool $static = false
'size' => 10,
'unit' => Plugin::getInstance()->getSettings()->weightUnits,
'placeholder' => Craft::t('commerce', 'Weight'),
'disabled' => $static,
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/fieldlayoutelements/VariantsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ protected function inputHtml(ElementInterface $element = null, bool $static = fa
Craft::$app->getView()->registerDeltaName($this->attribute());

return $element->getVariantManager()->getIndexHtml($element, [
'canCreate' => true,
'canCreate' => !$static,
'allowedViewModes' => [ElementIndexViewMode::Cards, ElementIndexViewMode::Table],
'sortable' => true,
'sortable' => !$static,
'fieldLayouts' => [$element->getType()->getVariantFieldLayout()],
]);
}
Expand Down
Loading

0 comments on commit 8f0b679

Please sign in to comment.