简体   繁体   English

使用输入字段更新购物车页面上的自定义 $cart_item

[英]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']?>">

Result:结果: 在此处输入图像描述

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.

相关问题 WooCommerce:在价格函数中获取 $cart_item - WooCommerce: Get $cart_item in price function 在 Woocommerce 购物车页面上添加自定义输入字段 - Add Custom Input Field on Woocommerce Cart Page 在 WooCommerce 购物车中单击“更新购物车”更新自定义字段 - Update a custom field on "Update Cart" click in WooCommerce Cart 如何通过输入字段更改购物车中的商品价格? - How to change item price in cart by input field? 我只是想从会话中删除单个项目[cart_item] - I'm just trying to remove the single item from a session[cart_item] 保存产品自定义字段并将其显示在购物车页面中 - Saving a product custom field and displaying it in cart page 从 Woocommerce 3 中的隐藏输入字段自定义价格设置购物车项目价格 - Set cart item price from a hidden input field custom price in Woocommerce 3 根据项目自定义字段最高值在购物车和结帐页面上添加通知 - Adding a notice on Cart and checkout page based on item custom field highest value 使用产品自定义字段值覆盖 woocommerce 购物车项目价格 - Override woocommerce cart item price with product custom field value 在购物车中设置自定义字段值,并将其另存为Woocommerce 3中的订单商品数据 - Set a custom field value in cart and save it as order item data in Woocommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM