WoCommerce:发货后使用优惠券吗?

时间:2018-03-18 作者:resunoiz

在更新的woocommerce版本中,通过优惠券代码插入折扣将导致折扣总额,并向其添加运费。在大多数情况下,这是正确的。

我的一位客户希望用woocommerce的“旧方法”计算优惠券:他想提供一个按total order amount included shipping. 有没有黑客迫使当前的woocommerce版本以这种方式工作?

1 个回复
SO网友:Amol Sawant

对只需将以下代码添加到主题函数中。phpupvote我的答案…:-)

<?php
// WooCommerce Shipping Calculated after Coupon
add_filter( \'woocommerce_shipping_free_shipping_is_available\', \'filter_shipping\', 10, 2 );
function filter_shipping( $is_available, $package ) {
    if ( WC()->cart->prices_include_tax )
        $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
    else
        $total = WC()->cart->cart_contents_total;
    $total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
    // You can hardcode the number or get the setting from the shipping method
    $shipping_settings = get_option(\'woocommerce_free_shipping_settings\');
    $min_total = $shipping_settings[\'min_amount\'] > 0 ? $shipping_settings[\'min_amount\'] : 0;

    if ( 50 > $total ) {
        $is_available = false;
    }
    return $is_available;
}
// This basically recalculates totals after the discount has been added
add_action( \'woocommerce_calculate_totals\', \'change_shipping_calc\' );
function change_shipping_calc( $cart ) {
        $packages = WC()->cart->get_shipping_packages();
        // Calculate costs for passed packages
        $package_keys       = array_keys( $packages );
        $package_keys_size  = sizeof( $package_keys );
        for ( $i = 0; $i < $package_keys_size; $i ++ ) {
            unset( $packages[ $package_keys[ $i ] ][\'rates\'] );
            $package_hash   = \'wc_ship_\' . md5( json_encode( $packages[ $package_keys[ $i ] ] ) );
            delete_transient( $package_hash );
        }
        // Calculate the Shipping
        $cart->calculate_shipping();
        // Trigger the fees API where developers can add fees to the cart
        $cart->calculate_fees();
        // Total up/round taxes and shipping taxes
        if ( $cart->round_at_subtotal ) {
            $cart->tax_total          = $cart->tax->get_tax_total( $cart->taxes );
            $cart->shipping_tax_total = $cart->tax->get_tax_total( $cart->shipping_taxes );
            $cart->taxes              = array_map( array( $cart->tax, \'round\' ), $cart->taxes );
            $cart->shipping_taxes     = array_map( array( $cart->tax, \'round\' ), $cart->shipping_taxes );
        } else {
            $cart->tax_total          = array_sum( $cart->taxes );
            $cart->shipping_tax_total = array_sum( $cart->shipping_taxes );
        }
        // VAT exemption done at this point - so all totals are correct before exemption
        if ( WC()->customer->is_vat_exempt() ) {
            $cart->remove_taxes();
        }
}
add_filter( \'woocommerce_table_rate_query_rates_args\', \'filter_shipping_2\', 10 );
function filter_shipping_2( $arguments ) {
    if ( WC()->cart->prices_include_tax )
        $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
    else
        $total = WC()->cart->cart_contents_total;
    $total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
    $arguments[\'price\'] = $total;
    return $arguments;
}

结束

相关推荐