【问题标题】:Replace zero with FREE for WooCommerce shipping rates on single product page将零替换为免费的 WooCommerce 单个产品页面上的运费
【发布时间】:2021-06-27 04:23:18
【问题描述】:

根据我上一个问题Issue display shipping data on WooCommerce single product page 的答案,我正在尝试将 0 替换为 FREE 或我选择的任何单词。

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

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

【问题讨论】:

    标签: php wordpress woocommerce price shipping-method


    【解决方案1】:

    要显示 '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 );
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2017-03-09
      • 1970-01-01
      • 2017-03-10
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      相关资源
      最近更新 更多