0

I have a form on a non-shop page which when submitted, takes the inputs and adds a product in the cart programmatically with some custom $cart_items like so:

WC()->cart->add_to_cart( $prodID, 1, 0, array(), array(
  'bridgeName'       => $_POST['bridge_name'],
  'prodWidth'        => $_POST['prod_width'],
) );  

These $cart_items get added properly in backend as intended. Now to display them, in the cart.php template, I have used these custom $cart_items through this code:

<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<p style="margin-bottom: 0;">%s</p>', esc_html( $cart_item['bridgeName']) ), $cart_item, $cart_item_key ) ); ?>
......
......
<label for="prodWidth">Structure Width</label>
<input type="number" style="width: 10rem;" name="cart[<?php echo $cart_item_key; ?>][prodWidth]" id="strHeight_cart" value="<?php echo $cart_item['prodWidth']?>">

Result: enter image description here

How do I make it so when I enter a new value in these input fields and click on Update Cart button, the $cart_item['prodWidth'] gets updated in the backend and also populates these input fields with the updated value.

I also tried this, but it is not updating the $cart_item['prodWidth']:

add_filter( 'woocommerce_update_cart_action_cart_updated', 'my_cart_custom_fields' );
function my_cart_custom_fields( $cart_updated ){

    $contents = WC()->cart->cart_contents;
    foreach ( $contents as $key => $item ) {
        $contents[$key]['prodWidth'] = $_POST['cart['.$key.'][prodWidth];
    }

    return $cart_updated;
}

Thanks.

Mr.Coder
  • 347
  • 1
  • 10

1 Answers1

0

I solved it. It was a typo I believe and now everything works as intended. Thanks.

Mr.Coder
  • 347
  • 1
  • 10