Skip to content

[5.4] Add catalog pricing rule ID to purchasable/line item #3915

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

Open
wants to merge 7 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
5 changes: 5 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
- Added Promotional Price column to product index.

### Development
- Line item snapshots now contain the `catalogPricingRuleId` field. ([#3910](https://github.com/craftcms/commerce/issues/3910))
- Added the `localized` GraphQL product query field. ([#3783](https://github.com/craftcms/commerce/discussions/3783))

### Extensibility
- Added `craft\commerce\base\Gateway::getConfig()`.
- Added `craft\commerce\base\Gateway::getOrderCondition()`.
- Added `craft\commerce\base\Gateway::hasOrderCondition()`.
- Added `craft\commerce\base\Gateway::setOrderCondition()`.
- Added `craft\commerce\base\Purchasable::$catalogPricingRuleId`.
- Added `craft\commerce\base\Purchasable::getCatalogPricingRule()`.
- Added `craft\commerce\base\Gateway::setOrderCondition()`.
- Added `craft\commerce\base\Gateway::getOrderCondition()`.
- Added `craft\commerce\elements\Product::$defaultBasePromotionalPrice`
- Added `craft\commerce\elements\conditions\customers\ShippingMethodCustomerCondition`.
- Added `craft\commerce\elements\conditions\customers\ShippingRuleCustomerCondition`.
Expand Down
34 changes: 33 additions & 1 deletion src/base/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\commerce\helpers\Currency;
use craft\commerce\helpers\Localization;
use craft\commerce\helpers\Purchasable as PurchasableHelper;
use craft\commerce\models\CatalogPricingRule;
use craft\commerce\models\InventoryItem;
use craft\commerce\models\InventoryLevel;
use craft\commerce\models\LineItem;
Expand Down Expand Up @@ -184,6 +185,20 @@ abstract class Purchasable extends Element implements PurchasableInterface, HasS
*/
private ?float $_basePromotionalPrice = null;

/**
* The ID of the catalog pricing rule that is affecting the sale price of this purchasable.
*
* @var int|null
* @since 5.4.0
*/
public ?int $catalogPricingRuleId = null;

/**
* @var CatalogPricingRule|null
* @since 5.4.0
* @see getCatalogPricingRule()
*/
private ?CatalogPricingRule $_catalogPricingRule = null;

/**
* @var bool
Expand Down Expand Up @@ -614,6 +629,21 @@ public function setPromotionalPrice(?float $price): void
$this->_promotionalPrice = $price;
}

/**
* @return CatalogPricingRule|null
* @throws InvalidConfigException
* @throws SiteNotFoundException
* @since 5.4.0
*/
public function getCatalogPricingRule(): ?CatalogPricingRule
{
if ($this->_catalogPricingRule === null && $this->catalogPricingRuleId !== null) {
$this->_catalogPricingRule = Plugin::getInstance()->getCatalogPricingRules()->getCatalogPricingRuleById($this->catalogPricingRuleId, $this->storeId);
}

return $this->_catalogPricingRule;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -705,7 +735,9 @@ public function getTaxCategory(): TaxCategory
*/
public function getSnapshot(): array
{
return [];
return [
'catalogPricingRuleId' => $this->catalogPricingRuleId,
];
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/elements/db/PurchasableQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,33 @@ protected function beforePrepare(): bool
'subquery.price',
'subquery.promotionalPrice as promotionalPrice',
'subquery.salePrice as salePrice',
'catprice.catalogPricingRuleId as catalogPricingRuleId',
'inventoryitems.id as inventoryItemId',
]);

$this->query->leftJoin(Table::SITESTORES . ' sitestores', '[[elements_sites.siteId]] = [[sitestores.siteId]]');
$this->query->leftJoin(Table::PURCHASABLES_STORES . ' purchasables_stores', '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]');
$this->query->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]');

// Retrieve the catalog pricing rule ID used to determine the price
$customerId = $this->forCustomer;
if ($customerId === null) {
$customerId = Craft::$app->getUser()->getIdentity()?->id;
} elseif ($customerId === false) {
$customerId = null;
}
$cprIdQuery = Plugin::getInstance()
->getCatalogPricing()
->createCatalogPricesQuery(userId: $customerId)
->select([
'purchasableId',
'storeId',
'price',
new Expression('MIN([[catalogPricingRuleId]]) as [[catalogPricingRuleId]]'),
])
->groupBy(['cp.purchasableId', 'cp.storeId', 'cp.price']);
$this->query->leftJoin(['catprice' => $cprIdQuery], '[[catprice.purchasableId]] = [[commerce_purchasables.id]] AND [[catprice.storeId]] = [[sitestores.storeId]] AND [[catprice.price]] = [[subquery.salePrice]]');

$this->subQuery->addSelect([
'catalogprices.price',
'catalogprices.promotionalPrice',
Expand Down
1 change: 1 addition & 0 deletions src/models/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ private function _populateFromPurchasable(PurchasableInterface $purchasable): vo
}

$snapshot = [
// @TODO move these to base purchasable on next breaking change
'price' => $purchasable->getPrice(),
'sku' => $purchasable->getSku(),
'description' => $purchasable->getDescription(),
Expand Down
Loading