我有一个场景,我需要删除 Woo-commerce 结帐屏幕上的“下订单”按钮。
目前我有两种运输方式:灵活运输和货运。
如果客户将运输类别为“货运”的商品添加到购物车,我当前的代码将禁用灵活的运输方法,然后货运方法会显示一条“致电了解当前费率”的消息。
问题是,他们基本上仍然可以结帐,而无需支付任何运费,这就是为什么如果货运是唯一可用的运输方式,我需要删除或更换下订单按钮。
这是我当前正在使用并尝试修改但未成功的代码:
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;
}
有一个简单的钩子还是我缺少的东西?
我已经搞乱这个有一段时间了并且碰壁了。
请尝试以下操作,当在购物车商品中找到特定的运输类别时,这将输出非活动的灰色“下订单”订单按钮:
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 文件中。经过测试并有效。
要完全删除“下订单”按钮,您将使用类似的方法:
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 文件中。经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)