在单个产品页面上将 WooCommerce 运费替换为零

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

根据我对上一个问题 的回答,我正在尝试将 0 替换为免费或我选择的任何词。

问题是,无论我做什么,它都不起作用。 这是我的尝试,我需要帮助:

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

这是我尝试添加/编辑但没有成功的内容:

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

要显示 'free' 而不是 0,您需要更改 if/else 条件。通过添加到我的答案的评论标签进行解释。

所以你得到:

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