根据总购物车金额的百分比存款

Deposit based on a percentage of total cart amount

我从另一个 post 那里获取了这段代码,基本上根据我的理解,这段代码试图强制将购物车价格更改为 40 美元的固定金额,并将其作为预订费收取。

我想做的是强制购物车金额为总金额的 20%,该金额基于将购物车中的所有产品相加。我的网站用于预订,所以我只想收取押金,然后让他们在使用预订时付款。

这是来自此 post 的代码:Woocommerce cart deposit

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

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

    $bookingfee_array = array( '2434' );

    $fixed = 40.00;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {

        if( in_array( $values['product_id'], $bookingfee_array ) ) {
            $surcharge = $fixed;
            $woocommerce->cart->add_fee( 'Broneeringutasu', $surcharge, true, '' );
        }

    }

}

下面是我将如何更改它:

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

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

    $bookingfee_array = array( '2434' );

    $percent = .20;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {

        if( in_array( $values['product_id'], $bookingfee_array ) ) {
            $surcharge = $percent;
            $woocommerce->cart->add_fee( 'Booking Fee', $surcharge, true, '' );
        }

    }

}

但这并没有像预期的那样工作。

有什么帮助吗?

谢谢

您的代码并没有真正达到您的预期,因为您要向购物车总金额添加 o.2 的费用。因此,如果购物车金额为 100,总计将为 100.2...

您想要做的是移除购物车总金额的 80% 以获得 20% 的购物车金额。这可以使用购物车总金额的 80% 的负费用。如果您不需要像在原始代码中那样设置目标产品 ID,请使用下面的第二个函数。

这是第一个函数,当购物车商品与函数中设置的目标产品 ID 匹配时,将删除购物车总金额的 80%:

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 targeted products IDs
    $target_product_ids = array( 2434 );

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

    $matching = false;

    // Iterating through each cart items
    foreach ( $cart_object->get_cart() as $item_values )
    {
        if( in_array( $item_values['product_id'], $target_product_ids ) )
        { // If a cart item match with a targeted product ID

            // 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 (Including taxes)
            $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );

            break; // We stop the loop
        }
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。


But may be you don't need to have some targeted products as in the original code.

如果是这种情况,请简化代码:

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 = -.80; // 80% 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 (Including taxes)
    $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );

}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

这两个功能都经过测试并且有效