WooCommerce相关产品(按属性)

时间:2018-02-15 作者:Dan

我想显示按类别或属性(不是标签)相关的产品

我已修改了此处发布的解决方案here 根据我的需要:

function custom_related_product_args ( $args ){
    global $product;

    $cats          = wc_get_product_terms( $product->id, \'product_cat\', array( \'fields\' => \'slugs\' ) );
    $brands        = wc_get_product_terms( $product->id, \'pa_brand\', array( \'fields\' => \'slugs\' ) );
    $collections   = wc_get_product_terms( $product->id, \'pa_collection\', array( \'fields\' => \'slugs\' ) );    

    unset( $args[\'post__in\'] );
    $args[\'tax_query\'] = array( 
        \'relation\' => \'OR\',
        array(
            \'taxonomy\' => \'product_cat\',
            \'field\'    => \'slug\',
            \'terms\'    => $cats,
        ),
        array(
            \'relation\' => \'OR\',
            array(
                \'taxonomy\' => \'pa_brand\',
                \'field\'    => \'slug\',
                \'terms\'    => $brands,
            ),
            array(
                \'taxonomy\' => \'pa_collection\',
                \'field\'    => \'slug\',
                \'terms\'    => $collections,
            )
        )
    );

    return $args;
}
add_filter(\'woocommerce_related_products_args\', \'custom_related_product_args\');
然而,这似乎对我不起作用,我也不知道我做错了什么?

2 个回复
SO网友:Paris Koutroumanos

禁用默认的相关产品并尝试此操作(向functions.php添加自定义代码)

add_action( \'woocommerce_after_single_product_summary\', \'custom_output_product_collection\', 12 );
function custom_output_product_collection(){

    ## --- YOUR SETTINGS --- ##

    $attribute = "attribute_slug"; // <== HERE define your attribute slug
    $limit     = "5";     // <== Number of products to be displayed
    $cols      = "5";     // <== Number of columns
    $orderby   = "rand";  // <== Order by argument (random order here)

    ## --- THE CODE --- ##

    global $post, $wpdb;

    // Formatting the attribute
    $attribute = sanitize_title( $attribute );
    $taxonomy  = \'pa_\' . $attribute;

    // Get the WP_Term object for the current product and the defined product attribute
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    $term = reset($terms);

    // Get all product IDs that have  the same product attribute value (except current product ID)
    $product_ids = $wpdb->get_col( "SELECT DISTINCT tr.object_id
        FROM {$wpdb->prefix}term_relationships as tr
        JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
        JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE tt.taxonomy LIKE \'$taxonomy\' AND t.term_id = \'{$term->term_id}\' AND tr.object_id != \'{$post->ID}\'" );

    // Convert array values to a coma separated string
    $ids = implode( \',\', $product_ids );

    ## --- THE OUTPUT --- ##

    echo \'<section class="\'.$attribute.\' \'.$attribute.\'-\'.$term->slug.\' products">
        <h2>\'.__( "Collection", "woocommerce" ).\': \'.$term->name.\'</h2>\';

    echo do_shortcode("[products ids=\'$ids\' columns=\'$cols\' limit=\'$limit\' orderby=\'$orderby\']");

    echo \'</section>\';
}

SO网友:Dharmishtha Patel
global $product;

$cats          = wc_get_product_terms( $product->id, \'product_cat\', array( \'fields\' => \'slugs\' ) );
$brands        = wc_get_product_terms( $product->id, \'pa_brand\', array( \'fields\' => \'slugs\' ) );
$artists       = wc_get_product_terms( $product->id, \'pa_artist\', array( \'fields\' => \'slugs\' ) );    
$manufacturers = wc_get_product_terms( $product->id, \'pa_manufacturer\', array( \'fields\' => \'slugs\' ) );

unset( $args[\'post__in\'] );
$args[\'tax_query\'] = array( 
    \'relation\' => \'AND\',
    array(
        \'taxonomy\' => \'product_cat\',
        \'field\'    => \'slug\',
        \'terms\'    => $cats,
    ),
    array(
        \'relation\' => \'OR\',
        array(
            \'taxonomy\' => \'pa_brand\',
            \'field\'    => \'slug\',
            \'terms\'    => $brands,
        ),
        array(
            \'taxonomy\' => \'pa_artist\',
            \'field\'    => \'slug\',
            \'terms\'    => $artists,
        ),
        array(
            \'taxonomy\' => \'pa_manufacturer\',
            \'field\'    => \'slug\',
            \'terms\'    => $manufacturers,
        )
    )
);

return $args;
结束

相关推荐

如果用户已登录并在主页上重定向,则使用PHP

我是一个彻头彻尾的傻瓜,但我正在努力学习,我被难住了。我有一个登录页面作为我的静态主页,我想做一个if语句,这样如果用户登录并转到静态主页,他们就会被重定向到另一个包含内容的页面。到目前为止,这是我所拥有的,但它不起作用:if ( is_user_logged_in() && is_page( home_url ) ) { wp_redirect(\'http://example.com/\') ; } 你能不能不仅给我一个答案,而且给我一个小小的解释,让