Skip to content
Advertisement

My cart_item_data isn’t showing up in the order

I’m working on a site that lets a user enter data from a form and have it attached to a product. I was originally using:

$cart_item_data ['Entry Link'] = $formUrl;
$woocommerce->cart->add_to_cart($productID,$quantity,0,$cart_item_data);

With $formUrl being a link to the form data. Whenever a user made an order, under the product would be ‘Entry Link’ with the url.

enter image description here

We later had to add variations to the product so the line looked like:

$woocommerce->cart->add_to_cart($productID,$quantity,$typeID,$cart_item_data);

With $typeID being the variation ID. Once I added the $typeID, only the variation showed up on the order in the backend and not the ‘Entry Link’.

enter image description here

If I reset $typeID to ‘0’, the entry link shows up when an order is made. I also tried using the variation id in place of the product id but was still getting the same issue. I looked at the documentation and it should be working.

I need both the variation and the ‘Entry Link’ to be entered and visible from the backend.


This was the part requested by Vincenzo Di Gaetano in the comments

$formUrl = $_SERVER['SERVER_NAME'].'/wp-admin/admin.php?page=gf_entries&view=entry&id='.$formID.'&lid='.$entryID;
$cart_item_data['Entry Link'] = $formUrl;
$variationAttributes['Per'] = 1000;
$woocommerce->cart->add_to_cart($productID,$quantity,$typeID,$variationAttributes,$cart_item_data);

I added variationAttributes to it with ‘Per’ lining up with the attribute name, ‘1000’ represents the value of that attribute. I hardcoded it in for testing. When I echo just the $formUrl, it does return the correct url

Advertisement

Answer

You are setting only 4 arguments instead of 5 for the add_to_cart method of the WC_Cart class.

Try replacing this:

$woocommerce->cart->add_to_cart( $productID, $quantity, $typeID, $cart_item_data );

with:

$woocommerce->cart->add_to_cart( $productID, $quantity, $typeID, $variation, $cart_item_data );

Here is an example of how to add a variation to the cart:

This will not be enough to display the cart item data as the meta of the order item.

Based on this question Save and display product custom meta on WooCommerce orders and emails you will need to save the value as a order item meta data:

// save the cart item data as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_data_as_order_item_meta_data', 20, 4 );
function save_cart_item_data_as_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['Entry Link'] ) ) {
        $item->update_meta_data( __( 'Entry Link'), $values['Entry Link'] );
    }
}

The code has been tested and works. Add it to your active theme’s functions.php.

Here is the result:

enter image description here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement