我在函数中有一些代码,当自定义字段的总添加值超过某个值时,该函数会向最终用户显示一条消息。基本上它是一条消息,告诉客户他们已经订购了太多的家具以装入68m3的容器中。
我还有一些代码可以禁用产品上的“添加到购物车”按钮 - 这似乎不起作用。
因此,当我的自定义字段总值超过68并且有一种方法可以完全禁用提交订单时,下面的代码中有一个明显的问题是禁用“添加到购物车”按钮,因此无法提交订购,直到自定义字段总值低于68?
显示消息的代码如下,工作正常。
// display message when cart volume exceeds 68mcube
add_action('woocommerce_before_calculate_totals', 'display_custom_notice',
50, 1);
function display_custom_notice( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 68 && $total_volume != 0 ){
// Display a custom notice
wc_add_notice( __("Note: Your order total volume has reached a full 40ft
container - 68 m3 - Please submit order with volume no greater than 68m3",
"woocommerce"), 'notice' );
}
}
以下功能似乎无法正常工作
// function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
return $total_volume;
}
// Replacing the button add to cart by a link to the product in Shop and
archives pages
add_filter( 'woocommerce_loop_add_to_cart_link',
'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if( get_total_volume() > 68 ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">'
. $button_text . '</a>';
}
return $button;
}
add_action( 'woocommerce_single_product_summary',
'remove_add_to_cart_button', 1 );
function remove_add_to_cart_button() {
// Only when total volume is up to 68
if( get_total_volume() <= 68 ) return;
global $product;
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation',
'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_product_summary',
'innactive_add_to_cart_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary',
'innactive_add_to_cart_button', 30 );
}
}
答案 0 :(得分:3)
替换&#34;下订单&#34;结帐页面中的按钮,当购物车总数超过68立方米时,我们需要在以下代码中使用您的效用函数get_total_volume()
:
// Utility function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
return $total_volume;
}
// Replacing the Place order button when total volume exceed 68 m3
add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {
// Only when total volume is up to 68
if( get_total_volume() <= 68 ) return $order_button;
$order_button_text = __( "Max volume reached", "woocommerce" );
$style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
当总音量超过68 m3时,您将获得一个非活动的自定义按钮:
检查效用函数
get_total_volume()
是否只定义一次,以避免错误......
现在,要禁用添加到购物车按钮,代码中有一些缺失的内容,例如函数inactive_add_to_cart_button()
和其他小东西......试试这个:
// display message when cart volume exceeds 68mcube
add_action('woocommerce_before_calculate_totals', 'display_custom_notice', 50, 1);
function display_custom_notice( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 68 && $total_volume != 0 ){
// Display a custom notice
wc_clear_notices();
wc_add_notice( __("Note: Your order total volume has reached a full 40ft container - 68 m3 - Please submit order with volume no greater than 68m3", "woocommerce"), 'notice' );
}
}
// Utility function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
return $total_volume;
}
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Only when total volume is up to 68
if( get_total_volume() <= 68 ) return $button;
$small_text = '<br><em style="font-size:85%;">(' . __( "Max volume reached", "woocommerce" ) . ')</em>';
$button_text = __( "View product", "woocommerce" ) . $small_text;
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
// Replacing the button add to cart by an inactive button on single product pages
add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
function remove_add_to_cart_button() {
// Only when total volume is up to 68
if( get_total_volume() <= 68 ) return;
global $product;
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_product_summary', 'inactive_add_to_cart_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'inactive_add_to_cart_button', 30 );
}
}
// Utility function: displays a custom innactive add to cart button replacement
function inactive_add_to_cart_button(){
global $product;
$style = 'style="color:#fff;cursor:not-allowed;background-color:#999;"';
echo '<a class="button" '.$style.'>' . __ ( 'Max volume reached', 'woocommerce' ) . '</a>';
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
检查效用函数
get_total_volume()
是否只定义一次,以避免错误......
当购物车总量达到68立方米时,您将获得:
1)在商店和档案页面上:
2)在单品页面上: