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

标签 php wordpress woocommerce product price

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

每打 5.50 美元 – 每打 100.00 美元

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

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

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

5.50 美元每打 – 100.00 美元每箱(数量)

在此先感谢您的帮助。

最佳答案

在这里,您将能够使用挂接到 woocommerce_price_htmlwoocommerce_variation_price_html 中的自定义函数添加自定义标签> 过滤器 Hook (用于简单和可变产品。

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

Update: AS my code will now will handle also " per dozen" on single products too, you have to remove your custom CSS rule .price .amount:after { content: " per dozen";}.

这将避免到处出现重复的“每打”。

But it's not possible to set a different label on the live price based on the selected attributes values. For that the only way is using Javascript/jQuery, as this is a live event on client side...

更新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 文件中,或者也位于任何插件文件中


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

enter image description here

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


相关回答:

关于php - 根据产品类型向产品价格添加自定义文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322098/

相关文章:

php - 将 URL 引荐来源网址(来源)限制为根 URL 或同一页面

php - 随机选择数组中的元素然后从数组中删除

php - Laravel - 以 .php 结尾的重定向捕获/重定向 Urls

wordpress - Git 子模块更新过程在尝试将 Azure DevOps 连接到 siteground 服务器时需要密码

javascript - 如何将 WooCommerce Frontend JS 包含到自定义主题中 - 最佳实践

css - 类别元素不会显示在一行中

php - Laravel:语法错误或访问冲突导致我也像字段一样使用 'order'

wordpress - Wordpress TinyMCE 的自定义样式

jquery - 使用jquery的安全方法是什么没有冲突

wordpress - Ionic 3 和 Woocommerce - 使用 woocommerce 客户 api 检索/创建客户?