1 回答

TA貢獻1906條經驗 獲得超3個贊
您沒有將所有必需的購物車商品數據保存為自定義訂單商品元數據,因此當使用“再次訂購”時,某些自定義購物車商品數據丟失,并且您的價格計算不會應用。
自 WooCommerce 3 起,woocommerce_add_order_item_meta
?hook也被棄用。
注意:使用您提供的代碼,我無法更改購物車頁面中的百分比,因此可能缺少某些內容。
您需要在代碼中刪除/替換以下函數:
// Add order item meta.
add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 3);
function add_order_item_meta($item_id, $cart_item, $cart_item_key)
{
? ? if (isset($cart_item['percentage'])) {
? ? ? ? wc_add_order_item_meta($item_id, 'percentage', $cart_item['percentage']);
? ? }
}
還有這個:
add_filter( 'woocommerce_order_again_cart_item_data', 'order_again_custom', 10, 3 );
function order_again_custom($cart_item_meta, $product, $order){
? ? //Create an array of all the missing custom field keys that needs to be added in cart item.
? ? $customfields = [
? ? ? ? ? ? ? ? ? ? 'Titulo1',?
? ? ? ? 'percentage',
? ? ? ? 'custom_price',
? ? ? ? 'price',
? ? ? ? 'new_price',
? ? ? ? ? ? ? ? ? ? ];
? ? global $woocommerce;
? ? remove_all_filters( 'woocommerce_add_to_cart_validation' );
? ? if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )
? ? foreach ( $customfields as $key ){
? ? ? ? if(!empty($product[$key])){
? ? ? ? ? ? $cart_item_meta[$key] = $product[$key];
? ? ? ? }
? ? }
? ? return $cart_item_meta;
}
由以下人員組成:
// Save custom cart item data as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'add_order_item_meta', 10, 4 );
function add_order_item_meta( $item, $cart_item_key, $values, $order ) {
? ??
? ? // Save and display the "Percentage" (optional - if needed)
? ? if (isset($values['percentage'])) {
? ? ? ? $item->update_meta_data( 'Percentage', $values['percentage'] );?
? ? }
? ? // Save All custom cart item data as a hidden data array (important)
? ? if (isset($values['percentage']) && isset($values['base_price']) && isset($values['new_price']) ) {
? ? ? ? $custom_data = array(
? ? ? ? ? ? 'percentage' => $values['percentage'],
? ? ? ? ? ? 'base_price' => $values['base_price'],
? ? ? ? ? ? 'new_price'? => $values['new_price'],
? ? ? ? );
? ? ? ? $item->update_meta_data( '_custom_data', $custom_data ); // save
? ? }
}
// Add custom order item meta as custom cart item meta
add_filter( 'woocommerce_order_again_cart_item_data', 'custom_cart_item_data_for_order_again', 10, 3 );
function custom_cart_item_data_for_order_again( $cart_item_meta, $item, $order ) {
? ? // Get the hidden order item data
? ? $custom_data = (array) $item->get_meta( '_custom_data' );
? ? if( ! empty($custom_data) ) {
? ? ? ? $cart_item_meta = array_merge( $cart_item_meta, $custom_data );
? ? }
? ? return $cart_item_meta;
}
代碼位于活動子主題(或活動主題)的 function.php 文件中。經過測試并有效。
- 1 回答
- 0 關注
- 182 瀏覽
添加回答
舉報