根据类型将自定义文本标签添加到产品价格中

Dor*_*ora 2 php wordpress product woocommerce price

我有一个我无法解决的小问题.我有这个WooCommerce网站,其中包含可变产品,目前价格以这种方式显示:

每打$ 5.50 - 每打$ 100.00

我使用这个CSS规则,在每个价格之后添加"每打",但在当前场景中没有意义.

.price .amount:after {
content: " per dozen";
}
Run Code Online (Sandbox Code Playgroud)

我想用这种方式显示这个变量产品的价格:

每打 $ 5.50 - 每箱100.00 美元(数量)

在此先感谢您的帮助.

Loi*_*tec 5

在这里,您可以使用挂钩的自定义函数woocommerce_price_htmlwoocommerce_variation_price_html过滤器钩子(对于简单和变量产品)添加自定义标签.

对于变量产品的最小/最大价格,我们需要一个挂在woocommerce_variation_sale_price_html过滤器钩子中的独立函数.

更新:我的代码现在也将在单个产品上处理"每打",你必须删除自定义CSS规则.price .amount:after { content: " per dozen";}.

这将避免在任何地方重复"每打".

但是,根据所选属性值,无法在实时价格上设置不同的标签.为此,唯一的方法是使用Javascript/jQuery,因为这是客户端的实时事件......

Update2
以下是工作和测试的代码(参见最后的屏幕截图):

add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2 );
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2 );
function prices_custom_labels( $price, $product ){

    // Custom label name
    $per_dozen = ' '. __('per dozen', 'woocommerce' );

    // Set HERE your "quantity" attribute slug
    $attribute_qty_slug = 'pa_quantity';

    $attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug;
    $append_label = '';

    // 1) Variable products
    if ($product->product_type != 'simple' && $product->variation_id ) {

        // Getting the attribute "quantity" value
        $attribute_qty_is_set = $product->variation_data[$attribute_qty_slug_key];

        // if "quantity" not set we display " per dozen"
        if( ! $attribute_qty_is_set )
            $append_label = $per_dozen;


        // Outputed price + custom label
        $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).$append_label.'</ins>';
    }
    // 2) Simple products
    else
    {
        // Here the output price + custom default label
        $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).$per_dozen.'</ins>';
    }
    return $price;
}

add_filter('woocommerce_variable_price_html', 'prices_custom_labels_min_max', 20, 2);
function prices_custom_labels_min_max( $price, $product) {

    // Custom label name
    $per_dozen = ' '. __('per dozen', 'woocommerce' );
    $per_case = ' '. __('per case', 'woocommerce' );

    // Set HERE your quantity attribute slug
    $attribute_qty_slug = 'pa_quantity';


    // Getting the min and max variations prices
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_max_reg_price = $product->get_variation_regular_price('max', true);
    $variation_reg_price = $product->get_variation_regular_price();


    if( $variation_min_reg_price == $variation_max_reg_price )
    {
        $price = '<ins class="highlight">'.woocommerce_price($variation_reg_price) . $per_dozen . '</ins>';
    }
    else
    {
        if( !in_array( $attribute_qty_slug, array_keys( $product->get_attributes() ) ) )
        {
            $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_dozen . '</ins>';
        }
        else
        {
            $price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . '</ins>';
        }
    }
    return $price;
}
Run Code Online (Sandbox Code Playgroud)

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.


这是我的测试服务器的真实截图:

在此输入图像描述

此代码经过测试且确实有效.


相关答案: