为每个产品而不是每个购物车添加自定义费用项目

时间:2019-11-11 14:21:54

标签: php woocommerce

我正在使用以下代码在管理面板中向woocommerce订单添加自定义默认项目:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
     global $woocommerce;

     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
          return;

     $fee = 0.00;
     $woocommerce->cart->add_fee( 'Shipping', $fee, true, 'standard' );
}

现在我要添加:

     $woocommerce->cart->add_fee( 'Packing', $fee, true, 'standard' );
     $woocommerce->cart->add_fee( 'Customs', $fee, true, 'standard' );
     $woocommerce->cart->add_fee( 'Insurance', $fee, true, 'standard' );
     $woocommerce->cart->add_fee( 'Tax', $fee, true, 'standard' );

订购的每种产品。

如何为每种产品显示这些物品?

更新

foreach ( $cart_object->cart_contents as $key => $value ) {
...
}

我认为我必须对此代码做些事情...

1 个答案:

答案 0 :(得分:2)

尝试在主题function.php中使用此代码:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
    function endo_handling_fee($cart_object) {
         global $woocommerce;

         if ( is_admin() && ! defined( 'DOING_AJAX' ) )
              return;

         $fee = 0.00;

        $woocommerce->cart->add_fee( 'Shipping', $fee, true, 'standard' );
        foreach ( $cart_object->get_cart() as $key => $value ) {
            $_product =  wc_get_product( $value['data']->get_id()); 
             $woocommerce->cart->add_fee( $_product->get_title().'~~Packing', $fee, true, 'standard' );
             $woocommerce->cart->add_fee( $_product->get_title().'~~Customs', $fee, true, 'standard' );
             $woocommerce->cart->add_fee( $_product->get_title().'~~Insurance', $fee, true, 'standard' );
             $woocommerce->cart->add_fee( $_product->get_title().'~~Tax', $fee, true, 'standard' );
        }
    }

    /**
 * Remove password strength check.
 */
function iconic_remove_password_strength() {
    wp_dequeue_script( 'wc-password-strength-meter' );
}
add_action( 'wp_print_scripts', 'iconic_remove_password_strength', 10 );
相关问题