🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۵ علی صالحی
فیلتر کردن براساس قیمت و دسته بندی
جامعه وردپرس (برنامه نویسی) ایجاد شده در ۱۱ بهمن ۱۴۰۰

سلام استاد وقت به خیر. اگه بخوایم قابلیت فیلتر کردن محصولات براساس دسته بندی و قیمت ایجاد کنیم.این رو چطور میتونیم مدیریت کنیم.ممنون میشم راهنمایی بفرمائید.

سلام برای نویشا فیلتر محصولا بر اساس قیمت میتونی از شورتکد

woocommerce_product_filter_price

استفاده کنی ،تنطیمات هم داره که نحوه نمایش رو تعیین می‌کنه، برای جستجو بر اساس دسته بندی هم به نظرم از همون WP Query می‌تونید استفاده کنید با این شرط که پست تایپ حتما product باشه.

کیوان علی محمدی ۱۸ بهمن ۱۴۰۰، ۱۷:۴۲

استاد من فیلتر رو اعمال کردم به درستی کار میکنه فقط مشکلم اینه که دو بار باید صفحه فروشگاه reload بشه تا term id تغییر کنه و اینکه من اینو داخل فایل loop start گذاشتم در صورتی که در فایلی که option رو ذخیره کردم برای بار اول اجرا میشه.

5115-filter.jpg

فایل ذخیره سازی option :

<?php
add_action('filter_product_category','get_filter_product_category');
if(isset($_POST['savefilter'])){
  do_action('filter_product_category');
}
$product_cat = get_option('filter-category',[]);
include 'filterproduct.php';
function get_filter_product_category(){
  get_option('filter-category',[]);
  $filter_product = [
    'cat_pr' => sanitize_text_field($_POST['catpr'])
  ];
  update_option('filter-category',$filter_product);

فایل filterproduct.php :

<div class="col-md-4">
<form action="" method="post" class="form-group bg-white p-2">
    <h5 class="text-center pt-2">دسته محصولاتh5>
    <hr>
    <label for="">دسته بندیlabel>
    <select name="catpr" id="">
    <?php $categories= get_categories(array('hide_empty' => 0,'taxonomy' => 'product_cat')) ;?>
    <?php foreach($categories as $cat) :?> 
    <option type="input"  class="form-control"  value="<?php echo $cat->cat_ID ;?>" <?php      selected(isset($product_cat['cat_pr']) && $product_cat['cat_pr'] == $cat->cat_ID )  ;?>><?php echo $cat->cat_name ;?>option>
    <?php endforeach ;?>
    select>

  <button type="submit" name="savefilter">اعمال فیلترbutton>
form>

  فایل loop-start.php :

<?php
/**
 * Product Loop Start
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/loop-start.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce\\Templates
 * @version     3.3.0
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<?php 
$pr_cat = get_cat_filter_product('cat_pr');
if(isset($pr_cat)){
    global $wp_query;
    $args = array(
        'post_type'      => 'product',
        'tax_query'      =>  array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'tag_ID',
            'terms'    => $pr_cat
         ),
        ),
    );  
  query_posts($args);
}
;?>
<div class="col-md-8 <?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">
<div class="row">

 ممنون میشم راهنمایی بفرمایید

علی صالحی ۱۹ بهمن ۱۴۰۰، ۱۴:۴۹

اینکه صفحه دوبار باید رفرش بشه تا term_id تغییر کنه رو متوجه نشدم.

کیوان علی محمدی ۲۲ بهمن ۱۴۰۰، ۰۹:۳۲

اون دکمه اعمال فیلتر میزنم باید محصولات اون دسته بندی تغییر کنه اما زمانی این اتفاق میوفته که یکبار صفحه رو رفرش کنم

علی صالحی ۲۲ بهمن ۱۴۰۰، ۱۰:۰۹

سلام دوست عزیز

با زدن دکمه اعمال فیلتر صفحه باید رفرش بشه چون دکمه submit هست. آیا این اتفاق نمی‌افته و شما خودتون صفحه رو رفرش می‌کنید؟

و اینکه فکر می‌کنم تگ‌های HTML شما یکم مشکل داره به طور مثال تگ form بسته نشده

تگ‌های HTML رو چک کنید

مجید زادگی ۲۸ بهمن ۱۴۰۰، ۱۲:۰۳