0

For the past week I have been trying to solve a problem related to a custom php script for WooCommerce.

The goal is to add functionality to the bulk order and retrieve the orders I selected and print them in an excel file. The code is separated into two php files. One to grab the orders and print them on a text file. The second is the printing of the text file to Excel.

Based on How to get WooCommerce order details answer code here is my main script:

// Adding to admin order list bulk dropdown a custom action 'custom_downloads'
add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 20, 1 );

function downloads_bulk_actions_edit_product( $actions ) {
    $actions['write_downloads'] = __( 'Download orders', 'woocommerce' );
    return $actions;  
}

// Make the action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order', 10, 3 );

function downloads_handle_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
   
    if ( $action !== 'write_downloads' ) return $redirect_to; // Exit

    $processed_ids = array();
    
    $file = WP_PLUGIN_DIR."/xplugin/Order.txt"; 
    $myfile = fopen($file, "w") or die("STOPP,unable to open file!");
          
    foreach ( $post_ids as $post_id ) {
        $order = wc_get_order( $post_id );
        $order_data = $order->get_data();
        $order_billing_first_name = $order_data['shipping']['first_name'];
        $order_billing_last_name = $order_data['shipping']['last_name'];
        $order_billing_address_1 = $order_data['shipping']['address_1'];
        $order_billing_address_2 = $order_data['shipping']['address_2'];
        $order_billing_postcode = $order_data['shipping']['postcode'];

        //Hvis nummeret starter på 0 så må du fikse excel 
        $order_billing_phone = $order_data['billing']['phone'];

        //hvis antallet tegn er 8 eller mindre må du legge på "+47" 
        if( mb_strlen($order_billing_phone) <= 8) {
           $order_billing_phone = '+47'. $order_billing_phone; 
        }

        $order_billing_email = $order_data['billing']['email'];
        $Pose_på_dør = 'YES';

      //Skriver til en den nyåpne tekstfila  
      fwrite($myfile,
            $order_billing_first_name. ' ' .
            $order_billing_last_name. '; ' .
            $order_billing_address_1. '; ' .
            $order_billing_address_2.'; ' .
            $order_billing_postcode.'; ' .
            $order_billing_first_name. ' ' .
            $order_billing_last_name. '; ' .
            $order_billing_phone.'; ' .
            $order_billing_email.'; ' .
            $order_billing_first_name. ' ' .
            $order_billing_last_name. '; ' .
            $order_billing_first_name. ' ' .
            $order_billing_last_name. '; ' .
            $Pose_på_dør. "\n"
        );
 
        $processed_ids[] = $post_id;
   
    }

    printExcel();


    fclose($myfile);

    return $redirect_to = add_query_arg( array(
        'write_downloads' => '1',
        'processed_count' => count( $processed_ids ),
        'processed_ids' => implode( ',', $processed_ids ),
    ), $redirect_to );
  
}

// The results notice from bulk action on orders
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice' );
function downloads_bulk_action_admin_notice() {
    if ( empty( $_REQUEST['write_downloads'] ) ) return; // Exit

    $count = intval( $_REQUEST['processed_count'] );

    printf( '<div id="message" class="updated fade"><p>' .
        _n( 'Processed %s Order for downloads.',
        'Processed %s Orders for downloads.',
        $count,
        'write_downloads'
    ) . '</p></div>', $count );
}

function printExcel() {
   include 'print.php'; 
}

Both scripts behave well, but when I go about combining them with an

function printExcel() {
   include 'print.php'; 
}

it goes wrong.

I'm not able to execute the "print.php" script at the right time.I would like it to run after admin notice is done updating.

I have tried to make an action for different hooks, but not successfully. Can anyone point me in the right direction?

Any suggestions on how to improve the script are much appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Bertil
  • 25
  • 4
  • Wrap everything in functions and just call them in the right order. – Grumpy Feb 04 '21 at 13:47
  • Tried but failed unfortunatly. Since it is connected to different hooks and are running when the hooks activated it becomes diffucult to set a running order. – Bertil Feb 04 '21 at 14:30

0 Answers0