1

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!

Mythikos
  • 119
  • 12
  • Does this answer your question? [Add extra meta for orders in Woocommerce](https://stackoverflow.com/questions/25626058/add-extra-meta-for-orders-in-woocommerce) – caiovisk Feb 13 '20 at 00:23
  • @caiovisk Top answer suggests using the ```woocommerce_checkout_update_order_meta``` action. That might work but the problem is the cart is currently holding all of my custom data. Can you suggest a way to get the data from the cart to this action? – Mythikos Feb 13 '20 at 00:46

1 Answers1

0

Seeing I couldn't tell if the woocommerce_checkout_create_order_line_item action was firing properly, I decided to try another approach to debugging. I found that WooCommerce exposes a logger so I created a method that would allow me to write to the WooCommerce logs. Something like:

function pvs_write_log($message)
{
    $logger = wc_get_logger();
    $logger->debug($message, array('source' => 'PVS Plugin'));
}

Then from the pvs_woocommerce_checkout_create_order_line_item function, I used pvs_write_log and var_export instead of echo and var_dump. Lo and behold, I had an output to the logs which allowed me to debug and figure out the rest of my woes. My suspicion, without digging into the WooCommerce plugin, is that this action must happen right before a page redirect causing the page output to not be visible.

Mythikos
  • 119
  • 12