我一直在尝试这样做,以便我可以更新我使用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;
}
}
}
}
Cats萌萌