Found the problem.
The "Add" button that I had next to the input box, which I was using to trigger the wc_update_cart
like jQuery(document).trigger('wc_update_cart');
was not validating the check I had put on the php side for the value of $_POST['update_cart']
. When I realised that I changed my woocommerce_cart_item_name
filter to only add the input box and use the Update Basket
button to update the name. So my code changed to:
<?php
add_filter('woocommerce_cart_item_name','custom_naming_front',10,3);
function custom_naming_front($product_title,$cart_item,$cart_item_key){
if(isset($cart_item['custom_name']) && !empty($cart_item['custom_name'])){
$product_title=$cart_item['custom_name'];
}
$product_title .= '<div class="custom-name-form-wrapper" id="custom-name'.$cart_item_key.'">
<input type="text" name="cart['.$cart_item_key.'][custom_name]" id="custom_name_text'.$cart_item_key.'" value="" placeholder="enter the name you want to give this product" />
</div>
</div>';
return $product_title;
}
?>
and the function to show update the name looks like:
<?php
function custom_name_save_to_cart(){
if ( ( ! empty( $_POST['apply_coupon'] ) || ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( ! WC()->cart->is_empty()) {
foreach ( WC()->cart->cart_contents as $cart_item_key => $values ) {
if ( ! isset( $cart_totals[ $cart_item_key ] ) || ! isset( $cart_totals[ $cart_item_key ]['custom_name'] ) ) {
continue;
}
WC()->cart->cart_contents[ $cart_item_key ]['custom_name'] = $cart_totals[ $cart_item_key ]['custom_name'];
}
}
}
}
add_action( 'wp_loaded', 'custom_name_save_to_cart', 25);
?>