基于Woocommerce中产品类别的自定义产品价格后缀 [英] Custom product price suffix on based on product categories in Woocommerce

查看:106
本文介绍了基于Woocommerce中产品类别的自定义产品价格后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在大多数在线目录的价格上添加每米,我在


I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?

I have edited the code as such:

/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('fabric','haberdashery', 'lining',);

    if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
        $price .= ' ' . __('per metre');

    return $price;
}

I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.

I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.

It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.

解决方案

There is a little mistake in your code in the has_term() function.

To handle parent product categories, we will use a custom conditional function instead of has_tem().

I have also added some code to handle the product variation selected price of variable products, So try this instead:

// Custom conditional function that checks for parent product categories
function has_product_categories( $categories, $product_id ) {
     // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy        = 'product_cat';

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
            $categories_ids[] = (int) $category;
        } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
            $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
function conditional_price_suffix( $price, $product ) {
    // Handling product variations
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('fabric','haberdashery', 'lining');

    if( has_product_categories( $product_categories, $product_id ) )
        $price .= ' ' . __('per metre');

    return $price;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

这篇关于基于Woocommerce中产品类别的自定义产品价格后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆