【问题标题】:Adding custom text labels to the product prices depending on their type根据产品类型向产品价格添加自定义文本标签
【发布时间】:2017-02-19 00:03:26
【问题描述】:

我有一个小问题我还不能解决。 我有这个带有可变产品的WooCommerce web site,目前价格以这种方式显示:

每打 5.50 美元 – 每打 100.00 美元

我使用这个在每个价格后添加“每打”的 CSS 规则,但这在当前场景中没有意义。

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

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

$5.50 每打 – $100.00 每箱(数量)

提前感谢您的帮助。

【问题讨论】:

  • 会发生什么,您希望发生什么?
  • 您检查过我问题中的链接吗?目前显示每打 5.50 美元 – 每打 100.00 美元,并希望显示每打 5.50 美元 – 每箱 100.00 美元 了解、单件价格和批量价格。
  • 我无法查看外部链接。您需要提供minimal reproducible example
  • 查看此图片:i.imgur.com/KKrtpvj.png?1 您会看到目前显示每打 5.50 美元 – 每打 100.00 美元,而我只想显示每打 5.50 美元 – 每箱 100.00 美元。就是这样。
  • 更改 CSS 类,或创建一个新的并仅在此处添加。

标签: php wordpress woocommerce product price


【解决方案1】:

在这里,您将能够使用挂钩在 woocommerce_price_htmlwoocommerce_variation_price_html 过滤器挂钩中的自定义函数添加自定义标签(为了简单和变量产品。

对于变量产品中的最低/最高价格,我们需要一个单独的函数挂钩在 woocommerce_variation_sale_price_html 过滤钩子中。

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

这将避免到处都有重复的“每打”。

但不可能根据所选属性值在实时价格上设置不同的标签。为此,唯一的方法是使用 Javascript/jQuery,因为这样是客户端的现场活动...

更新2
这是工作和测试的代码(见最后的截图)

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 文件或任何插件文件中


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

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


相关答案:

【讨论】:

  • 你这么多@loictheaztec。但是我网站上的代码有问题。查看图片:i.imgur.com/Mk3FRZ7.png?1
  • @Dora 你是否删除了自定义相关的 CSS 规则?
  • 啊哈,让我删除我的自定义 CSS 规则。
  • 您的解决方案有效,但仅适用于单一产品。在展会中搜索时,例如:wholesaleunderwear.co/shop/page/5 在每个产品中显示价格“每打 0”。如何解决这个问题,显示单一价格,如果没有在产品中设置批量价格?
【解决方案2】:

此代码解决了一个问题:

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

// Set HERE your custom labels names
$per_dozen = ' '. __('per dozen', 'woocommerce' );
$per_case = ' '. __('per case', 'woocommerce' );

// 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);

if( $variation_min_reg_price == $variation_max_reg_price ){
    $price = '<ins   class="highlight">'.woocommerce_price($variation_min_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;
}

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多