在 WooCommerce 单一产品页面上发布显示运输数据 [英] Issue display shipping data on WooCommerce single product page
问题描述
为了提供更多信息,使用最新的 WC 和 WP,我在 WooCommerce 产品页面上显示运输数据时收到两条通知.
注意:未定义索引:第 89 和 91 行的 min_amount
我在主题的 functions.php
文件中有代码,它运行良好,直到我开始调试所有内容 (wp-config
).
错误是指这两行:
$cost = $instance['cost'] ?$instance['cost'] : $instance['min_amount'];$above = $instance['min_amount'] ?'以上 ' : '';
任何人都可以对我哪里出错/我在这里做错了什么提出一些见解吗?
这是我的完整代码:
add_action('woocommerce_after_add_to_cart_form', 'shipping_on_product_page');函数shipping_on_product_page() {//获取所有区域$zones = WC_Shipping_Zones::get_zones();//获取商店基地国家$base_country = WC()->countries->get_base_country();$base_city = WC()->countries->get_base_city();//开始显示表格echo ''.__( '可用运费', 'woocommerce');echo '<br><small><span class="shipping-time-cutoff">所有订单均从.$base_country."发货.周一至周五上午 12 点之前订购,可在.$base_city."内当天送达.周一至周四下午 3 点前下单,次日送达.</span></small>';echo '';//获取每个区域的名称和每个区域的每种运输方式foreach ( $zones as $zone_id => $zone ) {echo '';echo ''.$zone['zone_name'] .'</strong>'.'</td><td>';$zone_shipping_methods = $zone['shipping_methods'];foreach ( $zone_shipping_methods as $index => $method ) {$instance = $method->instance_settings;$cost = $instance['cost'] ?$instance['cost'] : $instance['min_amount'];$above = $instance['min_amount'] ?'以上 ' : '';回声 $instance['title'] .':'.$以上.'<强>'.wc_price( $cost ) .'</strong>'.'
';}回声'</td></tr>';}echo '</table></small></div>';} 解决方案 检查值是否实际设置,因为并非每种运输方式都如此.
所以你得到:
function action_woocommerce_after_add_to_cart_form() {//获取所有区域$zones = WC_Shipping_Zones::get_zones();//获取商店基地国家$base_country = WC()->countries->get_base_country();$base_city = WC()->countries->get_base_city();//开始显示表格echo ''.__( '可用运费', 'woocommerce');echo '<br><small><span class="shipping-time-cutoff">所有订单均从.$base_country."发货.周一至周五上午 12 点之前订购,可在.$base_city."内当天送达.周一至周四下午 3 点前下单,次日送达.</span></small>';echo '';//获取每个区域的名称和每个区域的每种运输方式foreach ( $zones as $zone_id => $zone ) {echo '';echo ''.$zone['zone_name'] .'</strong>'.'</td><td>';$zone_shipping_methods = $zone['shipping_methods'];foreach ( $zone_shipping_methods as $index => $method ) {$instance = $method->instance_settings;如果 ( isset( $instance['min_amount'] ) ) {$instance_min_amount = $instance['min_amount'];} 别的 {$instance_min_amount = 0;}如果 ( isset( $instance['cost'] ) ) {$instance_cost = $instance['cost'];} 别的 {$instance_cost = 0;}$cost = $instance_cost ?$instance_cost : $instance_min_amount;$above = $instance_min_amount ?'以上 ' : '';回声 $instance['title'] .':'.$以上.'<强>'.wc_price( $cost ) .'</strong>'.'
';}回声'</td></tr>';}echo '</table></small></div>';}add_action('woocommerce_after_add_to_cart_form', 'action_woocommerce_after_add_to_cart_form', 10, 0);
相关:在单个产品页面上用免费替换 WooCommerce 运费
As an attempt in being extra informative, using the latest WC and WP, I get two notices when displaying shipping data on the WooCommerce product page.
Notice: Undefined index: min_amount on line 89 and 91
I have the code in my theme's functions.php
file and it worked fine until I started debugging everything (wp-config
).
The errors refers to these two lines:
$cost = $instance['cost'] ? $instance['cost'] : $instance['min_amount'];
$above = $instance['min_amount'] ? 'above ' : '';
Can anyone please shed some insight as to where I am going wrong / what I am doing wrong here?
This is my code in full:
add_action( 'woocommerce_after_add_to_cart_form', 'shipping_on_product_page' );
function shipping_on_product_page() {
// get all zones
$zones = WC_Shipping_Zones::get_zones();
// get the shop base country
$base_country = WC()->countries->get_base_country();
$base_city = WC()->countries->get_base_city();
// start display of table
echo '<div>' . __( 'Available Shipping', 'woocommerce' );
echo '<br><small><span class="shipping-time-cutoff">All orders are shipped from the '.$base_country.'. Order before 12AM Mon-Fri for same day delivery within '.$base_city.'. Order before 3PM Mon-Thu for next day delivery.</span></small>';
echo '<small><table class="shipping-and-delivery-table">';
// get name of each zone and each shipping method for each zone
foreach ( $zones as $zone_id => $zone ) {
echo '<tr><td>';
echo '<strong>' . $zone['zone_name'] . '</strong>' . '</td><td>';
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
$cost = $instance['cost'] ? $instance['cost'] : $instance['min_amount'];
$above = $instance['min_amount'] ? 'above ' : '';
echo $instance['title'] . ': ' . $above . '<strong>' . wc_price( $cost ) . '</strong>' . '<br>';
}
echo '</td></tr>';
}
echo '</table></small></div>';
}
解决方案 Check if the values are actually set, because that is not always the case for each shipping method.
So you get:
function action_woocommerce_after_add_to_cart_form() {
// get all zones
$zones = WC_Shipping_Zones::get_zones();
// get the shop base country
$base_country = WC()->countries->get_base_country();
$base_city = WC()->countries->get_base_city();
// start display of table
echo '<div>' . __( 'Available Shipping', 'woocommerce' );
echo '<br><small><span class="shipping-time-cutoff">All orders are shipped from the '.$base_country.'. Order before 12AM Mon-Fri for same day delivery within '.$base_city.'. Order before 3PM Mon-Thu for next day delivery.</span></small>';
echo '<small><table class="shipping-and-delivery-table">';
// get name of each zone and each shipping method for each zone
foreach ( $zones as $zone_id => $zone ) {
echo '<tr><td>';
echo '<strong>' . $zone['zone_name'] . '</strong>' . '</td><td>';
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
if ( isset( $instance['min_amount'] ) ) {
$instance_min_amount = $instance['min_amount'];
} else {
$instance_min_amount = 0;
}
if ( isset( $instance['cost'] ) ) {
$instance_cost = $instance['cost'];
} else {
$instance_cost = 0;
}
$cost = $instance_cost ? $instance_cost : $instance_min_amount;
$above = $instance_min_amount ? 'above ' : '';
echo $instance['title'] . ': ' . $above . '<strong>' . wc_price( $cost ) . '</strong>' . '<br>';
}
echo '</td></tr>';
}
echo '</table></small></div>';
}
add_action( 'woocommerce_after_add_to_cart_form', 'action_woocommerce_after_add_to_cart_form', 10, 0 );
Related: Replace zero with FREE for WooCommerce shipping rates on single product page
这篇关于在 WooCommerce 单一产品页面上发布显示运输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆