Skip to content

Conversation

@jeffstieler
Copy link
Contributor

All Submissions:

Changes proposed in this Pull Request:

Closes #28166.

This PR adds the storage of WC_Coupon data to Order Item Meta when Orders are created outside the Cart/Checkout context, like through the REST API or wp-admin Dashboard.

Without these changes, order coupon line item data can vary depending on the context the order was created in:

When you place an order via checkout (i.e. normal woocommerce flow) - coupon_data is added to the order's coupon_lines metadata here: https://github.com/woocommerce/woocommerce/blob/trunk/includes/class-wc-checkout.php#L626-L629

But when you add a coupon to an order programmatically, via apply_coupon in our case (or create the order programmaticall in full), that coupon_data metadata is not added at all.

How to test the changes in this Pull Request:

  1. Ensure you have a coupon you can apply to a new order
  2. Ensure you have a product you can add to the order
  3. Create a new order via the REST API (POST https://example.com/wp-json/wc/v3/orders):
{
    "status": "on-hold",
    "currency": "USD",
    "payment_method": "cod",
    "payment_method_title": "Cash on Delivery",
    "line_items": [
        {
            "product_id": 5,
            "quantity": 1
        }
    ],
    "coupon_lines": [
        {
            "code": "couponcode"
        }
    ]
}
  1. Verify that the resulting order response has meta_data in the coupon_lines that contains data like:
"meta_data": [
        {
          "id": 2016,
          "key": "coupon_data",
          "value": {
            "id": 130,
            "code": "couponcode",
            "amount": "7",
            "date_created": {
              "date": "2020-10-30 11:06:20.000000",
              "timezone_type": 3,
              "timezone": "Europe/Madrid"
            },
            "date_modified": {
              "date": "2020-10-30 11:07:19.000000",
              "timezone_type": 3,
              "timezone": "Europe/Madrid"
            },
            "date_expires": null,
            "discount_type": "percent",
            "description": "",
            "usage_count": 2,
            "individual_use": false,
            "product_ids": [],
            "excluded_product_ids": [],
            "usage_limit": 0,
            "usage_limit_per_user": 0,
            "limit_usage_to_x_items": null,
            "free_shipping": false,
            "product_categories": [],
            "excluded_product_categories": [],
            "exclude_sale_items": false,
            "minimum_amount": "",
            "maximum_amount": "",
            "email_restrictions": [],
            "virtual": false,
            "meta_data": []
          }
        }
      ]
  1. Create a new order through the wp-admin dashboard (WooCommerce > Orders > Add Order)
  2. Add a product ("Add item(s)")
  3. Add a coupon ("Apply coupon")
  4. Retrieve the order from the REST API (GET https://example.com/wp-json/wc/v3/orders/[ORDER_ID])
  5. Verify the coupon_lines contains meta_data like the previous example

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changelog entry

Enter a summary of all changes on this Pull Request. This will appear in the changelog if accepted.

Fix - Ensure coupon data is stored consistently when created outside of frontend checkout.

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@jeffstieler jeffstieler requested review from a team and peterfabian and removed request for a team November 30, 2021 22:33
Copy link
Contributor

@peterfabian peterfabian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, both from WP admin and via REST API. Thanks for also adding tests!

@peterfabian peterfabian merged commit edcffa9 into trunk Dec 7, 2021
@peterfabian peterfabian deleted the fix/28166-coupon-item-meta-data branch December 7, 2021 17:35
@peterfabian peterfabian added this to the 6.1.0 milestone Dec 7, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2021

Hi @peterfabian, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the status: needs changelog label
  • Add the status: needs testing instructions label

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New order via API coupons meta_data not present after

4 participants