在WooCommerce结帐时是否可以提供基于运输成本的折扣?

时间:2019-06-26 09:08:13

标签: php wordpress woocommerce

我们想根据运输成本给客户打折(运输成本的50%)。 下面是操纵折扣的代码,可以,但是我无法获得运费。尝试过php $current_shipping_cost = WC()->cart->get_cart_shipping_total();,但没有返回正确的值。 有没有办法做到这一点?

function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) { 

        if ($coupon->code == 'custom'){

            /*It is needed to know the shipping cost to calculate the discount. 
            $current_shipping_cost = WC()->cart->get_cart_shipping_total();
echo $current_shipping_cost; is not returning the correct value.
            */
             return $discount;
            }
        }
//add hook to coupon amount hook
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 100, 5);

4 个答案:

答案 0 :(得分:0)

使用"woocommerce_after_calculate_totals"挂钩。 它在结帐或购物车页面上为您提供购物车对象。如果您的站点使用税收,则可以同时获得运输总额和运输税总额。

$cart->shipping_total;
$cart->shipping_tax_total;

答案 1 :(得分:0)

@LászlóT ,您可以使用以下代码获取运费。看到代码我给了运输成本的50%的折扣。这对我有用。

function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) {
    if ($coupon->code == 'ship_discount'){  // ship_discount is coupon code
        $shipping_cost = 0;
        foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
            $shipping_cost += $rate->cost; // calculate  shipping cost
        }
        return  $shipping_cost/2;  //shipping cost to calculate the discount
    }
}
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5);

我希望这对您有帮助。

谢谢

答案 2 :(得分:0)

当在购物车项目中找到特定的已定义运输方式时,以下代码将为“统一费率”运输方式设置50%的运输成本。

运输“统一费率”设置:应定义您的运输费用。

add_filter('woocommerce_package_rates', 'shipping_costs_discounted_based_on_shipping_class', 10, 2);
function shipping_costs_discounted_based_on_shipping_class( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // Your settings bellow
    $shipping_class = 'large'; // <=== Shipping class slug
    $percentage     = 50; //      <=== Discount percentage

    $discount_rate  = $percentage / 100;
    $is_found       = false;

    // Loop through cart items and checking for the specific defined shipping class
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class )
            $is_found = true;
    }

    // Set shipping costs to 50% if shipping class is found
    if( $is_found ){
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;
            // Targeting "flat rate"
            if( 'flat_rate' === $rate->method_id  ){
                $rates[$rate_key]->cost = $rate->cost * $discount_rate;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $has_taxes = true;
                        $taxes[$key] = $tax * $discount_rate;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

这是完整的参考 Shipping cost discount based on a shipping classes in Woocommerce

答案 3 :(得分:0)

这可以帮助您获得折扣。

add_action('woocommerce_cart_calculate_fees','woocommerce_discount_for_shipping' );
function woocommerce_discount_for_shipping()
{
    global $woocommerce;
    //$discount_cart = $cart->discount_cart;
    $shipping_including_tax = $woocommerce->cart->shipping_total + $woocommerce->cart->shipping_tax_total;
    $percentage = 0.5;
    $discount = $shipping_including_tax * $percentage;
    //var_dump($discount);
    $woocommerce->cart->add_fee('Shipping Discount:', -$discount, false);
}