WPC Product Bundles for WooCommerce
Phone Orders correctly handles WPC Product Bundles by skipping child items, marking them as child cart items, and keeping the bundle price read‑only. This article provides a complete code snippet that ensures bundled products behave correctly when an agent adds them to a manual order.
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 |
add_filter( 'wpo_skip_add_to_cart_item', function ( $skip, $item ) { return isset($item['woosb_parent_id']) ? true : $skip; }, 10, 2 ); add_filter( 'wpo_is_child_cart_item', function ( $child, $item ) { return isset($item['woosb_parent_id']) ? true : $child; }, 10, 2 ); add_filter( 'wpo_search_product_types', function ( $types ) { $types[] = "woosb"; return $types; } ); add_filter('wpo_add_configured_products_skip_item', function ($skip, $item_data) { return isset($item_data['woosb_parent_key']) ? true : $skip; }, 10, 2); add_filter('wpo_children_cart_item', function ($children, $item) { return isset($item['woosb_keys']) ? $item['woosb_keys'] : $children; }, 10, 2); add_filter('wpo_cart_item_is_price_readonly', function ($is_readonly, $item) { return isset($item['woosb_keys']) ? true : $is_readonly; }, 10, 2); add_filter('wpo_load_order_skip_item', function ($skip, $order_item, $order) { return $order_item->get_meta('_woosb_parent_id') ? true : $skip; }, 10, 3); |
How the Code Works
| Filter | Purpose |
|---|---|
wpo_skip_add_to_cart_item | Returns true for any item that has a woosb_parent_id key (i.e., a child product of a bundle). The plugin skips adding child items directly – only the parent bundle is added. |
wpo_is_child_cart_item | Marks any item with woosb_parent_id as a child cart item. The plugin later groups child items under the parent bundle. |
wpo_search_product_types | Adds the product type woosb to the product search, so agents can find bundle products in the phone order interface. |
wpo_add_configured_products_skip_item | Skips adding child items when the plugin auto‑configures bundled products. |
wpo_children_cart_item | Returns the array of woosb_keys (child item IDs) for a parent bundle item. The plugin uses this to group the child items under the parent. |
wpo_cart_item_is_price_readonly | Makes the price read‑only for any cart item that has woosb_keys (i.e., the parent bundle). |
wpo_load_order_skip_item | Skips loading child items when the plugin loads an existing order from the database. |
The bundle item itself is the only item added directly; the plugin automatically pulls in the child products from the WPC configuration.
Why You Need This Code
Without these filters, adding a WPC Product Bundle to a phone order produces incorrect results. Child items may appear twice, the total price may be wrong, or the bundle may appear as a single product without its components. The code ensures the bundle displays correctly with its child items listed underneath it, and the price field remains read‑only to preserve the bundle’s calculated total.
Step‑by‑Step Implementation
- Copy the entire code snippet.
- Install and activate the free Code Snippets plugin.
- Go to Snippets → Add New.
- Give the snippet a title, for example “Phone Orders – WPC Product Bundles”.
- 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 bundle product using the WPC Product Bundles plugin (at least two child products).
- Add the bundle to a phone order.
- Verify that the bundle appears in the cart with all child items listed underneath it.
- Complete the order.
- Check the order details in WooCommerce → Orders. The bundle should display correctly with its child items, and the total price should match the bundle’s configuration.