Last active
September 18, 2024 08:00
-
-
Save mralaminahamed/aa44e373ad2d069febf036ba39d1c915 to your computer and use it in GitHub Desktop.
Updates the _dokan_vendor_id meta for specified WooCommerce orders where the vendor ID is not set.
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: Dokan Order Meta Updater | |
| * Plugin URI: https://gist.github.com/mralaminahamed | |
| * Description: Updates the _dokan_vendor_id meta for specified WooCommerce orders where the vendor ID is not set. | |
| * Version: 1.0.0 | |
| * Author: Al Amin Ahamed | |
| * Author URI: https://gist.github.com/mralaminahamed/aa44e373ad2d069febf036ba39d1c915 | |
| * License: GPL-2.0+ | |
| * License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
| * Text Domain: dokan-order-meta-updater | |
| * Domain Path: /languages | |
| * Requires at least: 5.0 | |
| * Requires PHP: 7.0 | |
| * | |
| * @package DokanOrderMetaUpdater | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| /** | |
| * Main plugin class for Dokan Order Meta Updater. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| class Dokan_Order_Meta_Updater { | |
| /** | |
| * Number of orders to process in each batch. | |
| * | |
| * @var int | |
| */ | |
| private $batch_size = 20; | |
| /** | |
| * Constructor. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| public function __construct() { | |
| add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); | |
| add_action( 'wp_ajax_process_order_batch', array( $this, 'process_order_batch' ) ); | |
| } | |
| /** | |
| * Adds the plugin's admin menu. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| public function add_admin_menu() { | |
| add_submenu_page( | |
| 'tools.php', | |
| 'Dokan Order Meta Updater', | |
| 'Dokan Order Updater', | |
| 'manage_options', | |
| 'dokan-order-meta-updater', | |
| array( $this, 'admin_page' ) | |
| ); | |
| } | |
| /** | |
| * Renders the admin page for the plugin. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| public function admin_page() { | |
| ?> | |
| <div class="wrap"> | |
| <h1>Dokan Order Meta Updater</h1> | |
| <p>This tool updates the _dokan_vendor_id meta for WooCommerce orders where the vendor ID is not set.</p> | |
| <form id="dokan-order-updater-form"> | |
| <label for="order_ids">Enter Order IDs (comma-separated):</label><br> | |
| <textarea id="order_ids" name="order_ids" rows="4" cols="50"></textarea><br><br> | |
| <button type="submit" class="button button-primary">Start Update Process</button> | |
| </form> | |
| <div id="progress-bar" style="display: none; margin-top: 20px;"> | |
| <div class="progress-label">Processing orders: <span id="processed-count">0</span> / <span id="total-count">0</span></div> | |
| <progress id="progress" value="0" max="100"></progress> | |
| </div> | |
| <div id="results" style="margin-top: 20px;"> | |
| <h3>Results:</h3> | |
| <p>Updated orders: <span id="updated-count">0</span></p> | |
| <p>Skipped orders: <span id="skipped-count">0</span></p> | |
| <p>Errors encountered: <span id="error-count">0</span></p> | |
| </div> | |
| <div id="error-log" style="margin-top: 20px;"> | |
| <h3>Error Log:</h3> | |
| <pre id="error-log-content"></pre> | |
| </div> | |
| </div> | |
| <?php | |
| } | |
| /** | |
| * Enqueues admin scripts and styles. | |
| * | |
| * @param string $hook The current admin page. | |
| * @since 1.0.0 | |
| */ | |
| public function enqueue_admin_scripts( $hook ) { | |
| if ( 'tools_page_dokan-order-meta-updater' !== $hook ) { | |
| return; | |
| } | |
| wp_enqueue_script( 'dokan-order-updater', plugins_url( 'dokan-order-updater.js', __FILE__ ), array( 'jquery' ), '1.0.0', true ); | |
| wp_localize_script( 'dokan-order-updater', 'dokanOrderUpdater', array( | |
| 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 'nonce' => wp_create_nonce( 'dokan_order_updater_nonce' ), | |
| ) ); | |
| } | |
| /** | |
| * Processes a batch of orders. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| public function process_order_batch() { | |
| check_ajax_referer( 'dokan_order_updater_nonce', 'nonce' ); | |
| if ( ! current_user_can( 'manage_options' ) ) { | |
| wp_send_json_error( 'Insufficient permissions' ); | |
| } | |
| $order_ids = isset( $_POST['order_ids'] ) ? array_map( 'intval', explode( ',', $_POST['order_ids'] ) ) : array(); | |
| $offset = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0; | |
| $batch = array_slice( $order_ids, $offset, $this->batch_size ); | |
| $results = array( | |
| 'updated' => 0, | |
| 'skipped' => 0, | |
| 'errors' => 0, | |
| 'error_log' => array(), | |
| ); | |
| foreach ( $batch as $order_id ) { | |
| $order = wc_get_order( $order_id ); | |
| if ( $order instanceof WC_Order ) { | |
| if ( empty( $order->get_meta( '_dokan_vendor_id' ) ) ) { | |
| $items = $order->get_items(); | |
| $first_item = reset( $items ); | |
| if ( $first_item ) { | |
| $product_id = $first_item->get_product_id(); | |
| $vendor_id = dokan_get_vendor_by_product( $product_id, true ); | |
| if ( $vendor_id ) { | |
| $order->update_meta_data( '_dokan_vendor_id', $vendor_id ); | |
| $order->save(); | |
| $results['updated']++; | |
| } else { | |
| $results['errors']++; | |
| $results['error_log'][] = "Error: Could not determine vendor for product ID $product_id in order $order_id"; | |
| } | |
| } else { | |
| $results['errors']++; | |
| $results['error_log'][] = "Error: No items found in order $order_id"; | |
| } | |
| } else { | |
| $results['skipped']++; | |
| } | |
| } else { | |
| $results['errors']++; | |
| $results['error_log'][] = "Error: Invalid or non-existent order for ID $order_id"; | |
| } | |
| } | |
| $results['processed'] = count( $batch ); | |
| $results['total'] = count( $order_ids ); | |
| $results['offset'] = $offset + $this->batch_size; | |
| $results['is_complete'] = $results['offset'] >= $results['total']; | |
| wp_send_json_success( $results ); | |
| } | |
| } | |
| // Initialize the plugin | |
| new Dokan_Order_Meta_Updater(); | |
| /** | |
| * JavaScript for handling AJAX requests and updating the UI. | |
| */ | |
| function dokan_order_updater_script() { | |
| ?> | |
| <script> | |
| jQuery(document).ready(function($) { | |
| var $form = $('#dokan-order-updater-form'); | |
| var $progressBar = $('#progress-bar'); | |
| var $progress = $('#progress'); | |
| var $processedCount = $('#processed-count'); | |
| var $totalCount = $('#total-count'); | |
| var $updatedCount = $('#updated-count'); | |
| var $skippedCount = $('#skipped-count'); | |
| var $errorCount = $('#error-count'); | |
| var $errorLogContent = $('#error-log-content'); | |
| $form.on('submit', function(e) { | |
| e.preventDefault(); | |
| var orderIds = $('#order_ids').val().split(',').map(function(id) { | |
| return parseInt(id.trim(), 10); | |
| }).filter(Boolean); | |
| if (orderIds.length === 0) { | |
| alert('Please enter valid order IDs.'); | |
| return; | |
| } | |
| $progressBar.show(); | |
| $totalCount.text(orderIds.length); | |
| $progress.attr('max', orderIds.length); | |
| processOrderBatch(orderIds, 0); | |
| }); | |
| function processOrderBatch(orderIds, offset) { | |
| $.ajax({ | |
| url: dokanOrderUpdater.ajax_url, | |
| method: 'POST', | |
| data: { | |
| action: 'process_order_batch', | |
| nonce: dokanOrderUpdater.nonce, | |
| order_ids: orderIds.join(','), | |
| offset: offset | |
| }, | |
| success: function(response) { | |
| if (response.success) { | |
| updateUI(response.data); | |
| if (!response.data.is_complete) { | |
| processOrderBatch(orderIds, response.data.offset); | |
| } else { | |
| alert('Process completed!'); | |
| } | |
| } else { | |
| alert('Error: ' + response.data); | |
| } | |
| }, | |
| error: function() { | |
| alert('An error occurred. Please try again.'); | |
| } | |
| }); | |
| } | |
| function updateUI(data) { | |
| $processedCount.text(data.offset); | |
| $progress.val(data.offset); | |
| $updatedCount.text(parseInt($updatedCount.text()) + data.updated); | |
| $skippedCount.text(parseInt($skippedCount.text()) + data.skipped); | |
| $errorCount.text(parseInt($errorCount.text()) + data.errors); | |
| if (data.error_log.length > 0) { | |
| $errorLogContent.append(data.error_log.join('\n') + '\n'); | |
| } | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } | |
| add_action( 'admin_footer', 'dokan_order_updater_script' ); | |
| /** | |
| * Usage Guidelines: | |
| * | |
| * 1. Installation: | |
| * - Save this file as 'dokan-order-meta-updater.php' in your WordPress plugins directory. | |
| * - Activate the plugin from the WordPress admin panel. | |
| * | |
| * 2. Accessing the Tool: | |
| * - Go to Tools > Dokan Order Updater in the WordPress admin menu. | |
| * | |
| * 3. Using the Tool: | |
| * - Enter the Order IDs you want to update, separated by commas. | |
| * - Click "Start Update Process" to begin. | |
| * - The tool will process orders in batches and display progress. | |
| * - Results and any errors will be shown on the page. | |
| * | |
| * 4. Important Notes: | |
| * - This tool updates the '_dokan_vendor_id' meta for orders where it's not set. | |
| * - It determines the vendor ID based on the first product in each order. | |
| * - Orders that already have a vendor ID set will be skipped. | |
| * - Always backup your database before running bulk updates. | |
| * - Test on a staging environment first if possible. | |
| * | |
| * 5. Troubleshooting: | |
| * - Check the error log on the tool's page for any issues encountered during processing. | |
| * - Ensure that WooCommerce and Dokan are active and up-to-date. | |
| * - If you encounter persistent issues, contact the plugin author or Dokan support. | |
| * | |
| * @author Al Amin Ahamed | |
| * @link https://github.com/mralaminahamed | |
| * @version 1.0.0 | |
| */ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dokan Order Meta Updater - Usage Guidelines
Overview
The Dokan Order Meta Updater is a WordPress plugin designed to update the _dokan_vendor_id meta for specified WooCommerce orders where the vendor ID is not set. It's particularly useful for Dokan multivendor marketplace sites that need to retroactively assign vendor IDs to orders.
Installation
dokan-order-meta-updater.phpfile./wp-content/plugins/directory of your WordPress installation.Requirements
Using the Plugin
Features
Best Practices
Troubleshooting
Advanced Usage
Programmatic Usage
You can also initiate the update process programmatically:
Customizing the Plugin
To modify the plugin's behavior:
Dokan_Order_Meta_Updaterclass to override methods as needed.Example:
Support
If you encounter any issues or have questions, please visit the plugin's support page at https://gist.github.com/mralaminahamed/1c6d621ccad98d5f02dc37866e353601 or contact the plugin author, Al Amin Ahamed.
Remember to always keep the plugin, WordPress, WooCommerce, and Dokan updated to their latest versions for optimal performance and compatibility.
Changelog
Version 1.0.0
License
This plugin is licensed under the GPL v2 or later.