Plugin Directory

Changeset 3159415


Ignore:
Timestamp:
09/29/2024 04:04:21 AM (6 months ago)
Author:
wizbee
Message:

Update for 2.5.0

Location:
easy-duplicate-woo-order
Files:
12 added
4 edited

Legend:

Unmodified
Added
Removed
  • easy-duplicate-woo-order/trunk/easy-duplicate-woo-order.php

    r3158473 r3159415  
    33Plugin Name: Easy Duplicate Woo Order
    44Description: Adds a custom action to duplicate WooCommerce orders easily.
    5 Version: 2.4.0
     5Version: 2.5.0
    66Plugin URI: https://www.wizbeeit.com/easy-duplicate-woo-order
    77Author: wizbee IT
     
    5858function wizbee_enqueue_duplicate_order_script() {
    5959    if (is_admin()) {
    60         wp_enqueue_script('wizbee-duplicate-order-script', plugin_dir_url(__FILE__) . 'assets/js/easy-duplicate-order-redirect-script.js', array('jquery'), '1.0.0', true);
     60        wp_enqueue_script('wizbee-duplicate-order-script', plugin_dir_url(__FILE__) . 'assets/js/easy-duplicate-order-redirect-script.js', array('jquery'), '1.1', true);
    6161        $redirect_order_id = get_transient('wizbee_duplicate_order_redirect');
    6262        wp_localize_script('wizbee-duplicate-order-script', 'wizbeeOrderData', array(
     
    6969add_action('admin_enqueue_scripts', 'wizbee_enqueue_duplicate_order_script');
    7070
     71if (isset($_GET['page']) && $_GET['page'] === 'wc-settings' && isset($_GET['tab']) && $_GET['tab'] === 'wizbee_duplicate_order') {
     72        // Enqueue your JavaScript file
     73        wp_enqueue_script('wizbee-admin-script', plugin_dir_url(__FILE__) . 'assets/js/admin-settings.js', array('jquery'), '1.1', true);
     74    }
  • easy-duplicate-woo-order/trunk/includes/wb-duplicate-order-admin-option.php

    r3158473 r3159415  
    2323}
    2424
    25 // Define settings fields
     25
    2626function wizbee_get_settings() {
    2727    $order_statuses = wc_get_order_statuses();
     
    4141            'default'  => 'wc-pending',
    4242        ),
    43         'copy_coupons' => array( // New setting for copying coupons
    44             'name'     => __('Copy Coupons', 'easy-duplicate-woo-order'),
     43        'copy_old_price' => array(
     44            'name'     => __('Copy Old Price', 'easy-duplicate-woo-order'),
     45            'type'     => 'checkbox',
     46            'desc'     => __('Enable copying of price from the original order.<br>Enable if you use multi currency', 'easy-duplicate-woo-order'),
     47            'id'       => 'wizbee_duplicate_order_copy_old_price',
     48            'default'  => 'yes',
     49        ),
     50        'apply_coupons' => array(
     51            'name'     => __('Apply Coupons', 'easy-duplicate-woo-order'),
    4552            'type'     => 'checkbox',
    4653            'desc'     => __('Enable copying and applying of coupons from the original order.', 'easy-duplicate-woo-order'),
    47             'id'       => 'wizbee_duplicate_order_copy_coupons',
    48             'default'  => 'yes', // Default to enabled
     54            'id'       => 'wizbee_duplicate_order_apply_coupons',
     55            'default'  => 'yes',
     56            'class'    => 'coupons-options',
     57        ),
     58        'copy_fees' => array(
     59            'name'     => __('Copy Fee', 'easy-duplicate-woo-order'),
     60            'type'     => 'checkbox',
     61            'desc'     => __('Enable copying of fees from the original order.', 'easy-duplicate-woo-order'),
     62            'id'       => 'wizbee_duplicate_order_copy_fees',
     63            'default'  => 'yes',
     64            'class'    => 'fee-options',
     65        ),
     66        'copy_shipping' => array(
     67            'name'     => __('Copy Shipping', 'easy-duplicate-woo-order'),
     68            'type'     => 'checkbox',
     69            'desc'     => __('Enable copying of shipping information.', 'easy-duplicate-woo-order'),
     70            'id'       => 'wizbee_duplicate_order_copy_shipping',
     71            'default'  => 'yes',
     72            'class'    => 'ship-options',
    4973        ),
    5074        'section_end' => array(
  • easy-duplicate-woo-order/trunk/includes/wb-duplicate-order-function.php

    r3158473 r3159415  
    1212    $order_id = $order->get_id();
    1313    $new_order = wc_create_order();
    14    
    15     // Copy customer
    16     $user_id = $order->get_customer_id();
    17     $new_order->set_customer_id($user_id);
    1814
    19     // Copy billing and shipping addresses
     15    // Copy customer
     16    $user_id = $order->get_customer_id();
     17    $new_order->set_customer_id($user_id);
     18
     19    // Copy billing and shipping addresses
    2020    $new_order->set_address($order->get_address('billing'), 'billing');
    2121    $new_order->set_address($order->get_address('shipping'), 'shipping');
    2222
    23     // Copy currency information
    24     $currency = $order->get_currency();
    25     $new_order->set_currency($currency);
    26    
    27     // Copy products from old order
     23    // settings
     24    $copy_old_price = get_option('wizbee_duplicate_order_copy_old_price', 'yes');
     25    $copy_fees = get_option('wizbee_duplicate_order_copy_fees', 'yes');
     26    $copy_shipping = get_option('wizbee_duplicate_order_copy_shipping', 'yes');
     27    $apply_coupons = get_option('wizbee_duplicate_order_apply_coupons', 'yes');
     28
     29    // Product items
    2830    $items = $order->get_items();
    29     foreach ($items as $item_id => $originalOrderItem) {
    30         $itemName = $originalOrderItem['name'];
     31    foreach ($items as $item_id => $originalOrderItem) {
    3132        $quantity = $originalOrderItem['qty'];
    32         $lineTotal = $originalOrderItem['line_total'];
    33         $lineTax = $originalOrderItem['line_tax'];
     33        $variationID = $originalOrderItem['variation_id'];
     34        $lineSubtotal = $originalOrderItem['line_subtotal'];
    3435        $productID = $originalOrderItem['product_id'];
    35         $variationID = $originalOrderItem['variation_id'];
    36         $taxClass = $originalOrderItem['tax_class'];
    37         $lineSubtotal = $originalOrderItem['line_subtotal'];
    38         $lineSubtotalTax = $originalOrderItem['line_subtotal_tax'];
     36        $product = wc_get_product($productID);
     37
     38        if ('yes' === $copy_old_price) {
     39            if ('yes' === $apply_coupons) {
     40                $lineTotal = $originalOrderItem['line_total'];
     41            } else {
     42                $lineSubtotal = $originalOrderItem['line_subtotal'] / $quantity;
     43                $lineNewSubtotal = $lineSubtotal;
     44                $lineTotal = $lineNewSubtotal * $quantity;
     45            }
     46        } else {
     47            $lineTotal = $product ? $product->get_price() * $quantity : 0;
     48        }
     49
    3950        // Add the product as a line item in the new order
    4051        $new_item_id = wc_add_order_item($new_order->get_id(), [
    41             'order_item_name' => $itemName,
    42             'order_item_type' => 'line_item'
     52            'order_item_name' => $originalOrderItem['name'],
     53            'order_item_type' => 'line_item',
    4354        ]);
    44         // Copy over all relevant item meta fields
     55$original_meta_data = $originalOrderItem->get_meta_data();
     56    foreach ($original_meta_data as $meta) {
     57        wc_add_order_item_meta($new_item_id, $meta->key, $meta->value, true); // Add original meta to the new item
     58    }
     59        // Copy item meta and set calculated price
    4560        wc_add_order_item_meta($new_item_id, '_qty', $quantity);
    4661        wc_add_order_item_meta($new_item_id, '_product_id', $productID);
    47         wc_add_order_item_meta($new_item_id, '_variation_id', $variationID);
    48         wc_add_order_item_meta($new_item_id, '_tax_class', $taxClass);
     62        wc_add_order_item_meta($new_item_id, '_variation_id', $variationID);
    4963        wc_add_order_item_meta($new_item_id, '_line_total', wc_format_decimal($lineTotal));
    50         wc_add_order_item_meta($new_item_id, '_line_tax', wc_format_decimal($lineTax));
    5164        wc_add_order_item_meta($new_item_id, '_line_subtotal', wc_format_decimal($lineSubtotal));
    52         wc_add_order_item_meta($new_item_id, '_line_subtotal_tax', wc_format_decimal($lineSubtotalTax));
    5365    }
    5466
    55 
    56    
    57     // Copy shipping methods and costs
    58     $shipping_items = $order->get_items('shipping');
    59     foreach ($shipping_items as $shipping_item) {
    60         $new_shipping_item = new WC_Order_Item_Shipping();
    61         $new_shipping_item->set_method_title($shipping_item->get_method_title());
    62         $new_shipping_item->set_method_id($shipping_item->get_method_id());
    63         $new_shipping_item->set_total($shipping_item->get_total());
    64         $new_shipping_item->set_taxes($shipping_item->get_taxes());
    65         $new_order->add_item($new_shipping_item);
     67    // Copy currency
     68    if ('yes' === $copy_old_price) {
     69        $currency = $order->get_currency();
     70        $new_order->set_currency($currency);
    6671    }
    6772
    68     // Copy fees
    69     $fee_items = $order->get_items('fee');
    70     foreach ($fee_items as $fee_item) {
    71         $new_fee_item = new WC_Order_Item_Fee();
    72         $new_fee_item->set_name($fee_item->get_name());
    73         $new_fee_item->set_total($fee_item->get_total());
    74         $new_fee_item->set_taxes($fee_item->get_taxes());
    75         $new_order->add_item($new_fee_item);
     73    // Copy fees, shipping, and coupons
     74    if ('yes' === $copy_fees) {
     75        $fee_items = $order->get_items('fee');
     76        foreach ($fee_items as $fee_item) {
     77            $new_fee_item = new WC_Order_Item_Fee();
     78            $new_fee_item->set_name($fee_item->get_name());
     79            $new_fee_item->set_total($fee_item->get_total());
     80            //$new_fee_item->set_taxes($fee_item->get_taxes());
     81            $new_order->add_item($new_fee_item);
     82        }
    7683    }
    7784
    78     // Copy taxes
    79     $tax_items = $order->get_items('tax');
    80     foreach ($tax_items as $tax_item) {
    81         $new_tax_item = new WC_Order_Item_Tax();
    82         $new_tax_item->set_tax_rate_id($tax_item->get_tax_rate_id());
    83         $new_tax_item->set_label($tax_item->get_label());
    84         $new_tax_item->set_compound($tax_item->get_compound());
    85         $new_tax_item->set_tax_total($tax_item->get_tax_total());
    86         $new_tax_item->set_shipping_tax_total($tax_item->get_shipping_tax_total());
    87         $new_order->add_item($new_tax_item);
     85    if ('yes' === $copy_shipping) {
     86        $shipping_items = $order->get_items('shipping');
     87        foreach ($shipping_items as $shipping_item) {
     88            $new_shipping_item = new WC_Order_Item_Shipping();
     89            $new_shipping_item->set_method_title($shipping_item->get_method_title());
     90            $new_shipping_item->set_method_id($shipping_item->get_method_id());
     91            $new_shipping_item->set_total($shipping_item->get_total());
     92            //$new_shipping_item->set_taxes($shipping_item->get_taxes());
     93            // Copy shipping item meta data
     94            $meta_data = $shipping_item->get_meta_data();
     95            foreach ($meta_data as $meta) {
     96            $new_shipping_item->add_meta_data($meta->key, $meta->value, true);
     97        }
     98           
     99            $new_order->add_item($new_shipping_item);
     100        }
    88101    }
    89    // Copy coupons and apply them if enabled
    90     $copy_coupons = get_option('wizbee_duplicate_order_copy_coupons', 'yes'); // Get the copy coupons setting
    91     if ('yes' === $copy_coupons) { // Check if copying coupons is enabled
    92         $coupons = $order->get_used_coupons(); // Get the applied coupons
     102
     103    if ('yes' === $apply_coupons) {
     104        $coupons = $order->get_used_coupons();
    93105        foreach ($coupons as $coupon_code) {
    94             $new_order->apply_coupon($coupon_code); // Apply each coupon to the new order
     106            $new_order->apply_coupon($coupon_code);
    95107        }
    96108    }
    97109
    98110    // Set the selected status
    99     $original_status = $order->get_status();
    100111    $selected_status = get_option('wizbee_duplicate_order_status', 'wc-pending');
    101112    $new_order->set_status($selected_status);
    102113
    103     // Add a note about the status change
    104     if ($original_status !== $selected_status) {
    105         $new_order->add_order_note(sprintf(__('As per duplicate order setting, order status changed from %1$s to %2$s.', 'easy-duplicate-woo-order'),
    106             wc_get_order_status_name($original_status),
    107             wc_get_order_status_name($selected_status)
    108         ));
    109     }
    110 
    111     // Recalculate totals for the new order
     114 
    112115    $new_order->calculate_totals();
    113 
    114     // Save the new order
    115116    $new_order->save();
    116117
    117     // Add a custom order note about duplication
     118    // Add notes for tracking
    118119    $original_order_url = admin_url('post.php?post=' . $order_id . '&action=edit');
    119120    $new_order->add_order_note(sprintf(__('Duplicated from order <a href="%1$s">#%2$d</a>', 'easy-duplicate-woo-order'), esc_url($original_order_url), $order_id));
  • easy-duplicate-woo-order/trunk/readme.txt

    r3158473 r3159415  
    22Contributors: wizbee
    33Tags: woocommerce, duplicate order, manage order, order clone
    4 Requires at least: 5.0
     4Requires at least: 6.0
    55Tested up to: 6.6
    66Requires PHP: 7.2
    7 Stable tag: 2.4.0
     7Stable tag: 2.5.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    6565== Changelog ==
    6666
     67= 2.5.0 =
     68* Added - Option to control default copy of price, coupon, fees, and shipping from admin setting.
     69* Fix - Error if tax is enabled.
     70* Fix - Product variation handling.
     71* Improved - Copy of product meta data.
     72* Various other fixes and improvements.
     73
    6774= 2.4.0 =
    6875* Added - Option to control default copy and apply of coupon code.
Note: See TracChangeset for help on using the changeset viewer.