禁用Woocommerce购物车订单项数量价格计算 [英] Disable Woocommerce cart line item quantity price calculation

查看:43
本文介绍了禁用Woocommerce购物车订单项数量价格计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以在购物车页面中禁用数量 x 件价格的功能.

I am looking for function that can disable the quantity x item price in the cart page.

通常,如果产品价格为€1 ,数量为 3 |订单项小计为€3 ... (1€x 3) .

Normally if product price is €1 and quantity is 3 | Line item subtotal is €3(1€ x 3).

现在,我想不使用x数量计算就保持产品价格,如下所示:
产品价格为€1 ,数量为 3 |订单项小计为€1

Now I Would like to keep the product price without x quantity calculation like this:
Product price is €1 and quantity is 3 | Line item subtotal is €1

有人知道如何禁用此数量价格计算吗?

Does anybody a idea how to disable this quantity price calculation?

推荐答案

是的,可以禁用数量价格计算,但这很复杂……

Yes it's possible to disable the quantity item prices calculation, but it's quite complicated…

代码:

// Custom line item Total/Subtotal price display
add_filter( 'woocommerce_cart_product_subtotal', 'custom_cart_subtotal', 10, 4 );
function custom_cart_subtotal( $product_subtotal, $product, $quantity, $wc_cart ) {
    $price   = $product->get_price();
    $taxable = $product->is_taxable();
    $quantity = 1; // HERE We set the quantity to 1 (So the price is calculated on a quantitity of 1)

    // Taxable
    if ( $taxable ) {
        if ( 'excl' === $wc_cart->tax_display_cart ) {
            $row_price        = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
            $product_subtotal = wc_price( $row_price );

            if ( $wc_cart->prices_include_tax && $wc_cart->tax_total > 0 ) {
                $product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        } else {
            $row_price        = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
            $product_subtotal = wc_price( $row_price );

            if ( ! $wc_cart->prices_include_tax && $wc_cart->tax_total > 0 ) {
                $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        }

    } // Non-taxable
    else {
        $row_price        = $price * $quantity;
        $product_subtotal = wc_price( $row_price );
    }
    return $product_subtotal;
}

// Custom cart subtotal and totals (Prices calculated on quatity = 1)
add_action( 'woocommerce_calculate_totals', 'custom_item_price', 10, 1);
function custom_item_price( $wc_cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $cart_contents_total = 0;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item )
        $cart_contents_total += floatval( strip_tags( $wc_cart->get_product_subtotal( $cart_item['data'], 1 ) ) );

    $wc_cart->subtotal = $cart_contents_total;
    $wc_cart->subtotal_ex_tax = $cart_contents_total;
    $wc_cart->cart_contents_total = $cart_contents_total;
}

// Custom cart subtotal and totals (Prices calculated on quatity = 1)
add_action( 'woocommerce_cart_get_taxes', 'custom_cart_get_taxes', 10, 2);
function custom_cart_get_taxes( $taxes, $wc_cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $taxes = $subtotal_taxes = array();

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ){
        foreach( $cart_item['line_tax_data']['subtotal'] as $key => $tax_price ){
            if( $tax_price > 0 ){
                if( array_key_exists($key, $subtotal_taxes))
                    $subtotal_taxes[$key] += number_format( $tax_price / $cart_item['quantity'], 2 );
                else
                    $subtotal_taxes[$key] = number_format( $tax_price / $cart_item['quantity'], 2 );
            } else {
                if( array_key_exists($key, $subtotal_taxes))
                    $subtotal_taxes[$key] += $tax_price;
                else
                    $subtotal_taxes[$key] = $tax_price;
            }
        }
    }
    foreach ( array_keys( $subtotal_taxes + $wc_cart->get_shipping_taxes() ) as $key ) {
        $taxes[ $key ] = ( isset( $wc_cart->get_shipping_taxes()[ $key ] ) ? $wc_cart->get_shipping_taxes()[ $key ] : 0 ) + ( isset( $subtotal_taxes[ $key ] ) ? $subtotal_taxes[ $key ] : 0 );
    }
    return $taxes;
}


// Custom line item Total/Subtotal set order prices (Prices calculated on quatity = 1)
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 10, 4  );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ){

    $line_tax_data = array();
    foreach( $values['line_tax_data'] as $key_line => $tax ){
        foreach( $tax as $key => $tax_price ){
            if( $tax_price > 0 )
                $line_tax_data[$key_line] = array( $key => number_format( $tax_price / $values['quantity'], 2 ) );
            else
                $line_tax_data[$key_line] = array( $key => $tax_price );
        }
    }

    $item->set_props( array(
        'quantity'     => $values['quantity'],
        'variation'    => $values['variation'],
        'subtotal'     => number_format( $values['line_subtotal'] / $values['quantity'], 2 ),
        'total'        => number_format( $values['line_total'] / $values['quantity'], 2 ),
        'subtotal_tax' => number_format( $values['line_subtotal_tax'] / $values['quantity'], 2 ),
        'total_tax'    => number_format( $values['line_tax'] / $values['quantity'], 2 ),
        'taxes'        => $line_tax_data,
    ) );
}

// Get the correct Cart gran total amount
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $price_total, $wc_cart ){
    $tax_total = 0;
    $taxes_arr = $wc_cart->get_taxes();
    foreach($taxes_arr as $tax)
        $tax_total += $tax;
    return round( $wc_cart->cart_contents_total + $tax_total + $wc_cart->shipping_total + $wc_cart->fee_total, $wc_cart->dp );
}

// Replacing the total tax amount
add_action( 'woocommerce_checkout_create_order', 'custom_set_order_tax_total', 10, 1 );
function custom_set_order_tax_total( $order ) {
    $subtotal_taxes = 0;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
        foreach( $cart_item['line_tax_data']['subtotal'] as $key => $tax_price ){
            if( $tax_price > 0 ){
                $subtotal_taxes += number_format( $tax_price / $cart_item['quantity'], 2 );
            } else {
                $subtotal_taxes += $tax_price;
            }
        }
    }
    $order->set_cart_tax( $subtotal_taxes );
}


// Update order line item tax total
add_action( 'woocommerce_checkout_update_order_meta', 'custom_update_order_item_tax_total', 10, 1 );
function custom_update_order_item_tax_total( $order_id ) {

    global $wpdb;
    $query = $wpdb->get_results( "
        SELECT woim.meta_id, woim.order_item_id as item_id, woi.order_item_type as type, woim.meta_key as akey, woim.meta_value as value
        FROM {$wpdb->prefix}woocommerce_order_items AS woi
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE woi.order_id = $order_id
        AND woim.meta_key IN ( 'tax_amount', '_line_tax_data', 'rate_id' )
    " );

    $taxes = $items = array();
    foreach( $query as $result){
        if( $result->type == 'line_item' ){
            $result_taxes = maybe_unserialize( $result->value );
            foreach( $result_taxes['subtotal'] as $tax_id => $tax_price )
                $taxes[$tax_id] = $tax_price;
        } elseif( $result->type == 'tax' && $result->akey == 'rate_id' ) {
            $items[$result->item_id] = array(
                'price' => $taxes[$result->value],
                'rate_id' => $result->value
            );
        } else {
            $items[$result->item_id]['meta_id'] = $result->meta_id;
        }
    }
    foreach($items as $item_id => $values){
        wc_update_order_item_meta( $item_id, 'tax_amount', $values['price'] );
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

在WooCommerce 3+上进行了测试,并且可以工作(适用于含税或不含税的价格).

Tested on WooCommerce 3+ and works (for prices including Tax or excluding Tax).

我没有使用费用和折扣对其进行测试.所以也许您应该需要一些其他代码...

I havent test it with fees and discounts. So may be you should need some additional code...

这篇关于禁用Woocommerce购物车订单项数量价格计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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