plugin development – Custom Product Bundles or Mix’n’Match with child items added to cart externally
Meal Service:
This site has 6 available meals (products) that change weekly. I built a custom ACF options page so the site owner can group whatever meals will be available per week. This provides an array of product ID’s for the active week which populates the front end.
Users can purchase the available meals individually or purchase a variable subscription for 10, 15, 20 meals per week in which I coded a custom usermeta feature to save their quantity preferences along with “Automate Woo” to update the subscription order items.
Everything works but the site owner has requested an additional purchase option: a single bundle with a set price for each 10, 15, and 20.
I’ve explored “Product Bundles” and “Mix’n’Match” but it seems these are not really set up to handle adding child items from my external array of ID’s. Is there not a more simplistic way to add child items to a product? Inventory is not important but recording a meal being sold is ideal.
Here’s what I’ve tried:
Product Bundles
Product bundles needs the bundle ID from their wp_woocommerce_bundled_items
DB table. There is no matching bundleID unless I add the products to the parent in the admin. I have successfully added the parent bundle but it will not add child products because the budleID doesn’t exist. Additionally, even though I’m trying to add 10, I get a cart error saying I must have 10 items (I’ve set the min/max to 10 in the parent bundle).
https://woocommerce.com/document/bundles/bundles-functions-reference/#section-7
$args = array(
'configuration' => array(
9 => array(
'product_id' => 4494,
'quantity' => 10,
'args' => array()
),
)
WC_PB()->cart->add_bundle_to_cart( 4494, 1, $args );
Mix’n’Match
Since this plugin does not have it’s own DB table, I figured this may be a little easier but there’s little documentation to help me figure this out. I dug these out of the source code but I’m a little lost getting them to work:
mnm_add_to_cart( $container_id, $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() );
I’m assuming $container_id
is the id of the MnM product? with 4071 being a meal ID. I’m not sure what to do with $cart_item_data
WC_Mix_and_Match()->cart->mnm_add_to_cart( 4493, 4071, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() );
This might be what I’m looking for but I’m not sure how $item_cart_key
works.
add_mnm_items_to_cart( $item_cart_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Chained Products
This method seems the most simplistic. The parent product contains two serialized postmeta fields _chained_product_detail
and _chained_product_ids
. However I’m not sure if any of the back-end processing will fail if this data is dynamic/per-customer and altered externally rather than stored in the DB.
Vanilla Woo
I am able to add both emtpy bundles to the cart with the following snippet but I run into the same issues as mentioned above when trying to add child items. The meals have no variations.
WC()->cart->add_to_cart(
$product_id
$quantity,
$variation_id,
$variation,
$cart_item_data
);
Is there a more simplistic way to achieve a single price for a group of products?
I might be able to achieve what I’m looking for by utilizing the bundles as they were meant to be used but all my prior work would be trashed and need to start over.
Leave an answer