Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1320805
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI'm trying through this code to set a minimum price for orders:
// 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'
);
}
}
}
So everything works, but I need to enable it only for orders placed in UK.
I thought that the solution could be adding these variables:
$states = array( 'UK' );
$shippingaddr = WC()->customer->shipping_state;
if ( is_cart() || is_checkout() || $shippingaddr = $states ) {
but it's not working. Any suggestions?
EDIT.
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;
Closed. This question is off-topic. It is not currently accepting answers.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI'm trying through this code to set a minimum price for orders:
// 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'
);
}
}
}
So everything works, but I need to enable it only for orders placed in UK.
I thought that the solution could be adding these variables:
$states = array( 'UK' );
$shippingaddr = WC()->customer->shipping_state;
if ( is_cart() || is_checkout() || $shippingaddr = $states ) {
but it's not working. Any suggestions?
EDIT.
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;
Share
Improve this question
edited Nov 2, 2015 at 18:29
sb0k
asked Nov 2, 2015 at 16:49
sb0ksb0k
838 bronze badges
3
- Where are you putting this code ie: functions.php or plugin? Are you requiring customers to be logged in before shopping? – Scriptonomy Commented Nov 2, 2015 at 17:56
- @Scriptonomy in functions.php .. i solved with the suggestion of ItsMePN. You can see the right code. – sb0k Commented Nov 2, 2015 at 18:30
- 3rd party plugin dev support is offtopic and not in this stacks scope – Tom J Nowell ♦ Commented Oct 1, 2020 at 13:13
1 Answer
Reset to default 1Looking at your code I think you have enabled checkout only for logged in users because you won't get the country otherwise. If that's the fact then you just need to change your if condition from if( is_cart() || is_checkout() )
to if( ( is_cart() || is_checkout()) && "UK" == WC()->customer->shipping_country )
and put global $woocommerce
before the if condition. Rest of the code is correct & working fine.
本文标签: WooCommerce set a min order price for a single country
版权声明:本文标题:WooCommerce set a min order price for a single country 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742089949a2420202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论