按属性和有库存的产品显示相关产品
Show related products by attribute and products that have stock
根据属性显示相关产品,效果很好。
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){
## --- YOUR SETTINGS --- ##
$attribute = "Color"; // <== HERE define your attribute name
$limit = "3"; // <== Number of products to be displayed
$cols = "3"; // <== 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>';
}
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){
## --- YOUR SETTINGS --- ##
$attribute = "Color"; // <== HERE define your attribute name
$limit = "3"; // <== Number of products to be displayed
$cols = "3"; // <== 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>';
}
很遗憾,缺货的产品也会显示。
我试着添加这个
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
//===========here===========//
JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'
//===========here===========//
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'" );
但是没有用。
有谁知道如何让显示的产品都是有货的产品吗?
非常感谢任何帮助,
谢谢
您的起点是正确的,您确实需要查看 postmeta 中的 _stock_status table 但是您有一点错误,因为您没有链接postmeta table 并且你在
行中有错字
JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'
所以这一行加入postmetatable使用
JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id
然后在你查看库存状态的地方
AND pm.meta_key = '_stock_status' AND meta_value = 'instock'"
尝试
$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
JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'
AND pm.meta_key = '_stock_status' AND meta_value = 'instock'" );
我已经测试过了,它对我有用。
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){
## --- YOUR SETTINGS --- ##
$attribute = "Color"; // <== HERE define your attribute name
$limit = "3"; // <== Number of products to be displayed
$cols = "3"; // <== 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>';
}
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){
## --- YOUR SETTINGS --- ##
$attribute = "Color"; // <== HERE define your attribute name
$limit = "3"; // <== Number of products to be displayed
$cols = "3"; // <== 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>';
}
很遗憾,缺货的产品也会显示。
我试着添加这个
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
//===========here===========//
JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'
//===========here===========//
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'" );
但是没有用。
有谁知道如何让显示的产品都是有货的产品吗?
非常感谢任何帮助,
谢谢
您的起点是正确的,您确实需要查看 postmeta 中的 _stock_status table 但是您有一点错误,因为您没有链接postmeta table 并且你在
行中有错字JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'
所以这一行加入postmetatable使用
JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id
然后在你查看库存状态的地方
AND pm.meta_key = '_stock_status' AND meta_value = 'instock'"
尝试
$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
JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = tr.object_id
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'
AND pm.meta_key = '_stock_status' AND meta_value = 'instock'" );
我已经测试过了,它对我有用。