根据购物车总金额的百分比进行存款 [英] Deposit based on a percentage of total cart amount

查看:105
本文介绍了根据购物车总金额的百分比进行存款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个帖子中获取了此代码,基本上我的理解是,该代码试图将购物车价格更改为固定的$ 40,并将其作为预订费用收取。

I've taken this code from another post and basically from my understanding, this code is trying to force the cart price to change to a fixed amount of $40 and charge it as a booking fee.

我要执行的操作是将购物车中的所有产品加总后,将购物车金额强制为总额的20%。我的网站用于预订,所以我只希望收取押金,然后让他们在使用预订时付款。

What I want to do is force the cart amount to be 20% of what the total would be based on adding up all the products in the cart. My site is for reservations, so I only want to charge a deposit and then have them pay when they use their reservation.

这是该帖子中的代码: Woocommerce购物车入金

Here is the code from this 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, '' );
        }

    }

}

这就是我要如何更改它:

And here is how I would change it:

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

Your code is not really going to do what you expect, as you are adding a fee of o.2 to total cart amount. So if the cart amount is 100, the grand total is going to be 100.2

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

What you want to do instead, is to remove 80% of total cart amount to get a cart amount of 20%. This is possible using a negative fee of 80% from total cart amount. If you don't need to set targeted products IDs as in your original code, use the 2nd function below.

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

Here is the first function that will remove 80% of total cart amount when a cart item match with the targeted product IDs set in the function:

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文件或任何插件文件中。

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


但是也许您不需要像原始代码中那样有针对性的产品。

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

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

If it's the case this simplify the 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文件活动的子主题(或主题),也可以在任何插件文件中使用。

两个功能都经过测试并且可以工作

Both functions are tested and works

这篇关于根据购物车总金额的百分比进行存款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆