I have created a custom add to cart that loads a custom price and custom data. I have it doing the price fine. This is my add to cart line
WC()->cart->add_to_cart( $product_id, 1, 0, array(), $custom_data );
Where custom_data is array of goal_id and new_price
I can view the custom data when I get to the cart.
function print_cart_array() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$goal_id = $cart_item['custom_data']['goal_id'][0];
}
$cart = WC()->cart->get_cart();
echo "<pre>";
print_r($cart);
echo "</pre>";
echo "<br> <br> Custom Data: ";
echo $goal_id;
}
it shows :
Array ( [54cbe79b943d26eb7ea000e6a860e03c] => Array ( [custom_data] => Array ( [goal_id] => Array ( [0] => 238773 ) [new_price] => 20 )
plus more. The new price is being updated just fine .
The problem I am having is to add the goal_id to the woocommerce cart item for checkout.
I can get the data to view on the cart but with this code I cannot figure out how to extract the data as it seems to process before the cart is shown and array is viable. With this code goal_id comes up empty and replaced by my test number.
`add_filter( 'woocommerce_add_cart_item_data', 's4f_goal_text_to_cart_item', 10, 3 ); function s4f_goal_text_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if (isset($cart_item['custom_data']['goal_id'][0])) {
$goal_id = $cart_item['custom_data']['goal_id'][0];
} else {
$goal_id = '59855';
}
$cart_item_data['goal_id'] = $goal_id;
}
return $cart_item_data;
}`
I think I am either going about this completely wrong or I am trying to access the data incorrectly at this point.
I have viewed lots of tutorials and the only thing I am really changing is how it gets the custom data. The tutorials I have read are using input fields which I do not have I am assigning the data from another form and then just filling the cart based on that.