I have been trying to make it so that I can update a custom field(per product) I have added on the basket/cart page using WooCommerce Product Add-ons.
I did get a method working as a test, but it is really a good solution, and most of it is a test/bad code. It also only works the second time for some reason - as if the cache is breaking it! It will be changed via a textarea, but I have set a value for now to see if I can get it to work.
So, to clarify, on submit first time, nothing happens, secound time it updates the product, however it now doesn't update the attribute, it updates quantity etc(when I add a value in the code) but not the attribute.
I have set it to delete the product afterwards but it looses the attribute when I delete it so I have left the duplication in there for now until I fix the double update required for it to update the page!
Is there anything anyone can see that will point me in the direction of a quicker fix? Greatly apprecaite any help!!!
As you can see from the commenting out I have been trying a few ways, but keep failing!!
<?php
if ( ! empty( $_POST['update_cart'] ) ) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['addons'] ) ) {
WC()->cart->remove_cart_item($cart_item_key);
#print_r($woocommerce->cart->cart_contents[ $cart_item_key ]['addons']);
#$cart_item['addons'] = array('name' => 'Add your message', 'value' => 'testing the message box', 'price' => 0, 'field_name' => $cart_item['product_id'].'-add-your-message-1', 'field_type' => 'custom_textarea', 'price_type' => 'quantity_based' );
$data = array('name' => 'Add your message', 'value' => 'testing the message box', 'price' => 0, 'field_name' => $cart_item['product_id'].'-add-your-message-1', 'field_type' => 'custom_textarea', 'price_type' => 'quantity_based' );
#WC()->cart->add_to_cart($cart_item['product_id'], $cart_item['quantity'], $cart_item['variation_id'], $cart_item['addons']);
WC()->cart->add_to_cart($cart_item['product_id'], $cart_item['quantity'], $cart_item['variation_id'], $cart_item['addons']);
#$woocommerce->cart->cart_contents[$cart_item_key]['addons'][0]['value'] = 'test';
$woocommerce->cart->cart_contents[$cart_item]['addons'] = $data;
}
}
}
}
EDIT: The below is updating the message box per item, which is great and exactly what was needed, just trying to get the session to accept the update. The cart is a multipage cart as that is what was requested, so when you go to the next page/checkout page you can see it has reverted back to the original message and you can see it in the cart session. Also, if you just refresh the page it reverts back(after you have already updated the page).
I have tried to edit via WC()->session->set but I think I am setting the wrong bit! I get some of the session set, but it doesn't set the correct entry!
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
// loop through cart items, passing the $item array by reference, so it can be updated
foreach (WC()->cart->cart_contents as $key => &$item) {
// set the values of the addons array as required
$test = $_POST['cart'][$key]['addons'];
$item['addons'][0]['value'] = $test;
print_r(WC()->session->get( 'cart'));
}
});
EDIT - WORKING CODE: Thank you to @benJ for the solution, very helpful! The session needed to be set, but not the way I thought I had to do it!
// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
// loop through cart items, passing the $item array by reference, so it can be updated
foreach (WC()->cart->cart_contents as $key => &$item) {
// set the values of the addons array as required
$addon = $_POST['cart'][$key]['addons'];
$item['addons'][0]['value'] = $addon;
// set the session
WC()->cart->set_session();
}
});
Cheers Iskander
If I understand your question correctly, you want to update an addon field on every product when you update the cart.
This can be achieved by adding a hook on woocommerce_update_cart_action_cart_updated
and editing the cart items as required.
// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
// loop through cart items, passing the $item array by reference, so it can be updated
foreach (WC()->cart->cart_contents as $key => &$item) {
// set the values of the addons array as required
$item['addons'][0]['value'] = 'your message here';
}
});
If you're only looking to do this for individual products, you can check their other values as you iterate over them.