php 更改WooCommerce购物车项目价格基于数量折扣的类别

tyu7yeag  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(120)

我需要某些项目有一个混合和匹配的数量折扣的基础上类别。我已经看到了一些其他的代码,实现这一点使用整体折扣在底部的车,但是我需要每个项目的价格,以反映折扣,我也需要这个价格结转到销售订单,所以我们可以适当的发票.以下是我目前所掌握的:

add_action( 'woocommerce_cart_calculate_fees','custom_bulk_discount', 20, 1 );
function custom_bulk_discount( $cart ) {

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

    $category = 'ferns';

    $calculated_qty = 0;
    $calculated_total = 0;

    foreach($cart->get_cart() as $cart_item):

        if(has_term($category, 'product_cat', $cart_item['product_id'])):
            $item_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price(); // The price for one (assuming that there is always 2.99)
            $item_qty = $cart_item["quantity"];// Quantity
            $item_line_total = $cart_item["line_total"]; // Item total price (price x quantity)
            $calculated_qty += $item_qty; // ctotal number of items in cart
            $calculated_total += $item_line_total; // calculated total items amount
        endif;
    endforeach;

    if ( $calculated_qty >= 100 && $calculated_qty < 200 ){
        foreach($cart->get_cart() as $cart_item):

            $price = $cart_item['data']->price - 1;
            if(has_term($category, 'product_cat', $cart_item['product_id'])):
                $cart_item['data']->set_price( $price );
            endif;
        endforeach;
    }else if( $calculated_qty >= 200){
        foreach($cart->get_cart() as $cart_item):

            $price = $cart_item['data']->price - 1.5;
            if(has_term($category, 'product_cat', $cart_item['product_id'])):
                $cart_item['data']->set_price( $price );
            endif;
        endforeach;
    }
}

问题是小计没有更新正确的价格。我假设这是因为我只改变了显示价格,而不是实际计算的价格。有办法做到我要求的吗?

8gsdolmq

8gsdolmq1#

首先,你没有使用正确的钩子,你的代码可以被简化,并且有遗漏的东西。
要根据特定产品类别的数量折扣购物车项目尝试:

// Utility function: Get specific category discount based on quantity
function get_category_custom_discount( $cart, $category ) {
    $calculated_qty = 0; // Initializing

    foreach( $cart->get_cart() as $cart_item ) {
        if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
            $calculated_qty   += $cart_item["quantity"]; 
        }
    }

    if ( $calculated_qty >= 200 ) {
        return 1.5;
    } else if( $calculated_qty >= 100 ) {
        return 1;
    } else {
        return 0;
    }
}

// Change cart item price
add_action( 'woocommerce_before_calculate_totals', 'category_ferns_set_bulk_discount', 20, 1 );
function category_ferns_set_bulk_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action('woocommerce_before_calculate_totals') > 1 )
        return;

    $category = 'ferns';
    $discount = get_category_custom_discount( $cart, $category );

    if ( $discount > 0 ) {
        foreach($cart->get_cart() as $cart_item) {
            if( has_term($category, 'product_cat', $cart_item['product_id']) ) {
                $product = wc_get_product( $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'] );
                $cart_item['data']->set_price( $product->get_price() - $discount );
            }
        }
    }
}

// Handle mini cart displayed price
add_action( 'woocommerce_cart_item_price', 'category_ferns_display_bulk_discount', 20, 2 );
function category_ferns_display_bulk_discount( $price_html, $cart_item ) {
    $cart     = WC()->cart;
    $category = 'ferns';
    $discount = get_category_custom_discount( $cart, $category );

    if( $discount > 0 && has_term($category, 'product_cat', $cart_item['product_id']) ) {
        $product = wc_get_product( $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'] );
        $args = array( 'price' => floatval( $product->get_price() - $discount ) ); 

        if ( $cart->display_prices_including_tax() ) {
            $product_price = wc_get_price_including_tax( $product, $args );
        } else {
            $product_price = wc_get_price_excluding_tax( $product, $args );
        }
        return wc_price( $product_price );
    }
    return $price_html;
}

代码放在子主题的functions.php文件中(或插件中)。应该可以的

相关问题