【问题标题】:Default deposit on a percentage of the total cart amount with exceptions以购物车总金额的一定百分比的默认存款,但有例外
【发布时间】:2017-10-18 01:14:49
【问题描述】:

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

我在这里找到了这个巧妙线程的答案:Deposit based on a percentage of total cart amount

这是我最终使用的代码:

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,我可以在前端设置描述的样式。)

但是,我不想对一些产品(一个产品类别)进行强制存款。

所以,这是我的问题:

如何调整代码以继续强制执行默认押金,但将押金排除在一个产品类别(或此类别中的产品,如果我无法排除整个类别)之外?

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    在下面的挂钩函数中,您必须设置一组产品 ID 或 (and) 产品类别,以排除它们。如果您不使用其中之一,您可以设置一个空白数组,例如 $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 上测试并有效。

    【讨论】:

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