下面的代碼根據屬性顯示相關產品并且作業正常。
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 hljs-variable">$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 hljs-variable">$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}'" );
但它沒有用。
有誰知道如何使顯示的產品是只有可用庫存的產品?
非常感謝任何幫助,
謝謝
uj5u.com熱心網友回復:
您從正確的軌道開始,您確實需要查看 postmeta 表中的 _stock_status 但是您有一點錯誤,因為您沒有在 postmeta 表中鏈接并且您在該行中有錯字
JOIN {$wpdb->postmeta} terms as as ON pm.meta_key = '_stock_status' AND meta_value = 'instock'
所以這條線加入到 postmeta 表中使用
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'" );
我已經對此進行了測驗,它對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419700.html
標籤:
