Skip to content

Commit

Permalink
Merge branch 'release/4.2.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed May 25, 2023
2 parents 62e54ba + 145ade2 commit 48c233d
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 506 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Craft Commerce

## 4.2.9 - 2023-05-25

- The `commerce/cart/update-cart` action now accepts `clearAddresses`, `clearBillingAddress`, and `clearShippingAddress` params.
- Fixed a JavaScript error that occurred when switching control panel tabs on small screens. ([#3162](https://github.com/craftcms/commerce/issues/3162))
- Fixed a bug where the `commerce/upgrade` command wasn’t migrating discounts’ and coupons’ Max Uses values properly. ([#2947](https://github.com/craftcms/commerce/issues/2947))

## 4.2.8 - 2023-05-03

- Added `craft\commerce\services\Customers::EVENT_UPDATE_PRIMARY_PAYMENT_SOURCE`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main",
"craftcms/redactor": "*",
"vlucas/phpdotenv": "^3.4"
"vlucas/phpdotenv": "^5.5.0"
},
"autoload": {
"psr-4": {
Expand Down
853 changes: 419 additions & 434 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example-templates/dist/shop/_private/layouts/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Common, top-level layout template.
let states = {{ craft.commerce.store.getStore().getAdministrativeAreasListByCountryCode()|json_encode|raw }};
{% set usedFields = {} %}
{% for countryCode in craft.app.addresses.getCountryRepository().getAll()|keys %}
{% set usedFields = usedFields|merge({ (countryCode): craft.app.addresses.getAddressFormatRepository().get(countryCode).getUsedFields()|merge([
{% set usedFields = usedFields|merge({ (countryCode): craft.app.addresses.getUsedFields(countryCode)|merge([
'fullName',
'latLong',
'organizationTaxId',
Expand All @@ -140,7 +140,7 @@ let states = {{ craft.commerce.store.getStore().getAdministrativeAreasListByCoun
]) }) %}
{% endfor %}
let usedAddressFieldsByCountryCode = {{ usedFields|json_encode|raw }};

console.log(usedAddressFieldsByCountryCode);
function hideAddressFields(selectorTemplate) {
const fields = document.querySelectorAll('.' + selectorTemplate.replace('placeHolder', 'js-address-field'));
if (!fields.length) {
Expand Down
4 changes: 2 additions & 2 deletions example-templates/src/shop/_private/layouts/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Common, top-level layout template.
let states = {{ craft.commerce.store.getStore().getAdministrativeAreasListByCountryCode()|json_encode|raw }};
{% set usedFields = {} %}
{% for countryCode in craft.app.addresses.getCountryRepository().getAll()|keys %}
{% set usedFields = usedFields|merge({ (countryCode): craft.app.addresses.getAddressFormatRepository().get(countryCode).getUsedFields()|merge([
{% set usedFields = usedFields|merge({ (countryCode): craft.app.addresses.getUsedFields(countryCode)|merge([
'fullName',
'latLong',
'organizationTaxId',
Expand All @@ -140,7 +140,7 @@ let states = {{ craft.commerce.store.getStore().getAdministrativeAreasListByCoun
]) }) %}
{% endfor %}
let usedAddressFieldsByCountryCode = {{ usedFields|json_encode|raw }};

console.log(usedAddressFieldsByCountryCode);
function hideAddressFields(selectorTemplate) {
const fields = document.querySelectorAll('.' + selectorTemplate.replace('placeHolder', 'js-address-field'));
if (!fields.length) {
Expand Down
136 changes: 84 additions & 52 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,30 @@ private function _getCart(bool $forceSave = false): Order
private function _setAddresses(): void
{
$currentUser = Craft::$app->getUser()->getIdentity();

$setShippingAddress = true;
if ($this->request->getParam('clearShippingAddress') !== null) {
$this->_cart->setShippingAddress(null);
$this->_cart->sourceShippingAddressId = null;
$setShippingAddress = false;
}

$setBillingAddress = true;
if ($this->request->getParam('clearBillingAddress') !== null) {
$this->_cart->setBillingAddress(null);
$this->_cart->sourceBillingAddressId = null;
$setBillingAddress = false;
}

if ($this->request->getParam('clearAddresses') !== null) {
$this->_cart->setShippingAddress(null);
$this->_cart->sourceShippingAddressId = null;
$this->_cart->setBillingAddress(null);
$this->_cart->sourceBillingAddressId = null;
$setBillingAddress = false;
$setShippingAddress = false;
}

// Copy address options
$shippingIsBilling = $this->request->getParam('shippingAddressSameAsBilling');
$billingIsShipping = $this->request->getParam('billingAddressSameAsShipping');
Expand All @@ -541,68 +565,72 @@ private function _setAddresses(): void
$shippingAddressId = $this->request->getParam('shippingAddressId');
$billingAddressId = $this->request->getParam('billingAddressId');

// Shipping address
if ($shippingAddressId && !$shippingIsBilling) {
/** @var Address|null $userShippingAddress */
$userShippingAddress = Collection::make($currentUser->getAddresses())->firstWhere('id', $shippingAddressId);
if ($setShippingAddress) {
// Shipping address
if ($shippingAddressId && !$shippingIsBilling) {
/** @var Address|null $userShippingAddress */
$userShippingAddress = Collection::make($currentUser->getAddresses())->firstWhere('id', $shippingAddressId);

// If a user's address ID has been submitted duplicate the address to the order
if ($userShippingAddress) {
$this->_cart->sourceShippingAddressId = $shippingAddressId;
// If a user's address ID has been submitted duplicate the address to the order
if ($userShippingAddress) {
$this->_cart->sourceShippingAddressId = $shippingAddressId;

/** @var Address $cartShippingAddress */
$cartShippingAddress = Craft::$app->getElements()->duplicateElement($userShippingAddress, ['ownerId' => $this->_cart->id]);
$this->_cart->setShippingAddress($cartShippingAddress);
/** @var Address $cartShippingAddress */
$cartShippingAddress = Craft::$app->getElements()->duplicateElement($userShippingAddress, ['ownerId' => $this->_cart->id]);
$this->_cart->setShippingAddress($cartShippingAddress);

if ($billingIsShipping) {
$this->_cart->sourceBillingAddressId = $userShippingAddress->id;
$this->_cart->setBillingAddress($cartShippingAddress);
if ($billingIsShipping) {
$this->_cart->sourceBillingAddressId = $userShippingAddress->id;
$this->_cart->setBillingAddress($cartShippingAddress);
}
}
}
} elseif ($shippingAddress && !$shippingIsBilling) {
$this->_cart->sourceShippingAddressId = null;
$this->_cart->setShippingAddress($shippingAddress);
} elseif ($shippingAddress && !$shippingIsBilling) {
$this->_cart->sourceShippingAddressId = null;
$this->_cart->setShippingAddress($shippingAddress);

if (!empty($shippingAddress['fields']) && $this->_cart->getShippingAddress()) {
$this->_cart->getShippingAddress()->setFieldValues($shippingAddress['fields']);
}
if (!empty($shippingAddress['fields']) && $this->_cart->getShippingAddress()) {
$this->_cart->getShippingAddress()->setFieldValues($shippingAddress['fields']);
}

if ($billingIsShipping) {
$this->_cart->sourceBillingAddressId = null;
$this->_cart->setBillingAddress($this->_cart->getShippingAddress());
if ($billingIsShipping) {
$this->_cart->sourceBillingAddressId = null;
$this->_cart->setBillingAddress($this->_cart->getShippingAddress());
}
}
}

// Billing address
if ($billingAddressId && !$billingIsShipping) {
/** @var Address|null $userBillingAddress */
$userBillingAddress = Collection::make($currentUser->getAddresses())->firstWhere('id', $billingAddressId);

// If a user's address ID has been submitted duplicate the address to the order
if ($userBillingAddress) {
$this->_cart->sourceBillingAddressId = $billingAddressId;
if ($setBillingAddress) {
if ($billingAddressId && !$billingIsShipping) {
/** @var Address|null $userBillingAddress */
$userBillingAddress = Collection::make($currentUser->getAddresses())->firstWhere('id', $billingAddressId);

// If a user's address ID has been submitted duplicate the address to the order
if ($userBillingAddress) {
$this->_cart->sourceBillingAddressId = $billingAddressId;

/** @var Address $cartBillingAddress */
$cartBillingAddress = Craft::$app->getElements()->duplicateElement($userBillingAddress, ['ownerId' => $this->_cart->id]);
$this->_cart->setBillingAddress($cartBillingAddress);

if ($shippingIsBilling) {
$this->_cart->sourceShippingAddressId = $userBillingAddress->id;
$this->_cart->setShippingAddress($cartBillingAddress);
}
}
} elseif ($billingAddress && !$billingIsShipping) {
$this->_cart->sourceBillingAddressId = null;
$this->_cart->setBillingAddress($billingAddress);

/** @var Address $cartBillingAddress */
$cartBillingAddress = Craft::$app->getElements()->duplicateElement($userBillingAddress, ['ownerId' => $this->_cart->id]);
$this->_cart->setBillingAddress($cartBillingAddress);
if (!empty($billingAddress['fields']) && $this->_cart->getBillingAddress()) {
$this->_cart->getBillingAddress()->setFieldValues($billingAddress['fields']);
}

if ($shippingIsBilling) {
$this->_cart->sourceShippingAddressId = $userBillingAddress->id;
$this->_cart->setShippingAddress($cartBillingAddress);
$this->_cart->sourceShippingAddressId = null;
$this->_cart->setShippingAddress($this->_cart->getBillingAddress());
}
}
} elseif ($billingAddress && !$billingIsShipping) {
$this->_cart->sourceBillingAddressId = null;
$this->_cart->setBillingAddress($billingAddress);

if (!empty($billingAddress['fields']) && $this->_cart->getBillingAddress()) {
$this->_cart->getBillingAddress()->setFieldValues($billingAddress['fields']);
}

if ($shippingIsBilling) {
$this->_cart->sourceShippingAddressId = null;
$this->_cart->setShippingAddress($this->_cart->getBillingAddress());
}
}

// Estimated Shipping Address
Expand All @@ -629,17 +657,21 @@ private function _setAddresses(): void
$this->_cart->setEstimatedBillingAddress($estimatedBillingAddress);
}


$this->_cart->billingSameAsShipping = (bool)$billingIsShipping;
$this->_cart->shippingSameAsBilling = (bool)$shippingIsBilling;
$this->_cart->estimatedBillingSameAsShipping = (bool)$estimatedBillingIsShipping;

// Set primary addresses
if ($this->request->getBodyParam('makePrimaryShippingAddress')) {
$this->_cart->makePrimaryShippingAddress = true;
if ($setShippingAddress) {
if ($this->request->getBodyParam('makePrimaryShippingAddress')) {
$this->_cart->makePrimaryShippingAddress = true;
}
}

if ($this->request->getBodyParam('makePrimaryBillingAddress')) {
$this->_cart->makePrimaryBillingAddress = true;
if ($setBillingAddress) {
if ($this->request->getBodyParam('makePrimaryBillingAddress')) {
$this->_cart->makePrimaryBillingAddress = true;
}
}
}
}
2 changes: 1 addition & 1 deletion src/elements/db/VariantQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ protected function beforePrepare(): bool
$this->subQuery->andWhere(Db::parseParam('commerce_variants.price', $this->price));
}

if (isset($this->isDefault) && $this->isDefault !== null) {
if (isset($this->isDefault)) {
$this->subQuery->andWhere(Db::parseBooleanParam('commerce_variants.isDefault', $this->isDefault, false));
}

Expand Down
6 changes: 5 additions & 1 deletion src/migrations/m211118_101920_split_coupon_codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ public function safeUp(): bool

if (!empty($discountsWithCodes)) {
$coupons = array_map(static function($discount) use ($codeUsage) {
$maxUses = $discount['totalDiscountUseLimit'] !== null && $discount['totalDiscountUseLimit'] > 0
? $discount['totalDiscountUseLimit']
: null;

$row['code'] = $discount['code'];
$row['discountId'] = $discount['id'];
$row['uses'] = $codeUsage[$discount['code']] ?? 0;
$row['maxUses'] = $discount['totalDiscountUseLimit'] ?? 0;
$row['maxUses'] = $maxUses;
$row['dateCreated'] = $discount['dateCreated'];
$row['dateUpdated'] = $discount['dateUpdated'];
$row['uid'] = StringHelper::UUID();
Expand Down
12 changes: 6 additions & 6 deletions src/templates/promotions/discounts/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
{% endif %}

{% set tabs = {
0: {'label':'Discount'|t('commerce'),'url':'#discount','class':discountClasses},
1: {'label':'Coupons'|t('commerce'),'url':'#coupons','class':couponClasses},
2: {'label':'Matching Items'|t('commerce'),'url':'#matching-items','class':matchingItemsClasses},
3: {'label':'Conditions Rules'|t('commerce'),'url':'#condition-rules','class':conditionRulesClasses},
4: {'label':'Conditions'|t('commerce'),'url':'#conditions','class':conditionsClasses},
5: {'label':'Actions'|t('commerce'),'url':'#actions'}
discount: {'label':'Discount'|t('commerce'),'url':'#discount','class':discountClasses},
coupons: {'label':'Coupons'|t('commerce'),'url':'#coupons','class':couponClasses},
matchingItems: {'label':'Matching Items'|t('commerce'),'url':'#matching-items','class':matchingItemsClasses},
conditionRules: {'label':'Conditions Rules'|t('commerce'),'url':'#condition-rules','class':conditionRulesClasses},
conditions: {'label':'Conditions'|t('commerce'),'url':'#conditions','class':conditionsClasses},
actions: {'label':'Actions'|t('commerce'),'url':'#actions'}
} %}

{% set couponsTable = {
Expand Down
8 changes: 4 additions & 4 deletions src/templates/promotions/sales/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
{% endif %}

{% set tabs = {
0: {'label':'Sale'|t('commerce'),'url':'#sale','class': saleClasses},
1: {'label':'Matching Items'|t('commerce'),'url':'#matching-items'},
2: {'label':'Conditions'|t('commerce'),'url':'#conditions'},
3: {'label':'Actions'|t('commerce'),'url':'#actions','class': actionClasses}
sale: {'label':'Sale'|t('commerce'),'url':'#sale','class': saleClasses},
matchingItems: {'label':'Matching Items'|t('commerce'),'url':'#matching-items'},
conditions: {'label':'Conditions'|t('commerce'),'url':'#conditions'},
actions: {'label':'Actions'|t('commerce'),'url':'#actions','class': actionClasses}
} %}

{% hook "cp.commerce.sales.edit" %}
Expand Down
6 changes: 3 additions & 3 deletions src/templates/shipping/shippingrules/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
{% import "commerce/_includes/forms/commerceForms" as commerceForms %}

{% set tabs = {
0: {'label':'Rule'|t('commerce'),'url':'#rule-tab'},
1: {'label':'Conditions'|t('commerce'),'url':'#conditions-tab'},
2: {'label':'Costs'|t('commerce'),'url':'#costs-tab'}
rule: {'label':'Rule'|t('commerce'),'url':'#rule-tab'},
conditions: {'label':'Conditions'|t('commerce'),'url':'#conditions-tab'},
costs: {'label':'Costs'|t('commerce'),'url':'#costs-tab'}
} %}

{% block actionButton %}
Expand Down

0 comments on commit 48c233d

Please sign in to comment.