WooCommerce Product Add-ons
The official WooCommerce Product Add‑Ons extension lets you offer extra options such as gift wrapping, custom messages, or size selections. When an agent creates a phone order, the plugin must capture these add‑on selections and display them correctly on the final order.
The code below copies the add‑on data into the cart item, forces the line item to be read‑only (so the agent cannot accidentally change the price), and formats the add‑on summary for display.
The Complete Code Snippet
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
add_filter('wpo_update_cart_cart_item_meta', function ($cart_item_meta, $item) { return array_merge($cart_item_meta, array( 'addons' => isset($item['addons']) ? $item['addons'] : null, )); }, 10, 2); add_filter('wpo_update_cart_loaded_product', function ($loaded_product, $item) { return array_merge($loaded_product, array( 'addons' => isset($item['addons']) ? $item['addons'] : null, 'readonly_price' => $item['line_subtotal'] / $item['quantity'], 'readonly_custom_meta_fields_html' => isset($item['addons']) ? wc_get_formatted_cart_item_data($item) : '', )); }, 10, 2); add_filter('wpo_product_calc_line_subtotal', function ($calc, $item) { return isset($item['addons']) && $item['addons'] ? false : $calc; }, 10, 2); add_filter('wpo_get_item_by_product', function ($product, $cart_item_data) { return array_merge($product, array( 'addons' => isset($cart_item_data['addons']) ? $cart_item_data['addons'] : null, 'readonly_price' => $cart_item_data['line_subtotal'] / $cart_item_data['quantity'], )); }, 10, 2); add_filter('wpo_cart_item_is_price_readonly', function ($is_readonly, $cart_item_data) { return isset($cart_item_data['addons']) && $cart_item_data['addons'] ? true : $is_readonly; }, 10, 2); |
What the code does
| Filter | Explanation |
|---|---|
wpo_update_cart_cart_item_meta | Merges the add‑on array into the cart item metadata. |
wpo_update_cart_loaded_product | Adds the add‑on data to the loaded product and calculates a read‑only price. It also calls wc_get_formatted_cart_item_data() to produce a human‑readable summary (e.g., “Gift wrap: Yes, Message: Happy Birthday”). |
wpo_product_calc_line_subtotal | Returns false when add‑ons exist, which tells the plugin not to automatically recalculate the line subtotal from the base product price. The actual total is determined by the add‑on prices plus the base product price. |
wpo_get_item_by_product | Copies the add‑on data and the read‑only price into the product item when the phone order interface loads. |
wpo_cart_item_is_price_readonly | Makes the price field read‑only in the cart table for any item that has add‑ons. This prevents the agent from accidentally overriding a price that includes complex add‑on costs. |
Why you need this code
Without these filters, add‑on selections would disappear when the agent adds the product to the phone order. The final order would contain only the base product, and the customer would not be charged for the extra options.
How to implement it
- Copy the complete code snippet.
- Add it as a new snippet in the Code Snippets plugin.
- Title it “Phone Orders – WooCommerce Product Add‑Ons”.
- Activate the snippet.
Testing the integration
- Create a product that has at least one Product Add‑On (e.g., a dropdown for gift wrapping).
- Add that product to a phone order and select an add‑on option.
- Complete the order.
- Go to WooCommerce → Orders and open the order details. The add‑on selections should appear as part of the line item.
- The price should reflect the base product price plus the add‑on cost.