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

Update basket custom attribute on basket/cart page WooCommerce Product Add-ons

我一直在努力做到这一点,以便我可以使用 WooCommerce 产品附加组件[=35= 更新我在 basket/cart 页面上添加的自定义字段(每个产品) ].

我确实得到了一个方法作为测试,但它确实是一个很好的解决方案,其中大部分是 test/bad 代码。由于某种原因,它也只在第二次工作——就好像缓存正在破坏它一样! 它将通过文本区域进行更改,但我现在设置了一个值以查看是否可以正常工作。

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

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

有没有任何人可以看到任何可以指出我更快修复方向的内容?非常感谢任何帮助!!!

正如您从评论中看到的那样,我一直在尝试一些方法,但一直失败!!

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

编辑: 下面是更新每个项目的消息框,这很好并且正是需要的,只是试图让会话接受更新。购物车是一个多页购物车,因为这是请求的内容,因此当您转到下一个 page/checkout 页面时,您可以看到它已恢复为原始消息,您可以在购物车会话中看到它。此外,如果您只是刷新页面,它会恢复原状(在您已经更新页面之后)。

我尝试通过 WC()->session->set 进行编辑,但我想我设置错位了!我得到了一些会话集,但没有设置正确的条目!

add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $test = $_POST['cart'][$key]['addons'];
        $item['addons'][0]['value'] = $test;
        print_r(WC()->session->get( 'cart'));
    }
});

编辑 - 工作代码: 感谢@benJ 提供的解决方案,非常有帮助!需要设置会话,但不是我认为必须设置的方式!

// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $addon = $_POST['cart'][$key]['addons'];
        $item['addons'][0]['value'] = $addon;
        // set the session
        WC()->cart->set_session();
    }
});

干杯 伊斯坎德尔

如果我对你的问题的理解正确,你想在更新购物车时更新每个产品的插件字段。

这可以通过在 woocommerce_update_cart_action_cart_updated 上添加挂钩并根据需要编辑购物车项目来实现。

// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {

    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $item['addons'][0]['value'] = 'your message here';
    }
});

如果您只想对单个产品执行此操作,则可以在迭代它们时检查它们的其他值。