Created
September 20, 2021 09:21
-
-
Save rezazoom/65889b1576f262a60ba0b89b89447556 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: اقدام گروهی به تغییر شیوهی ارسال | |
| * Plugin URI: https://baransys.com/ | |
| * Description: با استفاده از این افزونه میتوان با یک کلیک شیوهی ارسال را به نوع دیگری تغییر داد. | |
| * Version: 1.0 | |
| * Requires at least: 5.2 | |
| * Requires PHP: 7.2 | |
| * Author: Reza Esmaeili | |
| * Author URI: https://rezaesmaeili.ir/ | |
| * License: GPL v2 or later | |
| * License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
| * Update URI: https://baransys.com/ | |
| */ | |
| /* | |
| * Custom bulk action in dropdown | |
| * @since 3.5.0 | |
| */ | |
| add_filter('bulk_actions-edit-shop_order', 'baran_register_bulk_action'); // edit-shop_order is the screen ID of the orders page | |
| function baran_register_bulk_action ($bulk_actions) | |
| { | |
| $bulk_actions['mark_pishtaz_shipment'] = 'Force shipment to Pishtaz Post'; // <option value="mark_awaiting_shipment">Mark awaiting shipment</option> | |
| return $bulk_actions; | |
| } | |
| add_action('admin_action_mark_pishtaz_shipment', 'baran_bulk_process_custom_status'); // admin_action_{action name} | |
| function baran_bulk_process_custom_status () | |
| { | |
| // if an array with order IDs is not presented, exit the function | |
| if (!isset($_REQUEST['post']) && !is_array($_REQUEST['post'])) | |
| return; | |
| foreach ($_REQUEST['post'] as $order_id) { | |
| $order = new WC_Order($order_id); | |
| $shippings = $order->get_shipping_methods(); // same thing than $order->get_items('shipping') | |
| foreach ($shippings as $ship) { | |
| $ship->set_method_title("پست تاپین - پیشتاز (رایگان جشنوارهای)"); | |
| $ship->set_method_id('Tapin_Pishtaz_Method'); // set an existing Shipping method rate ID | |
| $ship->set_instance_id(9); | |
| } | |
| $order->save(); | |
| } | |
| // of course using add_query_arg() is not required, I could've built my URL inline | |
| $location = add_query_arg(array( | |
| 'post_type' => 'shop_order', | |
| 'mark_pishtaz_shipment' => 1, // mark_pishtaz_shipment=1 is just the $_GET variable for notices | |
| 'changed' => count($_REQUEST['post']), // number of changed orders | |
| 'ids' => join(',', $_REQUEST['post']), | |
| 'post_status' => 'all' | |
| ), 'edit.php'); | |
| wp_redirect(admin_url($location)); | |
| exit; | |
| } | |
| /* | |
| * Notices | |
| */ | |
| add_action('admin_notices', 'baran_custom_order_status_notices'); | |
| function baran_custom_order_status_notices () | |
| { | |
| global $pagenow, $typenow; | |
| if ($typenow == 'shop_order' | |
| && $pagenow == 'edit.php' | |
| && isset($_REQUEST['mark_pishtaz_shipment']) | |
| && $_REQUEST['mark_pishtaz_shipment'] == 1 | |
| && isset($_REQUEST['changed'])) { | |
| $message = sprintf(_n('شیوه ارسال به پیشتاز تغییر نمود.', 'شیوهی ارسال %s سفارش به پیشتاز تغییر نمود.', $_REQUEST['changed']), number_format_i18n($_REQUEST['changed'])); | |
| echo "<div class=\"updated\"><p>{$message}</p></div>"; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment