我正在尝试通过此代码设置订单的最低价格:
// Set a minimum dollar amount per order
add_action( \'woocommerce_check_cart_items\', __NAMESPACE__ . \'\\\\spyr_set_min_total\' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if ( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 10;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart\'s total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 10 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
if ( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice(
sprintf(
\'You have not yet reached the minimum order of € 10,00.\' .
\'Your order is: %s %s\',
$total,
get_option( \'woocommerce_currency\' )
),
\'error\'
);
}
}
}
所以一切都正常,但我只需要为在英国下的订单启用它。
我认为解决方案可以是添加以下变量:
$states = array( \'UK\' );
$shippingaddr = WC()->customer->shipping_state;
if ( is_cart() || is_checkout() || $shippingaddr = $states ) {
但它不起作用。有什么建议吗?
编辑
CORRECT CODE
// Only run in the Cart or Checkout pages
global $woocommerce;
if( ( is_cart() || is_checkout()) && "UK" == WC()->customer->shipping_country )
// Set minimum cart total
$minimum_cart_total = 10;
最合适的回答,由SO网友:Prasad Nevase 整理而成
查看您的代码,我认为您只为登录用户启用了签出,因为您无法获得其他国家/地区。如果这是事实,那么你只需要改变你的If条件if( is_cart() || is_checkout() )
到if( ( is_cart() || is_checkout()) && "UK" == WC()->customer->shipping_country )
并将global $woocommerce
在if条件之前。其余代码正确;工作正常。