[英]Update custom $cart_item on Cart page with input field
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:我在非商店页面上有一个表单,当提交时,它接受输入并以编程方式使用一些自定义$cart_items
在购物车中添加产品,如下所示:
WC()->cart->add_to_cart( $prodID, 1, 0, array(), array(
'bridgeName' => $_POST['bridge_name'],
'prodWidth' => $_POST['prod_width'],
) );
These $cart_item
s get added properly in backend as intended.这些$cart_item
会按预期在后端正确添加。 Now to display them, in the cart.php
template, I have used these custom $cart_items
through this code:现在要显示它们,在cart.php
模板中,我通过以下代码使用了这些自定义$cart_items
:
<?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']?>">
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.当我在这些输入字段中输入新值并单击“更新购物车”按钮时,如何做到这一点, $cart_item['prodWidth']
在后端得到更新,并用更新后的值填充这些输入字段。
I also tried this, but it is not updating the $cart_item['prodWidth']
:我也试过这个,但它没有更新$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.谢谢。
I solved it.我解决了。 It was a typo I believe and now everything works as intended.我认为这是一个错字,现在一切都按预期进行。 Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.