基于运输区域的不同最小订单量
Different Minimum Order Amounts Based on Shipping Zone
我正在尝试根据 woocommerce 中的送货区实施不同的最低订单要求,但我似乎无法让它发挥作用。尽管某人的地址位于 3 区,但 49.99 美元的最低限额似乎仍然有效。
// MINIMUM CART AMOUNT FOR CHECKOUT
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 3 );
// Only run it in Cart or Checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// HERE Set minimum cart total amount (for Zone 3 and for other zones)
$min_total = $zone == 3 ? 99.99 : 49.99;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf(
__("A minimum total purchase amount of %s is required to checkout."),
wc_price($min_total)
) . '<strong>', 'error' );
}
}
}
$zone = WC_Shipping_Zone 对象
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 3 );
...
// HERE Set minimum cart total amount (for Zone 3 and for other zones)
$min_total = $zone == 3 ? 99.99 : 49.99;
ERROR: Object of class WC_Shipping_Zone could not be converted to int
这是我认为的错误,您将对象作为整数进行比较,而这应该是 $zone_id
这是否解决了问题?
$min_total = $zone_id == 3 ? 99.99 : 49.99;
// MINIMUM CART AMOUNT FOR CHECKOUT
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
$zone = WC_Shipping_Zones::get_zone_by ( 'zone_name', "1" . 'zone_name', "2" . 'zone_name' , "3" );
// Only run it in Cart or Checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// HERE Set minimum cart total amount (for Zone 1,2,3 and for other zones)
// $min_total = $zone_name == ״1״ ? 99.99 : 200.0;
$min_total = 200;
if ($zone_name == "2") $min_total = 300;
if ($zone_name == "3") $min_total = 350;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
// Display an error message
wc_add_notice( sprintf(
__("minimum order is - %s "),
wc_price( $min_total)
) , 'error' );
}
}
}
我正在尝试根据 woocommerce 中的送货区实施不同的最低订单要求,但我似乎无法让它发挥作用。尽管某人的地址位于 3 区,但 49.99 美元的最低限额似乎仍然有效。
// MINIMUM CART AMOUNT FOR CHECKOUT
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 3 );
// Only run it in Cart or Checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// HERE Set minimum cart total amount (for Zone 3 and for other zones)
$min_total = $zone == 3 ? 99.99 : 49.99;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf(
__("A minimum total purchase amount of %s is required to checkout."),
wc_price($min_total)
) . '<strong>', 'error' );
}
}
}
$zone = WC_Shipping_Zone 对象
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 3 );
...
// HERE Set minimum cart total amount (for Zone 3 and for other zones)
$min_total = $zone == 3 ? 99.99 : 49.99;
ERROR: Object of class WC_Shipping_Zone could not be converted to int
这是我认为的错误,您将对象作为整数进行比较,而这应该是 $zone_id
这是否解决了问题?
$min_total = $zone_id == 3 ? 99.99 : 49.99;
// MINIMUM CART AMOUNT FOR CHECKOUT
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
$zone = WC_Shipping_Zones::get_zone_by ( 'zone_name', "1" . 'zone_name', "2" . 'zone_name' , "3" );
// Only run it in Cart or Checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// HERE Set minimum cart total amount (for Zone 1,2,3 and for other zones)
// $min_total = $zone_name == ״1״ ? 99.99 : 200.0;
$min_total = 200;
if ($zone_name == "2") $min_total = 300;
if ($zone_name == "3") $min_total = 350;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
// Display an error message
wc_add_notice( sprintf(
__("minimum order is - %s "),
wc_price( $min_total)
) , 'error' );
}
}
}