我一直在嘗試這樣做,以便我可以更新我使用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';
}
});
如果您只想對單個產品執行此操作,則可以在迭代它們時檢查它們的其他值。
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消