【问题标题】:How to show products in Woocommerce Product Vendor plugin vendor page if group product is showed in shop page?如果组产品显示在商店页面中,如何在 Woocommerce 产品供应商插件供应商页面中显示产品?
【发布时间】:2021-12-12 16:56:22
【问题描述】:

我在functions.php中添加了以下代码,因为我只想在woocommerce商店页面中显示组产品。该代码有效。问题是单个产品没有显示在产品供应商插件的每个供应商页面下。

插件支持告诉我要解决这个问题,我下面的代码需要添加一个条件,以便它只在某些页面上运行 - 否则它将影响整个网站.....

很遗憾,我不知道该怎么做。

欢迎您的支持!

'''/**
* Show Only Grouped Products in Woocommerce
*/

add_filter( 'woocommerce_product_query_tax_query', 'only_grouped_products', 20, 1 );
function only_grouped_products( $tax_query ){
    $tax_query[] = array(
        'taxonomy'  => 'product_type',
        'field'     => 'name',
        'terms'     => array('grouped'),
    );
    return $tax_query;
}'''

【问题讨论】:

  • is_page()、is_page_template()、is_category()、is_tax()、is_category()等使用条件。你可以用谷歌搜索它们中的每一个,并在 wordpress 文档中阅读它们。我不确定您使用的是哪个插件,但大多数插件也有自己的条件。 Woocommerce 也有自己的条件 - docs.woocommerce.com/document/conditional-tags。仅当我们在 id 为 7 的页面上运行示例 if(is_page('7')) {$tax_query[] = array(...) } return $tax_query;
  • 另外 woocommerce_product_query 只会影响商店 - wordpress.stackexchange.com/questions/278390/…
  • 问题是供应商页面也是商店页面......插件是产品供应商,实际上是Woocommerce的扩展
  • 也许这会有所帮助? gist.github.com/bentasm1/56a56f8996b289c076f8 。添加第 13,14,15 行 if ( $vendor_id ) 应该是 if ( !$vendor_id ) { your tax_query} 。我没有这个插件可以测试
  • ...我唯一知道的是:覆盖模板文件将不再有效。输出现在通过函数使用钩子:display_vendor_logo_profile() 位于 /includes/class-wc-product-vendors-vendor-frontend.php....

标签: php wordpress woocommerce conditional-statements


【解决方案1】:

我找到了解决办法:

/**
* Show Only Grouped Products in Woocommerce
*/

add_action( 'woocommerce_product_query', 
'prefix_custom_pre_get_posts_query' );

function prefix_custom_pre_get_posts_query( $q ) {

if( is_shop() || is_page('products-portfolio') ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_type',
           'field'    => 'name',
           'terms'    => array( 'grouped'), // set product categories here
    );
    $q->set( 'tax_query', $tax_query );
}
elseif( is_product_category() ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_type',
           'field'    => 'name',
           'terms'    => array( 'grouped'), // set product categories here
    );
    $q->set( 'tax_query', $tax_query );
}
elseif( is_product_tag() ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_type',
           'field'    => 'name',
           'terms'    => array( 'grouped'), // set product categories here
    );
    $q->set( 'tax_query', $tax_query );
}
else {
}

}

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2014-05-27
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多