从基于 WooCommerce 运输方式的短代码添加自定义消息

问题描述 投票:0回答:1

我正在尝试根据客户选择的运输方式添加自定义消息。使用这段代码,我能够做到这一点。唯一的问题是我应该用短代码替换文本部分。

add_action( 'woocommerce_cart_totals_after_shipping', 'ts_shipping_method_custom_notice' );
add_action( 'woocommerce_review_order_after_shipping', 'ts_shipping_method_custom_notice' );
function ts_shipping_method_custom_notice() {
    // HERE DEFINE YOUR TARGETED SHIPPING METHOD(S)
    $targeted_shipping_methods = array('local_pickup:3'); // Replace with the actual shipping method ID(s)
    // Get the chosen shipping methods
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    if ( ! empty( $chosen_methods ) ) {
        $chosen_method = reset( $chosen_methods );
        // Check if the chosen shipping method is in the targeted list
        if ( in_array( $chosen_method, $targeted_shipping_methods ) ) {
            echo '<tr class="shipping">
                <td colspan="2" style="text-align:center">' . sprintf(
                    __( "a vit è na briosh", "woocommerce"),
                    '<strong>al 100%</strong>',
                    '<strong>' . WC()->customer->get_shipping_postcode() . '</strong>'
                ) . '</td>
            </tr>';
        }
    }
}
php wordpress woocommerce shortcode shipping-method
1个回答
1
投票

要显示短代码,您可以在代码中使用 WordPress

do_shrotcode()
功能,例如:

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>';
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.