簡體   English   中英

如何為 WooCommerce 購物車中的不同產品添加額外費用

[英]How to add additional fees to different products in WooCommerce cart

我使用此代碼向特定產品 ID 添加額外費用。 問題是我只能向不同的產品 ID 添加一項費用。

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids'); 
function add_fees_on_ids() { 
   if (is_admin() && !defined

('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) { 
     if( in_array( $item['product_id'], 

fee_ids() )) { 
     WC()->cart->add_fee(__('ADDITIONAL FEE:'), 5); 
     } 
   } 
} 
function fee_ids() { 
   return array( 2179 ); 
}

我需要為不同的產品添加不同的費用 - 例如:

  • 產品 ID 為 1234 的產品 1 將收取“xx”額外費用。
  • 產品 ID 為 5678 的產品 2 將收取“xx”額外費用。

使用此代碼,我只能為不同的產品設置一項費用。 如何在 WooCommerce 中為不同的產品添加不同的費用?

您可以使用 WordPress 插件“高級自定義字段”:

  1. 安裝插件
  2. 設置一個標題為“Product with fee”的字段組,並將其位置規則設置為“Post Type” => “is equal to” => “Product”
  3. “字段類型”“文本”的“+添加字段”並注意其“名稱”(不是“標簽”)以供以后檢查,例如“費用”
  4. 在相應的 WooCommerce 產品帖子的費用元字段中設置費用值
  5. 在您的'woocommerce_cart_calculate_fees'掛鈎回調循環中,購物車產品並檢查具有名為“fee”的元字段的設置值的產品,並在該條件下添加您的費用添加代碼:
if (get_field( "fee" )) {        // get_field() returns false if empty aka not set from admin area
  // fee adding code for single product in cart
};

你好,謝謝大家! 我找到了解決問題的方法。 我只是多次復制下面描述的代碼,因為我對不同的產品有不同的數量並進行更改:

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids'); 
function add_fees_on_ids() { 
   if (is_admin() && !defined

('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) { 
     if( in_array( $item['product_id'], 

fee_ids() )) { 
     WC()->cart->add_fee(__('ADDITIONAL FEE:'), 5); 
     } 
   } 
} 
function fee_ids() { 
   return array( 2179 ); 
}

我改變add_fees_on_idsadd_fees_on_ids_1fee_idsfee_ids_1ADDITIONAL FEE:ADDITIONAL FEE 1:

我知道對於某些人來說,它可能看起來不專業,但它可以解決我的問題。

有幾種方法。 例如,您確實可以向管理產品設置添加自定義字段

但這並不一定很復雜,為此安裝一個額外的插件太牽強了!

多次添加相同的代碼也是一個壞主意,因為這樣您將多次瀏覽購物車。 這不僅會減慢您的網站速度,還會最終導致錯誤。

添加一個數組,您不僅可以確定產品 ID,還可以根據數組鍵立即確定額外費用,這在我看來是最簡單有效的方法。

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Add in the following way: Additional fee => Product ID
    $settings = array(
        10  => 1234,
        20  => 5678,
        5   => 30,
        20  => 813,
        2   => 815,
    );
    
    // Initialize
    $additional_fee = 0;
    
    // Loop through cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];
        
        // In array, get the key as well
        if ( false !== $key = array_search( $product_id, $settings ) ) {
            $additional_fee += $key;
        }
    }

    // If greater than 0, so a matching product ID was found in the cart
    if ( $additional_fee > 0 ) {
        // Add additional fee (total)
        $cart->add_fee( __( 'Additional fee', 'woocommerce' ), $additional_fee, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM