基于 WooCommerce 中选择的付款方式的运费折扣

问题描述

如果客户选择某种付款方式,我目前正在尝试应用运费折扣。

出于某种原因,无论选择哪种付款方式,都会应用折扣。

我在 functions.PHP 中使用的代码是:

function filter_woocommerce_package_rates( $rates,$package ) {
    
    $min = 25;
    $min2 = 25;
    $max = 50;
    $discount_percent = 50;
    $payment_method = 'clearpay';
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    // Get cart total
    $cart_total = WC()->cart->cart_contents_total;

    // Condition
    if ( $cart_total >= $min && $cart_total <= $max && $payment_method == $chosen_payment_method ) {
        // (Multiple)
        foreach ( $rates as $rate_key => $rate ) {
            // Get rate cost            
            $cost = $rates[$rate_key]->cost;
            
            // Set rate cost
            $rates[$rate_key]->cost = $cost - ( ( $cost * $discount_percent ) / 100 );
        }
        
        wc_add_notice( 
            sprintf( 'Congratulations! Your shipping is Now 50&#37; off!',wc_price( WC()->cart->total ),wc_price( $minimum )
            ),'success' 
        );
        
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates',10,2 );
do_action( 'woocommerce_set_cart_cookies',true );

知道出了什么问题吗?

解决方法

这就是我为了获得不同付款方式的固定折扣金额而做的事情

您可以根据自己的情况进行调整

add_action( 'woocommerce_cart_calculate_fees','mlnc_add_discount',20,1 );

function mlnc_add_discount( $cart_object ) {

$label= __('');
$discount = 0;

$chosen_payment_method = WC()->session->get('chosen_payment_method'); //get the selected payment method

switch($chosen_payment_method){
case 'paypal':    
$label = __( "PayPal Discount" );
// The discount amount to apply
$discount = 5;
break;
case 'bacs':    
$label = __( "Direct Bank Transfer Discount" );
// The discount amount to apply
$discount = 10;
break; 
case 'cod':    
$label = __( "Cash on Delivery Discount" );
// The discount amount to apply
$discount = 0;
break; 
}

// Add the discount
$cart_object->add_fee( $label,- $discount,false );
}
,

您的代码包含您没有使用或使用不当的不必要变量。你的问题描述中肯定没有提到。

因此要根据您可以使用的所选付款方式应用运费折扣。

import pyodbc 

conn = pyodbc.connect(r'Driver={SQL Server};'
                      r'Server=tcp:MY-SRV-NAME\ABC,49133;'                       
                      r'Database=MyDatabase;'
                      r'Trusted_Connection=yes;')

cursor = conn.cursor()
cursor.execute('SELECT coalesce(max(NextDate),?) FROM [dbo].[TableName]',b)

注意:不要忘记清空购物车,以刷新 WooCommerce 运输缓存