craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
218 stars 170 forks source link

[4.x]: Shipping and BillingAddress never the same #2978

Closed vandres closed 2 years ago

vandres commented 2 years ago

What happened?

Description

I am seeing an issue with billingAddressSameAsShipping in our instance. I can see the same problem in the Spoke & Chain demo instance.

When selecting billingAddressSameAsShipping and setting a shipping address, a billing address is set, but it is never the same as the shipping address. So the following code from the checkout summary always results to false:

{% if cart.shippingAddressId == cart.billingAddressId %}
  <p>{{ 'Same as shipping.'|t }}</p>
{% elseif cart.billingAddress %}
  {{ craft.app.addresses.formatAddress(cart.billingAddress)|raw }}
{% endif %}

Is that just an error in the demo code and the comparison should use "sourceXAddressId" instead?

Steps to reproduce

  1. Create a cart by adding a product
  2. Go to checkout
  3. Select "Use address for billing"
  4. Proceed to summary

Expected behavior

I expect to see "Same as shipping."

Actual behavior

I see two full addresses. In the database I can also see separate addresses created for that order and two different ids on the order. What I see is two times the same "sourceXAddressId"

image

Craft CMS version

4.2.5

Craft Commerce version

4.1.2

PHP version

8.1

Operating system and version

MacOS, Debian Docker

Database type and version

MariaDB 10.5

Image driver and version

-

Installed plugins and versions

  "require": {
    "craftcms/cms": "^4.2.0.2",
    "craftcms/commerce": "^4.1.0",
    "craftcms/commerce-paypal-checkout": "^2.1.0.2",
    "craftcms/redactor": "^3.0",
    "mmikkel/cp-field-inspect": "^1.4",
    "nystudio107/craft-vite": "^4.0",
    "php-http/curl-client": "^2.2",
    "putyourlightson/craft-elements-panel": "^2.0",
    "putyourlightson/craft-sprig": "^2.1",
    "sebastianlenz/linkfield": "^2.1",
    "spatie/craft-ray": "^2.0",
    "spicyweb/craft-neo": "^3.2",
    "typesense/typesense-php": "^4.8",
    "vaersaagod/geomate": "^2.0",
    "verbb/expanded-singles": "^2.0",
    "verbb/field-manager": "^3.0",
    "verbb/tablemaker": "^4.0",
    "verbb/wishlist": "^2.0",
    "vlucas/phpdotenv": "^5.4.0"
  },
  "require-dev": {
    "deployer/deployer": "^7.0",
    "roave/security-advisories": "dev-master",
    "yiisoft/yii2-shell": "^2.0.3"
  },
robzor commented 2 years ago

Hi @vandres I think I had the same problem as you, in the short term I've done this:

{% set areCartAddressesTheSame = (shippingAddress|address == billingAddress|address) ? 1 : 0 %}

I had to use the |address filter pipe otherwise the site would white screen of death.

nfourtythree commented 2 years ago

Hi @vandres

Addresses on the Order element a stored as separate IDs by design.

If you would like to check if the addresses are the same you can simply call the hasMatchingAddresses() method on the Order element e.g.

Are the addresses the same: {{ cart.hasMatchingAddresses() ? 'Yes' : 'No' }}

You mentioned some existing twig code that was causing confusion, was that in the Commerce example templates or in the Spoke & Chain demo project?

Thanks.

vandres commented 2 years ago

@nfourtythree I took the code from Spoke & Chain

Thanks for the help, will use the suggested method instead

nfourtythree commented 2 years ago

@vandres thank you for letting us know. We will look to fix the demo project shortly.

Thanks!

robzor commented 2 years ago

Ooh, I didn't know about hasMatchingAddresses(), thanks @nfourtythree :)