php - WooCommerce 购物车和结账中运输(标签)方法的额外 HTML

标签 php wordpress woocommerce cart checkout

我正在尝试将预计到达时间添加到我的运输方式的标签上。不过,我希望到达时间标签中包含一些额外的 HTML。 (使它们更小并且字体粗细不同)。

这是一个简单的例子:

function add_estimated_arrival_times($rates, $package){
    
    $groundStuff = $timeReturnedFromAPI; //collect a bunch of cart information, post to the UPS API, return estimated arrival time to $timeReturnedFromAPI
    $ground_rate_id = 'ups:6:09'; //UPS ground rate ID
    
    // Loop through available shipping rates, add arrival time if rate is UPS ground
    foreach ( $rates as $rate_key => $rate ) {
        if( $ground_rate_id === $rate_key ) {
            $rates[$rate_key]->label .= ' <span style="font-size: 12px; font-weight: normal;">(' . $groundStuff . ')</span>'; //<---- problem: HTML is not added
            break;
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','add_estimated_arrival_times', 50, 2 );

上面的代码成功将纯文本添加到送货方式标签,但 HTML 消失了。

对此有什么好的解决方法吗?

最佳答案

您使用的 Hook 仅允许您编辑标签的文本:

function filter_woocommerce_package_rates( $rates, $package ){    
    // Loop through available shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        $rates[$rate_key]->label = __( 'My label', 'woocommerce' );
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );

也可以通过 woocommerce_cart_shipping_method_full_label Hook 进行调整:

function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
    // Target a specific method ID
    if ( $method->id == 'local_pickup:1' ) {
        // Overwrite
        $label = __( 'My new label', 'woocommerce' );
    } elseif ( $method->id == 'free_shipping:2' ) {
        // Append
        $label .= __( ' - Extra text label', 'woocommerce' );
    }

    return $label; 
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );

但是您的问题是在标签后面添加一些内容,这可以通过 woocommerce_after_shipping_rate Hook 来完成:

function action_woocommerce_after_shipping_rate( $method, $index ) {
    // Target a specific method ID
    if ( $method->id == 'local_pickup:1' ) {
        echo '<p>test</p>';
    } else {
        $groundStuff = 'some value';
        
        echo '<span style="font-size: 12px; font-weight: normal;">(' . $groundStuff . ')</span>';
    }
}
add_action( 'woocommerce_after_shipping_rate', 'action_woocommerce_after_shipping_rate', 10, 2 );

或者

通过覆盖 /cart/cart-shipping.php第 40 行 @version 3.6.0 - 可以通过将其复制到 yourtheme/woocommerce/cart/cart-shipping.php

来覆盖此模板

关于php - WooCommerce 购物车和结账中运输(标签)方法的额外 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69816193/

相关文章:

php - 无法替换 php 中的回车符和换行符

css - 无法突出显示当前菜单项/页面 wordpress

javascript - 隐藏产品描述页面中的第一个缩略图

wordpress - WC_Product_Query 不适用于 have_posts()

php - 相关SEO和PHP框架

php - 如何在mysql中像with case一样使用sql?

PHP 不从 Android Post 值返回任何内容

php - Symfony 表单测试成员函数 create() on null

wordpress - 反向代理 heroku 应用程序到 WordPress 博客

php - 在 Woocommerce 中一次只允许购物车中的一个产品类别