【问题标题】:Deposit based on a percentage of total cart amount根据购物车总金额的百分比存款
【发布时间】:2017-03-06 01:55:50
【问题描述】:

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

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

这是这篇文章的代码: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, '' );
        }

    }

}

但这并没有按预期工作。

有什么帮助吗?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart woocommerce-bookings


    【解决方案1】:

    您的代码实际上并没有达到您的预期,因为您将 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 文件或任何插件文件中。


    但可能您不需要像原始代码中那样有一些目标产品。

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

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多