如何使用费用 ID 而不是费用名称?

问题描述

如何使用 fee ID 而不是 fee name

我对具有相同名称的条件费用有问题(这是我想要的 - 至少在购物车和结帐页面显示它时。改用   以便同名由浏览器显示,但他们实际上没有。所以... ID 可以方便地替换名称用于条件目的,因为我想显示它们的名称,它们应该是相同的。

// Add a conditional fee
add_action( 'woocommerce_cart_calculate_fees','flat_rate_based_fee',20,1 );
function flat_rate_based_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    $product_ids = array (10133,9992);
      foreach( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];

    if( in_array( 'flexible_shipping_5_1',$chosen_shipping_methods ) && in_array( $product_id,$product_ids)  ) {
        $fee = array( 'text' => __( "Return","woocommerce" ),'amount' => 0.00 );
    } elseif ( in_array( 'flexible_shipping_7_4',$product_ids) ) {
        $fee = array( 'text' => __( "Return ",'amount' => 0.00 );
    }

    if( isset($fee) ) {
        $cart->add_fee( $fee['text'],$fee['amount'],false );
    }
            }
}

add_filter('woocommerce_cart_totals_fee_html','show_positive_fee_value',10,2 );
function show_positive_fee_value($cart_totals_fee_html,$fee ) {
   if ($fee->name === 'Return' ) {
$cart_totals_fee_html ='Post Europe (Free)';
}   
return $cart_totals_fee_html;
}

add_filter('woocommerce_cart_totals_fee_html','show_positive_fee_valuk',2 );
function show_positive_fee_valuk($cart_totals_fee_html,$fee) {
if($fee->name === 'Return ') {
$cart_totals_fee_html ='Post America (Free)';
}   
return $cart_totals_fee_html;
}

代码的作用:如果带有 ID=10133 OR ID=9992 的商品在购物车中,则显示额外费用。但如果选择的第一种运输方式是美国邮政,附加费用应命名为退货,并显示美国邮政(免费) .反之亦然,如果选择欧洲邮政,则将欧洲邮政(免费)显示为名为退货的费用。

我想证明使用与发货时相同的快递员退货是免费的。

因此,“费用”有多个(两个)条件:运输方式和产品或购物车中的产品。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)