ahmad4u 115 ارسال شده در 16 اسفند گزارش بازنشر ارسال شده در 16 اسفند سلام دوستان درخواست کد نمایش محصولات مرتبط بر اساس دسته بندی یا نام محصول دارم دسته بندی منظورم آخرین دسته بندی که محصول تو اون وجود داره نه دسته بندی مادر نقل قول لینک به ارسال
ahmad4u 115 ارسال شده در 16 اسفند مالک گزارش بازنشر ارسال شده در 16 اسفند کد را یافتم اینجا قرار میدم تا همه دوستان استفاده کنن بر اساس آخرین دسته بندی محصولات مرتبط رو نشون میده function wc_related_products_by_last_available_depth_term( $related_posts, $product_id, $args ) { $product = wc_get_product( $product_id ); $terms = wp_get_post_terms( $product_id, 'product_cat' ); $hierarchy = array(); $cat_id = ''; // find the depth of terms foreach ( $terms as $key => $term ) { $ancestors = get_ancestors( $term->term_id, 'product_cat' ); if( $ancestors && count( $ancestors ) > 1 ) { $hierarchy[$term->term_id] = max($ancestors); }elseif( $ancestors ) { $hierarchy[$term->term_id] = $ancestors[0]; } $cat_id = $term->term_id; } // if level of depth term available replace $cat_id if( $hierarchy ){ $cat_id = max( array_keys( $hierarchy ) ); } $related_posts = get_posts( array( 'post_type' => 'product', 'post_status' => 'publish', 'fields' => 'ids', 'posts_per_page' => -1, 'exclude' => array( $product_id ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => array( $cat_id ) ) ) )); return $related_posts; } add_filter( 'woocommerce_related_products', 'wc_related_products_by_last_available_depth_term', 99, 3 ); نقل قول لینک به ارسال
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .