删除特定发货的 Woocommerce "place order" 按钮 class
Remove Woocommerce "place order" button for a specific shipping class
我有一个场景需要删除 Woo-commerce 结帐屏幕上的 "Place order" 按钮。
目前我有两种送货方式:灵活送货和货运
如果客户将运费 class 为 "Freight" 的商品添加到他们的购物车,我当前的代码会禁用灵活的送货方式,然后运费方式显示消息 "Call for current rates".
问题是他们基本上仍然可以在不支付任何运费的情况下结账,这就是为什么如果货运是唯一可用的运输方式,我需要删除或更换下订单按钮。
这是我目前正在使用并尝试修改但未成功的代码:
add_filter( 'woocommerce_package_rates', 'wc_hide_free_shipping_for_shipping_class', 10, 2 );
function wc_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = 332;
$in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates['flexible_shipping_7_2'] );
}
return $rates;
}
是否有简单的钩子或我缺少的东西?
我已经弄乱了一段时间,现在碰壁了。
尝试以下操作,当在购物车商品中找到特定运输 class 时,将输出一个非活动的灰色 "Place Order" 订购按钮:
add_filter('woocommerce_order_button_html', 'inactive_order_button_html' );
function inactive_order_button_html( $button ) {
// HERE define your targeted shipping class
$targeted_shipping_class = 332;
$found = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
$found = true; // The targeted shipping class is found
break; // We stop the loop
}
}
// If found we replace the button by an inactive greyed one
if( $found ) {
$style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
$button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
$button = '<a class="button" '.$style.'>' . $button_text . '</a>';
}
return $button;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
要完全删除 "Place order" 按钮,您将使用类似的按钮:
add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
function remove_order_button_html( $button ) {
// HERE define your targeted shipping class
$targeted_shipping_class = 332;
$found = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
$found = true; // The targeted shipping class is found
break; // We stop the loop
}
}
// If found we remove the button
if( $found )
$button = '';
return $button;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
我有一个场景需要删除 Woo-commerce 结帐屏幕上的 "Place order" 按钮。
目前我有两种送货方式:灵活送货和货运
如果客户将运费 class 为 "Freight" 的商品添加到他们的购物车,我当前的代码会禁用灵活的送货方式,然后运费方式显示消息 "Call for current rates".
问题是他们基本上仍然可以在不支付任何运费的情况下结账,这就是为什么如果货运是唯一可用的运输方式,我需要删除或更换下订单按钮。
这是我目前正在使用并尝试修改但未成功的代码:
add_filter( 'woocommerce_package_rates', 'wc_hide_free_shipping_for_shipping_class', 10, 2 );
function wc_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = 332;
$in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates['flexible_shipping_7_2'] );
}
return $rates;
}
是否有简单的钩子或我缺少的东西?
我已经弄乱了一段时间,现在碰壁了。
尝试以下操作,当在购物车商品中找到特定运输 class 时,将输出一个非活动的灰色 "Place Order" 订购按钮:
add_filter('woocommerce_order_button_html', 'inactive_order_button_html' );
function inactive_order_button_html( $button ) {
// HERE define your targeted shipping class
$targeted_shipping_class = 332;
$found = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
$found = true; // The targeted shipping class is found
break; // We stop the loop
}
}
// If found we replace the button by an inactive greyed one
if( $found ) {
$style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
$button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
$button = '<a class="button" '.$style.'>' . $button_text . '</a>';
}
return $button;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
要完全删除 "Place order" 按钮,您将使用类似的按钮:
add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
function remove_order_button_html( $button ) {
// HERE define your targeted shipping class
$targeted_shipping_class = 332;
$found = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
$found = true; // The targeted shipping class is found
break; // We stop the loop
}
}
// If found we remove the button
if( $found )
$button = '';
return $button;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。