Skip to content

Conversation

@Konamiman
Copy link
Contributor

@Konamiman Konamiman commented May 7, 2025

Changes proposed in this Pull Request:

This pull request adds support for Cost of Goods Sold to orders with refunds:

  • The value calculated by calculate_cogs_total_value for an order will have the combined cost of the refunded items substracted.
  • The cost value for an order will be recalculated when a refund is created with wc_create_refund.
  • The REST API endpoint that returns the refunds of an order (/wp-json/wc/v3/orders/<order id>/refunds, note that it's for v3 only) now includes the cost value for the refund (the one substracted from the order cost) as well as the cost of each line item of the refund.
  • The "cost" column in the order details page in admin will include the cost associated to the refunded items in each line.
  • Additionally, hovering the "cost" column will display a tooltip with the cost per unit for the product in the line. The tooltip can be customized by using the new woocommerce_order_item_cogs_per_item_tooltip filter.

Screenshots or screen recordings:

image

image

How to test the changes in this Pull Request:

  1. Enable the Cost of Goods Sold feature (WooCommerce - Settings - Advanced - Features).

  2. Create a few products with a non-zero value for the Cost of Goods field (see Add Cost of Goods Sold fields in product editors in admin #54399).

  3. In the admin area create an order with the above products, about 10 units of each so that you can create multiple refunds.

  4. Issue a few partial refunds. Verify that the "Cost" column now displays the individual cost of the refunded item times the number of refunded items.

  5. Also verify that after creating each refund the total cost value of the order (displayed in the summary area under the order items as "Cost total") updates (gets decreased) accordingly.

  6. Hover the mouse pointer in the "Cost" column of one of the line items with refunds and verify that the tooltip appears with the correct value.

  7. You can test the new filter with the following snippet:

add_filter( 'woocommerce_order_item_cogs_per_item_tooltip', function($tooltip_text, $cost_per_item, $formatted_cost_per_item, $order_item ) {
	return "Product " . $order_item->get_product_id() . ", cost per item $cost_per_item (formatted: $formatted_cost_per_item)";
}, 10, 4);
  1. Issue a GET request to the following REST API endpoint: /wp-json/wc/v3/orders/<order id>/refunds. Verify that there's a "cost_of_goods_sold" field with the correct value for each refund, and also for each product line item within each refund.

  2. Issue a OPTIONS request to the same endpoint, verify that the "cost_of_goods_sold" fields for both the refunds and their line items get a proper schema entry.

Changelog entry

  • Automatically create a changelog entry from the details below.
  • This Pull Request does not require a changelog entry. (Comment required below)
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Changelog Entry Comment

Comment

@Konamiman Konamiman self-assigned this May 7, 2025
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label May 7, 2025
@Konamiman Konamiman marked this pull request as ready for review May 7, 2025 16:09
@woocommercebot woocommercebot requested a review from a team May 7, 2025 16:09
@Konamiman Konamiman requested a review from a team May 7, 2025 16:09
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2025

Testing Guidelines

Hi @prettyboymp @woocommerce/flux,

Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed.

Reminder: PR reviewers are required to document testing performed. This includes:

  • 🖼️ Screenshots or screen recordings.
  • 📝 List of functionality tested / steps followed.
  • 🌐 Site details (environment attributes such as hosting type, plugins, theme, store size, store age, and relevant settings).
  • 🔍 Any analysis performed, such as assessing potential impacts on environment attributes and other plugins, conducting performance profiling, or using LLM/AI-based analysis.

⚠️ Within the testing details you provide, please ensure that no sensitive information (such as API keys, passwords, user data, etc.) is included in this public issue.

@Konamiman Konamiman changed the title Substract the cost of refunded items when calculating Cost of Goods Sold for an order Take in acount refunds when calculating Cost of Goods Sold for an order May 7, 2025
@Konamiman Konamiman changed the title Take in acount refunds when calculating Cost of Goods Sold for an order Take in account refunds when calculating Cost of Goods Sold for an order May 7, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2025

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

@prettyboymp prettyboymp self-requested a review May 7, 2025 17:20
Copy link
Contributor

@prettyboymp prettyboymp left a comment

Choose a reason for hiding this comment

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

Tested well. I didn't see anything specific that I have concerns with.

Looks like you'll need to apply an ignore comment to the lint rule. Preapproving.

@Konamiman Konamiman merged commit e7eddf1 into trunk May 9, 2025
36 checks passed
@Konamiman Konamiman deleted the cogs/add-cogs-to-refunds branch May 9, 2025 07:47
@github-actions github-actions bot added this to the 9.9.0 milestone May 9, 2025
@Konamiman
Copy link
Contributor Author

@prettyboymp Thank you. Instead of adding a linting ignore for that $object argument I renamed it to $data_object which is supposedly the right thing to do ™️ .

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

Labels

plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants