Adding custom message on Thank You page by shipping method

限于喜欢 提交于 2019-12-08 04:09:19

问题


I'm trying to add a message to the order-received (Thank You) page, only if the order is using Free Shipping. The message can either replace the standard "Thank you..." message, or can be in addition to.

Here is the code I'm working with. It's based off of the answer here: Customize Order received page based on shipping method in WooCommerce

//add message to order received if outside delivery area
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $thankyou_text, $order ) {
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

        $order_id  = absint( $wp->query_vars['order-received'] );
        $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';


        $method_title_names = array();

        if( in_array( 'Free shipping', $method_title_names ) ) {
            return sprintf( __("%s <div class=\"outside-delivery-checkout\"><b>PLEASE NOTE:</b><br />Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.</div>", "woocommerce"),
    $thankyou_text
                                );
        }
    }
    return $thankyou_text;
}

I can't get it to work correctly, and not sure what's wrong. Any help is greatly appreciated.


回答1:


You need simply the following (where "Free shipping" is the name of your free shipping method):

add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $text, $order ) {
    if( $order->get_shipping_method() == 'Free shipping' ) {
        $text .= ' <div class=\"outside-delivery-checkout\"><strong>'. __("PLEASE NOTE", "woocommerce") . ':</strong><br />'.__("Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.", "woocommerce") . '</div>';
    }
    return $text;
}

Similar: Code goes in function.php file of your active child theme (or active theme). Tested and works.


Customize Order received page based on shipping method in WooCommerce



来源:https://stackoverflow.com/questions/51829609/adding-custom-message-on-thank-you-page-by-shipping-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!