php Woocommerce结帐页面中的自定义统一费率描述文本

yzuktlbb  于 2023-01-08  发布在  PHP
关注(0)|答案(2)|浏览(148)

我有两个统一费率航运区内设置航运方法。在结帐这两种航运方法都可用。
我想在每个统一运费选项下显示一个文本描述。WooCommerce中似乎没有任何选项可以这样做。
我试过下面的代码,但不用说它不工作:

add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
function wc_get_shipping_zone( $package ) {

    if( $package == 'flat_rate:1') {
    return "<p>Arriving on your chosen date between 9am - 1pm Perfect for business addresses & special occasions</p>";
    }
    if( $package == 'flat_rate:2') {
    return "<p>Arriving on your chosen date between 9am - 7pm Perfect for residential addresses</p>";
    }
}

有谁能帮我们完成这个任务吗?
这是我希望结帐时的描述:

bbmckpt7

bbmckpt71#

正确的挂钩函数添加额外的信息到您的航运“统一费率”的方法是:

add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate', 20, 2 );
function action_after_shipping_rate ( $method, $index ) {
    // Targeting checkout page only:
    if( is_cart() ) return; // Exit on cart page

    if( 'flat_rate:1' === $method->id ) {
        echo __("<p>Arriving on your chosen date between 9am - 1pm Perfect for business addresses & special occasions</p>");
    }
    if( 'flat_rate:2' === $method->id ) {
        echo __("<p>Arriving on your chosen date between 9am - 7pm Perfect for residential addresses</p>");
    }
}

此代码在您的活动子主题(或活动主题)的function.php文件上运行。

63lcw9qa

63lcw9qa2#

更可靠的解决方案是在发运方法表单中添加新的说明字段:

/**
 * Add extra form field to all shipping methods available
 */
add_action( 'woocommerce_loaded', 'smdfw_add_filters' );
function smdfw_add_filters() {
    $shipping_methods = WC()->shipping->get_shipping_methods();

    foreach ( $shipping_methods as $id => $shipping_method ) {
        add_filter( "woocommerce_shipping_instance_form_fields_$id", 'smdfw_add_form_fields' );
    }
}

/**
 * Add description field to shipping method form
 */
function smdfw_add_form_fields( $fields ) {
    $fields['description'] = array(
        'title' => __( 'Description', 'smdfw' ),
        'type'  => 'textarea',
    );
    return $fields;
}

然后在前端显示:

/**
 * Load description as metadata
 */
add_filter( 'woocommerce_shipping_method_add_rate_args', 'smdfw_add_rate_description_arg', 10, 2 );
function smdfw_add_rate_description_arg( $args, $method ) {
    $args['meta_data']['description'] = $method->get_option( 'description' );
    return $args;
}

/**
 * Display description field after method label
 */
add_action( 'woocommerce_after_shipping_rate', 'smdfw_output_shipping_rate_description', 10 );
function smdfw_output_shipping_rate_description( $method ) {
    $meta_data = $method->get_meta_data();
    if ( array_key_exists( 'description', $meta_data ) ) {
        echo '<div><small class="smdfw">' . esc_html( $meta_data['description'] ) . '</small></div>'
    }
}

我需要这样做的许多WooCommmerce项目,所以我做了一个免费的插件了:https://wordpress.org/plugins/wc-shipping-method-description/

相关问题