【问题标题】:Custom product price suffix on based on product categories in Woocommerce根据 Woocommerce 中的产品类别自定义产品价格后缀
【发布时间】:2019-04-25 09:25:15
【问题描述】:

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

我已经这样编辑了代码:

/*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;
}

我希望“fabrics”、“haberdashery”、“lining”以每米显示,并且“fasteners”、“patches”、“remnants”不显示后缀。

我已经尝试了代码的变体——我在顶部的排除项和第二部分的包含项,以及带有/不带有“(!有术语”部分,但无论我做什么都会带走所有后缀消息,或者适用于所有类别。

如果我能像以前使用一个非常臃肿的插件一样让它工作,那就太棒了。我基本上只有这方面的能力,所以请随时与我交谈,就好像我是个白痴一样。

【问题讨论】:

  • 我的答案下方有一个评论区,您可以在其中添加与我的答案相关的评论,我会收到通知(而不是在答案中发表评论)。 我的代码已经过测试并且运行良好。 请注意,has_term() 函数不适用于父产品类别或子子类别。您需要指定相关产品中设置的产品类别。现在 20 是钩子优先级,2 是函数中的参数(变量)个数,没有任何问题。
  • 这对我不起作用,我怀疑它与父/子类别有关,如果它对它们不起作用,它对它们起作用是什么?我的父类别是“Fabric”、“Haberdashery”,我拥有的所有其他类别都在它们之下——例如“jersey”(每米销售)和“buttons”(单独销售)。
  • 如果has_term ( ) 功能不适用于我的类别,是否还有其他功能可以使用?
  • 首先,作为新用户,您应该[快速浏览(30 秒)](stackoverflow.com/tour) 以更好地了解 StackOverFlow 的基本工作原理。现在,正如我已经告诉您的那样,您应该在评论区的我的答案下方添加这些 cmets。我已经更新了答案,它现在应该适用于您的父类别。如果这个答案是回答你的问题,请don't forget to accept the answer,谢谢。

标签: php wordpress woocommerce custom-taxonomy price


【解决方案1】:

has_term() 函数中的代码有一点错误。

为了处理父产品类别,我们将使用自定义条件函数而不是has_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 文件中。经过测试并且有效。

【讨论】:

  • 这是一个很好的解决方案,谢谢。您将如何在购物车中显示此内容?
猜你喜欢
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2021-07-22
  • 2018-07-18
  • 2017-11-13
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多