为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
交互式爱情 2023-07-08 21:45:08
我一直在尝试这样做,以便我可以更新我使用WooCommerce Product Add-ons在购物篮/购物车页面上添加的自定义字段(每个产品) 。我确实得到了一个作为测试的方法,但这确实是一个很好的解决方案,而且大部分都是测试/糟糕的代码。由于某种原因,它也只能第二次工作 - 就好像缓存破坏了它一样!它将通过文本区域进行更改,但我现在已经设置了一个值,看看是否可以让它工作。因此,为了澄清,第一次提交时,没有任何反应,第二次它更新产品,但它现在不更新属性,它更新数量等(当我在代码中添加值时),但不更新属性。我已将其设置为稍后删除产品,但当我删除它时,它会丢失属性,因此我暂时将重复项保留在那里,直到我修复更新页面所需的双重更新!有没有任何人可以看到的东西可以为我指明更快修复的方向?非常感谢任何帮助!正如你从评论中看到的,我一直在尝试几种方法,但总是失败!<?phpif ( ! 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;                            }        }    }}
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

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


这可以通过添加挂钩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';

    }

});

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


查看完整回答
反对 回复 2023-07-08
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信