我需要某些项目有混合和匹配的数量折扣的基础上.我已经看到在购物车底部使用总体折扣来实现这一点的一些其他代码,但是我需要每个项目的价格来反映折扣,我还需要将该价格结转到销售订单中,以便我们可以适当地开具发票.以下是我目前掌握的情况:

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;
    }
}

问题是小计没有以正确的价格更新.我假设这是因为我只更改了显示价格,而不是实际计算的价格.有什么办法可以照我说的做吗?

推荐答案

首先,您没有使用右钩子,您的代码可以简化,并且有遗漏的地方.

要根据特定产品类别的数量对购物车项目进行折扣,请try :

// 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;
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.

Php相关问答推荐

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

保存PHP条带以备将来使用,不显示用户界面

MySQLi是否在事务错误时删除保存点(并且仅删除保存点)?

使用ESI的Symfony Sulu清漆

htaccess重命名index.php以在URL中显示为SEO的友好名称

Laravel路由到控制器传输变量解决方案需要

WordPress:今天的计数';修改和发布的帖子

将数据推入所有子数组条目

Woocommerce单一产品Ajax添加到带有自定义购物车项目数据的购物车

如何批量处理消息总线中的消息

使用 wp_footer 挂钩添加一些 CSS 不起作用

为什么 php 只能在我的 Nginx Web 服务器的某些目录中工作?

如何在 php 中获取正确的时间

从图像URL中获取文件扩展名?

Woocommerce注册中的自定义复选框验证错误提示问题

如何在WooCommerce管理产品列表中显示运输类别列

Laravel的Storage::exists返回false,而file_exists返回true.

$_SERVER 超全局变量在 PHP 中是否保证可写?

我正在使用 Baryryvdh/laravel-snappy 从 HTML 导出 PDF 它正在工作但缩小了 pdf 文件

如何在 Laravel 9 中判断异常是否可报告?