【问题标题】:How to add additional fees to different products in WooCommerce cart如何在 WooCommerce 购物车中为不同产品添加额外费用
【发布时间】:2021-11-06 11:10:09
【问题描述】:

我使用此代码向特定产品 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 中为不同的产品添加不同的费用?

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    有几种方法。例如,您确实可以add a custom field to the admin product settings

    但不一定要复杂,为此安装一个额外的插件太牵强了!

    多次添加相同的代码也是一个坏主意,因为这样您将多次浏览购物车。这不仅会减慢您的网站速度,而且最终还会导致错误。

    在我看来,添加一个数组,不仅可以确定产品 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,
            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 );
    

    可选:

    要单独显示每笔费用的加法,让客户知道哪个费用是指哪个产品,可以使用多维数组。

    将必要的信息添加到设置数组中,其他一切都会自动发生。

    所以你得到:

    function action_woocommerce_cart_calculate_fees( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Settings
        $settings = array(
            array(
                'product_id' => 30,
                'amount'     => 5,
                'name'       => __( 'Additional service fee', 'woocommerce' ),
            ),
            array(
                'product_id' => 813,
                'amount'     => 10,
                'name'       => __( 'Packing fee', 'woocommerce' ),
            ),
            array(
                'product_id' => 815,
                'amount'     => 15,
                'name'       => __( 'Another fee', 'woocommerce' ),
            ),
        );
        
        // Loop through cart contents
        foreach ( $cart->get_cart_contents() as $cart_item ) {      
            // Get product id
            $product_id = $cart_item['product_id'];
            
            // Loop trough settings array
            foreach ( $settings as $setting ) {
                // Search for the product ID
                if ( $setting['product_id'] == $product_id ) {
                    // Add fee
                    $cart->add_fee( $setting['name'], $setting['amount'], false );
                }
            }       
        }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
    

    相关:How to sum the additional fees of added product ID's in WooCommerce cart

    【讨论】:

    • 您好 7uc1f3r,再次感谢您。我的意思是......在这种情况下,我不能以固定价格添加多个产品 ID。例如,如何添加 5 多于 1 个产品 ID -“'product_id' => 30,40,34,44,22...”
    • @Yordan 抱歉,但您的原始问题在此期间已经被回答了好几次。我们真的不打算为每个给出的答案提出一个新问题。 Stack Overflow 不是代码编写服务。如果您想要添加,请随时尝试自己添加它们。如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。
    【解决方案2】:

    大家好,谢谢大家! 我找到了解决问题的方法。我只是复制下面描述的代码,因为我对不同的产品有不同的数量并更改它:

    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_ids 更改为add_fees_on_ids_1fee_ids 更改为fee_ids_1ADDITIONAL FEE:ADDITIONAL FEE 1:

    我知道对于某些人来说它可能看起来不专业,但它可以解决我的问题。

    【讨论】:

      【解决方案3】:

      我。您可以为此使用 WordPress 插件 "Advanced Custom Fields

      1. 安装插件
      2. 设置一个字段组,标题为“产品收费”,并将其位置规则设置为“帖子类型”=>“等于”=>“产品”
      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
      };
      

      二。或者您可以通过添加和显示 WooCommerce 自定义产品元字段来实现这一点,如下所述:

      https://www.cloudways.com/blog/add-custom-product-fields-woocommerce/

      【讨论】:

      • 你好,bitski,没有插件可以解决我的问题吗?我已经安装了一个插件并且它可以工作,但我需要在没有插件的情况下实现这个功能。再次感谢你。亲切的问候
      • 有可能。您必须添加一个自定义元字段。这是一个例子cloudways.com/blog/add-custom-product-fields-woocommerce
      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2023-04-10
      • 2015-05-03
      相关资源
      最近更新 更多