我希望删除购物车和 checkout 页面中的发货标签值时,将该值分配给发货.例:-将"统一税率100.00美元"改为"100.00美元".这意味着我只需要不带标签文本的值.你能帮我做到这一点吗?

我在shipping_package_rates_filter_callback函数中更改为$rates[$targeted_rate_id]->label = '';.但它仍然在值的开头显示':'(冒号符号).例:-:100.00$.

shipping_package_rates_filter_callback

add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 );
function shipping_package_rates_filter_callback( $rates, $package ) {
    // HERE define your targeted rate id
    $targeted_rate_id = 'flat_rate:4'; 
    
    if ( ! array_key_exists($targeted_rate_id, $rates) ) 
        return $rates;
    
    $new_cost = WC()->session->get('shipping_cost'); // Get new cost from WC sessions

    if ( ! empty($new_cost) ) {
        // Set shipping label empty
        $rates[$targeted_rate_id]->label = '';
        $rate = $rates[$targeted_rate_id];
        $cost = $rate->cost; // Set the initial cost in a variable for taxes calculations
        $rates[$targeted_rate_id]->cost = $new_cost; // Set the new cost

        $has_taxes = false; // Initializing
        $taxes     = array(); // Initializing
        foreach ($rate->taxes as $key => $tax_cost){
            if( $tax_cost > 0 ) {
                $tax_rate    = $tax_cost / $cost; // Get the tax rate conversion
                $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
                $has_taxes   = true; // There are taxes (set it to true)
            }
        }
        if( $has_taxes ) {
            $rates[$targeted_rate_id]->taxes = $taxes; // Set taxes costs array
        }
    } else {
        unset($rates[$targeted_rate_id]);
    }
    return $rates;
}

推荐答案

要从显示的发货方式中删除冒号": ",请使用以下代码:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'display_only_shipping_method_price', 10, 2 );
function display_only_shipping_method_price( $label, $method ) {
    // HERE define your targeted rate id
    $targeted_rate_id = 'flat_rate:4';

    if ( $method->id === $targeted_rate_id && 0 < $method->cost ) {
        $label = str_replace( $method->label . ': ', '', $label);
    }
    return $label;
}

相关:

Php相关问答推荐

尽管包含目标日期,但日期范围比较仍有意外输出

dockerized laravel服务器拒绝POST请求

WooCommerce购物车中仅允许一个可下载产品

LaravelEloquent 地根据表中的值提取记录

将日期数组排序为年->;月

从AJAX响应中获取一个未定义的html

Laravel Carbon DiffForHumans选项(年,月,天)

是否有条件判断是否发布了另一个Wordpress页面?

Regex|PHP捕获json字符串中的每个非法双引号

如何在其他数组php中创建具有关联数组的数组

用户注册表单中的 HTTP 错误 405

在 Woocommerce 邮箱订单中显示产品 GTIN

如何使用 AJAX、PHP 和 MYSQL 在 Select 项目时在文本框中填充数据

wp_insert_post() 和 WordPress 中查询字符串的多个帖子

将自定义保存金额移至 Woocommerce 简单产品中的价格以下

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

如何搜索和替换 XML 文件中的一段文本?

PHP Symfony 在测试.env文件中将接口自动装配作为构造函数参数,但使用接口的特定类

如何将数字文本文件添加到 PHP 数学方程式中?

构建 [Controller] 时目标 [Interface] 不可实例化