Php 按购物车总金额的百分比进行默认存款,但有例外

Php 按购物车总金额的百分比进行默认存款,但有例外,php,wordpress,woocommerce,cart,fee,Php,Wordpress,Woocommerce,Cart,Fee,我在寻找一种方法,在Woocommerce网站上增加购物车总金额的押金(而不仅仅是在每个产品线项目上增加押金) 我在这里找到了这个巧妙线索的答案: 以下是我最终使用的代码: add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' ); function booking_deposit_calculation( $cart_object ) { if ( is_admin() &&

我在寻找一种方法,在Woocommerce网站上增加购物车总金额的押金(而不仅仅是在每个产品线项目上增加押金)

我在这里找到了这个巧妙线索的答案:

以下是我最终使用的代码:

add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' ); 
function booking_deposit_calculation( $cart_object ) {

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

    ## Set HERE your negative percentage (to remove an amount from cart total)
    $percent = -.50; // 50% off (negative)

    // Get cart subtotal excluding taxes
    $cart_subtotal = $cart_object->subtotal_ex_tax;
    // or for subtotal including taxes use instead:
    // $cart_subtotal = $cart_object->subtotal;

    ## ## CALCULATION ## ##
    $calculated_amount = $cart_subtotal * $percent;

    // Adding a negative fee to cart amount (excluding taxes)
    $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, false );

}
这将为购物车和结帐页面上的每个产品创建50%的默认押金。明亮的(使用CSS,我可以在前端设置描述的样式。)

但是,我有一些产品(一个产品类别),我不想强制收取押金

所以,我的问题是:


如何调整代码以继续强制执行默认存款,但将存款从一个产品类别中排除(如果无法排除整个类别,则将此类别中的产品排除)?

在下面的挂钩函数中,您必须设置一组产品ID或(和)产品类别,以排除它们。如果不使用其中一个,可以设置一个空白数组,例如
$product_categories=array()

代码如下:

add_action( 'woocommerce_cart_calculate_fees', 'custom_deposit_calculation', 10, 1 );
function custom_deposit_calculation( $cart_object ) {

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

    // Define the product IDs to exclude
    $product_ids = array( 37, 25, 50 );
    // Define the product categories to exclude (can be IDs, slugs or names)
    $product_categories = array( 'clothing' );
    $amount_to_exclude_with_tax = 0;

    // Iterating through cart items
    foreach ( $cart_object->get_cart() as $cart_item ){
        // If condition match we get the sum of the line item total (excl. tax) 
        if( in_array( $cart_item['product_id'], $product_ids ) || has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) )
            $amount_to_exclude_with_tax += $cart_item['line_total'];
            // OR replace by (for tax inclusion)
            // $amount_to_exclude_with_tax += $cart_item['line_tax'] + $cart_item['line_total'];
    }

    ## Set HERE your negative percentage (to remove an amount from cart total)
    $percent = -0.5; // 50% off (negative)

    // Get cart subtotal excluding taxes
    $cart_subtotal = $cart_object->subtotal_ex_tax - $amount_to_exclude_with_tax;
    // or for subtotal including taxes use instead:
    // $cart_subtotal = $cart_object->subtotal;

    ## ## CALCULATION ## ##
    $calculated_amount = $cart_subtotal * $percent;

    if( $calculated_amount != 0){
        // Adding a negative fee to cart amount (Including taxes)
        $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
    }
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

在WooCommerce 3和works上测试