افزایش گروهی قیمت براساس دسته بندی در ووکامرس بدون افزونه
با توجه به افزایش یا کاهش قیمت ها، فروشگاه های اینترنتی هم باید به صورت روزانه و یا لحظه ای تغییرات افزایش یا کاهش قیمت ها را اعمال کنند اما به عنوان مثال مدیر یک فروشگاه اینترنتی با داشتن صد ها بلکه هزاران محصول نمی توانید هر لحظه نسبت به افزایش قیمت ها اقدام کند زیرا هنگامی که قیمت یک محصول بر اساس نرخ دلار تعیین شود سخت خواهد بود تا قیمت ها هرلحظه به صورت تک به تک در سایت اعمال شود، به همین دلیل است که نیاز خواهید داشت تا به وسیله ابزار هایی قیمت محصولات خود را کاهش و یا افزایش دهید.
روش های کاهش یا افزایش قیمت گروهی محصولات
روش اول : استفاده از افزونه
یکی از روش های افزایش و یا کاهش قیمت محصولات استفاده از افزونه است، افزونه های زیادی مانند WooCommerce Bulk Price Edit وجود دارند که به لیست قیمت محصولات را به مدیر سایت نمایش خواهند داد و مدیر سایت نیز می توانید به صورت گروهی قیمت ها را افزایش یا کاهش دهد.
روش دوم : استفاده از کد در فایل Functions.php (پیشنهادی)
در این روش با استفاده از تکه کد زیر و تزریق آن در فایل Functions.php می توانید قیمت محصولات سایت بر اساس دسته بندی آنها افزایش و یا کاهش دهید.
کد محصولات ساده :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
// Add a submenu under WooCommerce add_action('admin_menu', 'add_bulk_price_update_submenu'); function add_bulk_price_update_submenu() { add_submenu_page( 'woocommerce', // Parent slug 'تغییر قیمت دستهای', // Page title 'تغییر قیمت دستهای', // Menu title 'manage_woocommerce', // Capability 'bulk-price-update', // Menu slug 'bulk_price_update_page'// Function ); } // Display the bulk price update page function bulk_price_update_page() { if (!current_user_can('manage_woocommerce')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (isset($_POST['submit'])) { $category_id = intval($_POST['category_id']); $price_change_percentage = floatval($_POST['price_change_percentage']); $change_type = sanitize_text_field($_POST['change_type']); update_prices_in_category($category_id, $price_change_percentage, $change_type); echo '<div class="updated"><p>قیمتها با موفقیت بهروزرسانی شدند.</p></div>'; } ?> <div class="wrap"> <h1>تغییر قیمت دستهای</h1> <form method="post"> <table class="form-table"> <tr valign="top"> <th scope="row">دستهبندی محصولات:</th> <td> <?php $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, ); $product_categories = get_terms($args); ?> <select name="category_id" required> <option value="">یک دستهبندی انتخاب کنید</option> <?php foreach ($product_categories as $category) { echo '<option value="' . $category->term_id . '">' . $category->name . '</option>'; } ?> </select> </td> </tr> <tr valign="top"> <th scope="row">درصد تغییر قیمت:</th> <td> <input type="number" step="0.01" name="price_change_percentage" required /> % </td> </tr> <tr valign="top"> <th scope="row">نوع تغییر قیمت:</th> <td> <label><input type="radio" name="change_type" value="increase" required> افزایش</label> <label><input type="radio" name="change_type" value="decrease" required> کاهش</label> </td> </tr> </table> <?php submit_button('اعمال تغییرات'); ?> </form> </div> <?php } // Function to update prices in a specific category function update_prices_in_category($category_id, $price_change_percentage, $change_type) { $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $category_id, ), ), ); $products = get_posts($args); foreach ($products as $product) { $product_id = $product->ID; $product_obj = wc_get_product($product_id); $regular_price = $product_obj->get_regular_price(); if ($change_type == 'increase') { $new_price = $regular_price + ($regular_price * ($price_change_percentage / 100)); } else { $new_price = $regular_price - ($regular_price * ($price_change_percentage / 100)); } $product_obj->set_regular_price($new_price); $product_obj->save(); } } |
کد محصولات متغییر و ساده :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
// Add a submenu under WooCommerce add_action('admin_menu', 'add_bulk_price_update_submenu'); function add_bulk_price_update_submenu() { add_submenu_page( 'woocommerce', // Parent slug 'تغییر قیمت دستهای', // Page title 'تغییر قیمت دستهای', // Menu title 'manage_woocommerce', // Capability 'bulk-price-update', // Menu slug 'bulk_price_update_page'// Function ); } // Display the bulk price update page function bulk_price_update_page() { if (!current_user_can('manage_woocommerce')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (isset($_POST['submit'])) { $category_id = intval($_POST['category_id']); $price_change_percentage = floatval($_POST['price_change_percentage']); $change_type = sanitize_text_field($_POST['change_type']); update_prices_in_category($category_id, $price_change_percentage, $change_type); echo '<div class="updated"><p>قیمتها با موفقیت بهروزرسانی شدند.</p></div>'; } ?> <div class="wrap"> <h1>تغییر قیمت دستهای</h1> <form method="post"> <table class="form-table"> <tr valign="top"> <th scope="row">دستهبندی محصولات:</th> <td> <?php $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, ); $product_categories = get_terms($args); ?> <select name="category_id" required> <option value="">یک دستهبندی انتخاب کنید</option> <?php foreach ($product_categories as $category) { echo '<option value="' . $category->term_id . '">' . $category->name . '</option>'; } ?> </select> </td> </tr> <tr valign="top"> <th scope="row">درصد تغییر قیمت:</th> <td> <input type="number" step="0.01" name="price_change_percentage" required /> % </td> </tr> <tr valign="top"> <th scope="row">نوع تغییر قیمت:</th> <td> <label><input type="radio" name="change_type" value="increase" required> افزایش</label> <label><input type="radio" name="change_type" value="decrease" required> کاهش</label> </td> </tr> </table> <?php submit_button('اعمال تغییرات'); ?> </form> </div> <?php } // Function to update prices in a specific category for both simple and variable products function update_prices_in_category($category_id, $price_change_percentage, $change_type) { $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $category_id, ), ), ); $products = get_posts($args); foreach ($products as $product) { $product_id = $product->ID; $product_obj = wc_get_product($product_id); // Handle simple products if ($product_obj->is_type('simple')) { $regular_price = $product_obj->get_regular_price(); if ($change_type == 'increase') { $new_price = $regular_price + ($regular_price * ($price_change_percentage / 100)); } else { $new_price = $regular_price - ($regular_price * ($price_change_percentage / 100)); } $product_obj->set_regular_price($new_price); $product_obj->save(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Handle variable products if ($product_obj->is_type('variable')) { $variations = $product_obj->get_children(); foreach ($variations as $variation_id) { $variation = wc_get_product($variation_id); $regular_price = $variation->get_regular_price(); if ($change_type == 'increase') { $new_price = $regular_price + ($regular_price * ($price_change_percentage / 100)); } else { $new_price = $regular_price - ($regular_price * ($price_change_percentage / 100)); } $variation->set_regular_price($new_price); $variation->save(); } } } } |
جمع بندی
در این آموزش به دو روش برای اضافه و یا کاهش دادن قیمت محصولات در وردپرس پرداخته ایم و در بخش آموزش ویدیویی نیز به صورت کامل نحوه تزریق کد به فایل Function.php را آموزش دادیم، امیدواریم این آموزش مورد توجه شما قرار گرفته باشد و توانسته باشید نسبط به افزایش و یا کاهش قیمت گروهی محصولات در وردپرس اقدام کرده باشید.