我想在 Woocommerce 结帐页面中为特定的运输类别设置一条消息。
我尝试使用:
add_action( 'woocommerce_review_order_before_order_total', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
$shipping_class_id = 14; // Your shipping class Id
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// Check cart items for specific shipping class, displaying a notice
//if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
var_dump($cart_item['data']->get_shipping_class_id());
echo "Do poznámky k objednávce napište adresu Zásilkovny nebo ZBoxu, kam chcete objednávku doručit.";
//break;
//}
}
}
但是当我尝试 var_dump
选择运输类别,它总是返回我int(0)
.我用过类似的东西:Cart Message for a Specific Shipping Class in WooCommerce ,因为它对我不起作用。
最佳答案
Cart Message for a Specific Shipping Class in WooCommerce答案仍然适用于上一个 woocommerce 版本。
您的问题代码有效并显示运输类 Id……所以还有其他问题正在制造麻烦。
确保购物车项目为其设置了所需的运输类别。
尝试按照以下步骤在结帐时显示基于特定运输类别的消息 弹头 :
add_action( 'woocommerce_review_order_before_order_total', 'checkout_shipping_class_message' );
function checkout_shipping_class_message(){
// Here your shipping class slugs in the array
$shipping_classes = array('truck');
// Here your shipping message
$message = __("Please add some shipping details in order notes field.", "woocommerce");
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
$shipping_class = $cart_item['data']->get_shipping_class();
// echo '<pre>' . print_r($shipping_class, true) . '</pre>'; // Uncomment for testing
// Check cart items for specific shipping class, displaying a message
if( in_array($shipping_class, $shipping_classes ) ){
echo '<tr class="shipping-note">
<td colspan="2"><strong>'.__("Note", "woocommerce").':</strong> '.$message.'</td>
</tr>';
break;
}
}
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。
关于php - WooCommerce 结帐显示基于特定运输类别的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66918659/