在单个产品页面上用免费替换 WooCommerce 运费的零 [英] Replace zero with FREE for WooCommerce shipping rates on single product page

查看:22
本文介绍了在单个产品页面上用免费替换 WooCommerce 运费的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于我上一个问题的答案,

Based on the answer on my previous question, Issue display shipping data on WooCommerce single product page, I'm trying to replace the 0 with FREE or whatever word I choose.

Problem is, whatever I do, it does not work. This is my attempt and I need help with it:

add_action('woocommerce_after_add_to_cart_form', 'display_shipping_on_product_page', 10, 0);
function display_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>' . __( '<b>Available Shipping</b>', '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 = str_replace($instance_cost, "FREE" );
            }
            
            $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>';
}

This is what I tried to add / edit without success:

$instance_cost = str_replace($instance_cost, "FREE" );

解决方案

To display 'free' instead of 0, you need to change your if/else conditions. Explanation via comment tags added to my answer.

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;
            
            // Initialize
            $above = '';
            $output_cost = __( 'Free', 'woocommerce' );

            // Cost isset
            if ( isset( $instance['cost'] ) ) {
                // NOT empty
                if ( ! empty ( $instance['cost'] ) ) {
                    // Output
                    $output_cost = wc_price( $instance['cost'] );   
                }
            }
            
            // Min amount isset
            if ( isset( $instance['min_amount'] ) ) {
                // NOT empty
                if ( ! empty ( $instance['min_amount'] ) ) {
                    // Above
                    $above = __( 'above ', 'woocommerce' );
                    
                    // Output
                    $output_cost = wc_price( $instance['min_amount'] );
                }
            }
            
            echo $instance['title'] . ': ' . $above . '<strong>' . $output_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 );

这篇关于在单个产品页面上用免费替换 WooCommerce 运费的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆