I'm trying to add a custom message when the customer selects a specific shipping method on the checkout page

32 Views Asked by At

My problem is that the message I need to show consists of JavaScript code that contains an API key. I previously placed the JavaScript code in a shortcode. In an open environment and without tying it to any action, necessary for its display, the shortcode displays its content. When I try to show it where I need it following a specific action the content is blocked. I used this code to insert the shortcode. I can't figure out what's missing

add_action( 'woocommerce_cart_totals_after_shipping', 'display_shipping_method_custom_notice' );
add_action( 'woocommerce_review_order_after_shipping', 'display_shipping_method_custom_notice' );
function display_shipping_method_custom_notice() {
    $targeted_methods = array('local_pickup:3'); // <== Define the desired shipping method ID(s)
    $chosen_methods   = WC()->session->get( 'chosen_shipping_methods' );

    if ( ! empty($chosen_methods) ) {
        // Targeting the chosen shipping method
        if ( in_array( reset($chosen_methods), $targeted_methods) ) {
            echo '<tr class="shipping"><td colspan="2" style="text-align:center">' .
            do_shortcode("[my_shortcode_here]") .
            '</td></tr>';
        }
    }
}

//The code you see below is the content of the shortcode

<script maps src="https://mysiteURL/maps/mapsDropOffPoints.js" key="mypersonalAPIKey"></script>
<div class="point_id"></div> <!-- Optional -->
<div class="point_name"></div> <!-- Optional -->
<div class="point_address"></div> <!-- Optional -->
<div class="courier_name"></div> <!-- Optional -->
<div class="point_type"></div> <!-- Optional -->
0

There are 0 best solutions below