我有一个场景,我需要在结帐屏幕上删除Woo-commerce的“下订单”按钮。
目前我有2种送货方式:灵活运费和运费
如果客户将装运类别为“运费”的商品添加到购物车,我当前的代码会停用灵活的送货方式,然后运费方式会显示“请求当前费率”的消息。
问题是他们仍然可以在没有支付任何运费的情况下结账,这就是为什么如果货运是唯一可用的运输方式,我需要将地方订单按钮移除或更换。
以下是我目前使用的代码并尝试修改失败:
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;
}
是否有一个简单的钩子或我遗漏的东西?
我已经搞砸了一段时间而且正在撞墙。
答案 0 :(得分:0)
尝试以下操作,输出无效的灰色"下订单"在购物车项目中找到特定货运类时的订单按钮:
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文件中。经过测试和工作。
完全删除&#34;下订单&#34;按钮,您将使用此类似的代码:
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文件中。经过测试和工作。