Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于Woocommerce中产品类别的自定义产品价格后缀_Php_Wordpress_Woocommerce_Custom Taxonomy_Price - Fatal编程技术网

Php 基于Woocommerce中产品类别的自定义产品价格后缀

Php 基于Woocommerce中产品类别的自定义产品价格后缀,php,wordpress,woocommerce,custom-taxonomy,price,Php,Wordpress,Woocommerce,Custom Taxonomy,Price,我需要将“每米”添加到大多数在线目录的价格中,我在finctions.php中尝试了该代码,但我无法让它省略/包括特定类别-它似乎是全部或全部。我做错了什么 我已将代码编辑为: /*add 'per metre' after selected items*/ add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 ); function conditional_price_suffix( $price

我需要将“每米”添加到大多数在线目录的价格中,我在finctions.php中尝试了该代码,但我无法让它省略/包括特定类别-它似乎是全部或全部。我做错了什么

我已将代码编辑为:

/*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;
}
我希望“织物”、“服装”、“衬里”显示每米,而“紧固件”、“补丁”、“残余物”不显示后缀

我已经尝试过代码的变体-我在顶部位中的排除和在第二部分中的包含,以及带/不带“(!has term”部分,但是无论我做什么,都会删除所有后缀消息,或者应用于所有类别


如果我能像以前使用一个非常臃肿的插件那样让它工作,那将是一件令人惊讶的事情。我基本上只会做这件事,所以请随意告诉我,就像我是一个白痴一样。

你的代码在
has_term()
函数中有一个小错误

要处理父产品类别,我们将使用自定义条件函数,而不是
has\u tem()

我还添加了一些代码来处理可变产品的产品变化选择价格,因此请尝试以下操作:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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;
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好


我的答案下方有一个评论区,您可以在这里添加与我的答案相关的评论,我会收到通知(而不是在答案中发表评论)。我的代码经过测试,工作正常。请注意,
有\u term()
函数不适用于父产品类别或子产品类别。您需要指定在相关产品中设置的产品类别。现在,20是挂钩优先级,2是参数(变量)数量在功能中,它没有任何问题。这对我不起作用,我怀疑这与父/子类别有关,如果它对它们不起作用,它对它们起什么作用?我的父类别是“织物”、“服装”,我拥有的所有其他类别都在它们下面-例如“运动衫”(每米出售)和“纽扣”(单独出售)。如果
有_term()
功能对我的类别不起作用,是否还有其他功能起作用?首先,作为新用户,您应该[快速浏览(30秒)]()为了更好地理解StackOverFlow的基本工作原理。正如我告诉过你的,你应该在我的答案下面的评论区添加这些评论。我已经更新了答案,现在它应该适用于你的父类别。如果这个答案回答了你的问题,请,谢谢。这是一个很好的解决方案,谢谢。你会如何处理这些问题车里的这个怎么样?