更新购物篮/购物车页面上的购物篮自定义属性 WooCommerce 产品附加组件

我一直在尝试这样做,以便我可以更新我使用WooCommerce Product Add-ons在购物篮/购物车页面上添加的自定义字段(每个产品) 。

我确实得到了一个作为测试的方法,但这确实是一个很好的解决方案,而且大部分都是测试/糟糕的代码。由于某种原因,它也只能第二次工作 - 就好像缓存破坏了它一样!它将通过文本区域进行更改,但我现在已经设置了一个值,看看是否可以让它工作。

因此,为了澄清,第一次提交时,没有任何反应,第二次它更新产品,但它现在不更新属性,它更新数量等(当我在代码中添加值时),但不更新属性。

我已将其设置为稍后删除产品,但当我删除它时,它会丢失属性,因此我暂时将重复项保留在那里,直到我修复更新页面所需的双重更新!

有没有任何人可以看到的东西可以为我指明更快修复的方向?非常感谢任何帮助!

正如你从评论中看到的,我一直在尝试几种方法,但总是失败!

<?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;

                

            }

        }

    }

}


交互式爱情
浏览 95回答 1
1回答

Cats萌萌

如果我正确理解您的问题,您希望在更新购物车时更新每个产品的插件字段。这可以通过添加挂钩woocommerce_update_cart_action_cart_updated并根据需要编辑购物车项目来实现。// hook on woocommerce_update_cart_action_cart_updatedadd_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {&nbsp; &nbsp; // loop through cart items, passing the $item array by reference, so it can be updated&nbsp; &nbsp; foreach (WC()->cart->cart_contents as $key => &$item) {&nbsp; &nbsp; &nbsp; &nbsp; // set the values of the addons array as required&nbsp; &nbsp; &nbsp; &nbsp; $item['addons'][0]['value'] = 'your message here';&nbsp; &nbsp; }});如果您只想对单个产品执行此操作,则可以在迭代它们时检查它们的其他值。
打开App,查看更多内容
随时随地看视频慕课网APP