微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 WooCommerce 单一产品页面上发布显示运输数据

如何解决在 WooCommerce 单一产品页面上发布显示运输数据

为了提供更多信息,使用最新的 WC 和 WP,我在 WooCommerce 产品页面显示运输数据时收到两条通知

Notice: Undefined index: min_amount on line 89 and 91

我在我的主题functions.PHP 文件中有代码并且它运行良好,直到我开始调试所有内容 (wp-config)。

错误是指这两行:

$cost = $instance['cost'] ? $instance['cost'] : $instance['min_amount'];
$above = $instance['min_amount'] ? 'above ' : '';

任何人都可以对我哪里出错/我在这里做错了什么提出一些见解吗?

这是我的完整代码

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>';
}

解决方法

检查是否实际设置了值,因为对于每种运输方式而言,情况并非总是如此。

所以你得到:

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 );

enter image description here


相关:Replace zero with FREE for WooCommerce shipping rates on single product page

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。