In my scenario, a subscription order is added to the cart of the current user with additional data used to calculate additional data about the subscription such as the next renewal dates, subscription price, and so on. This is working nicely through the use of:
$woocommerce->cart->add_to_cart($product->get_id(), '1', '0', array(), array(
PVS_META_DATA_KEY_INPUTS => $test_data['result']['inputs'],
PVS_META_DATA_KEY_MONTHS => $test_data['result']['month-data'],
PVS_META_DATA_KEY_PRODUCTS => $test_data['result']['product-data']
));
With that out the way, the last step for me is grabbing the month and product metadata from the cart's custom data and store it with the subscription for order fulfillment reasons. My research revealed that I probably need to use the woocommerce_checkout_create_order_line_item
action. The following is my code:
function pvs_woocommerce_checkout_create_order_line_item($item, $cart_item_key, $values, $order)
{
var_dump($item);
echo '<br />';
var_dump($cart_item_key);
echo '<br />';
var_dump($values);
echo '<br />';
var_dump($order);
if (isset($values[PVS_META_DATA_KEY_INPUTS]))
{
$item->add_meta_data(PVS_META_DATA_KEY_INPUTS, json_encode($values[PVS_META_DATA_KEY_INPUTS]));
}
if (isset($values[PVS_META_DATA_KEY_MONTHS]))
{
$item->add_meta_data(PVS_META_DATA_KEY_MONTHS, json_encode($values[PVS_META_DATA_KEY_MONTHS]));
}
if (isset($values[PVS_META_DATA_KEY_PRODUCTS]))
{
$item->add_meta_data(PVS_META_DATA_KEY_PRODUCTS, json_encode($values[PVS_META_DATA_KEY_PRODUCTS]));
}
}
add_action('woocommerce_checkout_create_order_line_item', 'pvs_woocommerce_checkout_create_order_line_item', 10, 4);
However, upon checkout, I am never getting output from this action. Checking the WooCommerce status page, there are no errors being returned - so if it is failing it is silently failing. As a sanity check, I also used the following function/action just to see if it was being called and the other one as failing:
function idk_im_losing_my_mind_tho($item, $cart_item_key, $values, $order)
{
echo 'Please work ;(';
}
add_action('woocommerce_checkout_create_order_line_item', 'idk_im_losing_my_mind_tho', 10, 4);
Any words of advice here on either a) getting this action to start firing or b) getting the cart data stored in the order after checkout?
We are using WordPress version 5.3.2, WooCommerce version 3.9.1, and WooCommerce Subscription version 3.0.1. Any help is greatly appreciated!