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

朵拉

我有一个尚无法解决的小问题。我有一个带有可变产品的WooCommerce网站,当前价格以这种方式显示:

每打$ 5.50 –每打$ 100.00

我使用此CSS规则,在每个价格之后加上“每打”,但在当前情况下没有意义。

.price .amount:after {
content: " per dozen";
}

我想以此方式显示此可变产品的价格:

每打$ 5.50 每案$ 100.00 (数量)

在此先感谢您的帮助。

LoicTheAztec

在这里,您将能够使用挂钩的自定义函数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;
}

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


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

在此处输入图片说明

此代码已经过测试,并且确实有效。


相关答案:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

产品类别的WooCommerce产品价格中的自定义小数

为产品价格添加自定义图标

显示WooCommerce产品价格的自定义简码:以零价格显示文本

在WooCommerce中将自定义标签添加到特定产品类型的单个产品页面

产品销售时在价格中添加自定义文本标签

从自定义产品类型创建产品

在WooCommerce 4中以编程方式从自定义产品类型添加新产品

如何在WooCommerce中为自定义产品类型启用价格和库存

使用ACF编号字段作为WooCommerce自定义产品类型的价格

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”

根据产品类型,WooCommerce的“添加到购物车”按钮旁边的“自定义”按钮

基于WooCommerce中产品的自定义产品价格后缀

在Woocommerce单个产品页面中显示高于产品价格的自定义字段

根据产品类别自定义购物车总计和结帐总计文本

根据Woocommerce中的产品类别添加自定义结帐字段

启用向后价格后缀到在WooCommerce中显示自定义产品价格的功能

附加的自定义按钮,可在添加到Wovcommerce中的购物车之前更改产品价格

WooCommerce自定义产品类型-多个添加到购物车部分的问题

如何在 Woocommerce 的自定义产品类型中添加变体选项卡?

在自定义模板中显示WooCommerce产品价格(带折扣)

通过自定义字段在Woocommerce中更改购物车中的产品价格

如何在自定义页面上按ID号显示Woocommerce产品价格?

自定义 Woocommerce 可变产品价格范围,用于多个活动变体

将自定义产品价格乘以WooCommerce购物车中的数量

根据产品类型定义递归函数

如何在以下Woocommerce缺货产品文本中添加单个产品上的Woocommerce产品价格

将自定义字段价格作为产品价格分配给WooCommerce中的特定用户角色

根据woocommerce中的产品价格显示%

根据客户代码获取产品价格