WooCommerce TM Extra Product Options
Phone Orders captures all add‑on data from ThemeHigh Extra Product Options and stores it correctly on the final order. This article provides a complete code snippet that preserves the extra options and ensures the prices remain accurate.
The Complete Code Snippet
Copy and paste the following code into your child theme’s functions.php file or via the Code Snippets plugin.
|
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
add_filter('wpo_update_cart_cart_item_meta', function ($cart_item_meta, $item) { return array_merge($cart_item_meta, array( 'tmpost_data' => isset($item['tmpost_data']) ? $item['tmpost_data'] : null, )); }, 10, 2); add_filter('wpo_update_cart_loaded_product', function ($loaded_product, $item) { return array_merge($loaded_product, array( 'tmpost_data' => isset($item['tmpost_data']) ? $item['tmpost_data'] : null, 'readonly_price' => isset($item['tm_epo_product_price_with_options']) ? $item['tm_epo_product_price_with_options'] : null, 'readonly_custom_meta_fields_html' => isset($item['tmcartepo']) ? wc_get_formatted_cart_item_data($item) : '', )); }, 10, 2); add_filter('wpo_get_item_by_product', function ($product, $cart_item_data) { return array_merge($product, array( 'tmpost_data' => isset($cart_item_data['tmpost_data']) ? $cart_item_data['tmpost_data'] : null, )); }, 10, 2); add_filter('wpo_cart_item_is_price_readonly', function ($is_readonly, $cart_item_data) { return isset($cart_item_data['tmpost_data']) && $cart_item_data['tmpost_data'] ? true : $is_readonly; }, 10, 2); add_filter('woocommerce_add_cart_item_data', function ($cart_item_data, $product_id) { if ( isset( $cart_item_data['tmpost_data'] ) && function_exists('TM_EPO') && !isset($_POST['add-to-cart']) ) { return TM_EPO()->tm_add_cart_item_data( $cart_item_data, $product_id, $cart_item_data['tmpost_data'] ); } if ( isset( $cart_item_data['tmpost_data'] ) && function_exists('THEMECOMPLETE_EPO_Cart') && !isset($_POST['add-to-cart']) ) { return THEMECOMPLETE_EPO_Cart()->tm_add_cart_item_data( $cart_item_data, $product_id, $cart_item_data['tmpost_data'] ); } return $cart_item_data; }, 70, 2); //correctly show item options add_action( "wpo_init_frontend_page", function () { add_action( 'wp_enqueue_scripts', function () { if ( defined( "THEMECOMPLETE_EPO_PLUGIN_URL" ) && defined( "THEMECOMPLETE_EPO_VERSION" ) ) { $tm_src = THEMECOMPLETE_EPO_PLUGIN_URL . '/assets/css/tm-epo.min.css'; add_filter( 'wpo_frontend_disable_load_src', function ( $disable, $src ) use ( $tm_src ) { return $disable ? $tm_src !== substr( $src, 0, strlen( $tm_src ) ) : false; }, 10, 2 ); wp_enqueue_style( 'themecomplete-epo', $tm_src, false, THEMECOMPLETE_EPO_VERSION, "all" ); } } ); } ); |
How the Code Works
| Filter / Action | Purpose |
|---|---|
wpo_update_cart_cart_item_meta | Copies the tmpost_data (the serialised extra options) from the product into the cart item meta. The plugin later uses this data to build the final order. |
wpo_update_cart_loaded_product | Adds the extra options to the loaded product object when the phone order interface displays the cart. It also calculates a read‑only price (the base product price plus the add‑on costs) and formats the add‑on summary using wc_get_formatted_cart_item_data(). |
wpo_get_item_by_product | Copies the tmpost_data into the product item when the phone order interface loads an existing order. |
wpo_cart_item_is_price_readonly | Makes the price field read‑only in the cart table for any item that has extra options. This prevents the agent from accidentally overriding a price that includes complex add‑on costs. |
woocommerce_add_cart_item_data | Calls the ThemeHigh plugin’s internal tm_add_cart_item_data method to process the extra options and calculate the final price. The priority 70ensures this filter runs after other cart‑item data filters. |
wpo_init_frontend_page | Enqueues the ThemeHigh CSS file on the phone order frontend page, ensuring the extra options display correctly. |
Why You Need This Code
Without these filters, the add‑on selections disappear when the agent adds the product to the phone order. The final order contains only the base product, and the customer does not receive the customisation they requested. The order total also fails to include the add‑on costs.
The code works with both the older TM_EPO class and the newer THEMECOMPLETE_EPO_Cart class, ensuring compatibility across different versions of the ThemeHigh plugin.
Step‑by‑Step Implementation
- Copy the entire code snippet.
- Install and activate the free Code Snippets plugin (search for “Code Snippets” in Plugins → Add New).
- Go to Snippets → Add New.
- Give the snippet a title, for example “Phone Orders – ThemeHigh Extra Options”.
- Paste the code into the Code text area.
- Set the “Run snippet” option to “Run everywhere”.
- Click Save Changes and Activate.
Testing the Integration
- Create a product that has at least one ThemeHigh Extra Product Options field (e.g., a text field for engraving, a colour swatch, or a file upload).
- Add that product to a phone order and fill in the extra options.
- Complete the order.
- Go to WooCommerce → Orders and open the order details. The extra options should appear as part of the line item.
- The price should reflect the base product price plus the add‑on cost.