0

I'm trying to build a print functionality in my Woocommerce website, I've created a button that opens a print window when clicked & it's working fine.Woocommerce order page screenshot with 'get order details button' The problem I'm facing when I'm trying to print order's in bulk by selecting multiple order's at once, print window opens all orders in a single page Screenshot of print window with multiple orders on same page & I want to fix that, I need single order on single page so I can print orders properly.

add_filter( 'bulk_actions-edit-shop_order', 'add_bulk_action_print_orders' );
function add_bulk_action_print_orders( $actions ) {
   $actions['print_selected'] = __( 'Print selected orders', 'textdomain' );
   return $actions;
}
// Handle the custom bulk action
add_filter( 'handle_bulk_actions-edit-shop_order', 'handle_bulk_action_print_orders', 10, 3 );
function handle_bulk_action_print_orders( $redirect_to, $action, $order_ids ) {
   if ( $action !== 'print_selected' ) {
       return $redirect_to;
   }
$order_ids = isset( $_REQUEST['post'] ) ? $_REQUEST['post'] : array();
   if ( empty( $order_ids ) ) {
       return;
   }
foreach ( $order_ids as $order_id ) {
       $order = wc_get_order( $order_id );
$order_note = $order->get_customer_note();
$order_no = $order->get_order_number();
$order_total = $order->get_total();
$total_tax = $order->get_total_tax();
$subtotal = $order->get_subtotal();
$items=$order->get_items();

$billing_email=$order->get_billing_email();
$billing_phone=$order->get_billing_phone();

//$shipping_email = $order->get_shipping_email();
//$shipping_phone=$order->get_shipping_phone();

  $shipping_address_1 =$order->get_shipping_address_1();
$shipping_address_2 =$order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_country = $order->get_shipping_country();
$shipping_postcode = $order->get_shipping_postcode();

  $billing_address_1 =$order->get_billing_address_1();
$billing_address_2 =$order->get_billing_address_2();
$billing_city = $order->get_billing_city();
$billing_state = $order->get_billing_state();
$billing_country = $order->get_billing_country();
$billing_postcode = $order->get_billing_postcode();
echo'<style>.my-button.clicked{background-color:yellow;}</style>';
echo'<style>body{ background-color:#fff;margin: 0;padding: 0;}</style>';
echo'<style> h1,h2,h3,h4,h5,h6{ margin: 0; padding: 0;} p{ margin: 0; padding: 0; } </style>';
echo'<style> .container{ width: 100%; margin-right: auto; margin-left: auto; } .brand-section{ background-color: #b2ac88; padding: 10px 40px;} </style>'; echo'<style>.logo{ width: 50%;} .row{ display: flex; flex-wrap: wrap; } .col-6{ width: 50%; flex: 0 0 auto; } .text-white{ color: #fff; } </style>';
echo'<style> .company-details{ float: right; text-align: right;} .body-section{ padding: 16px; border: 1px solid gray;} </style>';
echo'<style>.heading{ font-size: 20px; margin-bottom: 08px; } .sub-heading{ color: #262626; margin-bottom: 05px; } </style>';
echo'<style> table{ background-color: #fff; width: 100%; border-collapse: collapse; } </style>';
echo'<style> table thead tr{ border: 1px solid #111; background-color: #fff; } table td { vertical-align: middle !important; text-align: center; }</style>';
echo'<style> table th, table td { padding-top: 08px; padding-bottom: 08px; } .table-bordered{box-shadow: 0px 0px 5px 0.5px gray;} </style>';
echo'<style> table-bordered td, .table-bordered th { order: 1px solid #dee2e6; } .text-right{ ext-align: end;} .w-20{ width: 20%;} </style>';
echo'<style>.float-right{float: right; }</style>';
echo'<h3>Inovice</h3>';
echo'<div class="container">';
echo '<div class="brand-section">';
echo '<div class="row">';
echo '<div class="col-6">';
                   echo '<h1 class="text-white">Order No.</h1>'.$order_no.'</div>';
echo '<div class="col-6">';
                   echo '<div class="company-details">';
echo '<h2 class="text-white">Oven Fresh</h2>';
                      echo '<p class="text-white">Unit No. 210 2nd floor,Mahim Industrial Estate,Off Cadel Road Mahim,Mumbai-400016 <br>GST No: 27AAUCA4280D1ZI <br> FSSAI NO:11521004000462 <br>State Name: Maharashtra, Code: 27</p>';
                      echo '<p class="text-white">+91-8080-146666</p>';
                   echo '</div>';
               echo '</div>';
           echo '</div>';
       echo '</div>';
echo '<div class="body-section">';
          echo '<div class="row">';
               echo '<div class="col-6">';
                  echo '<h2 class="heading">Billing Address</h2>';  echo'<br>'.$billing_address_1.$billing_address_2.$billing_city.$billing_state.$billing_country.$billing_postcode ;
                  echo '<p class="sub-heading">Email </p>'.$billing_email;
                  echo '<p class="sub-heading">Phone </p>'.$billing_phone;
                  
             echo '</div>';
echo '<div class="col-6">';
                  echo '<h2 class="heading">Shipping Address</h2>'.$shipping_address_1.$shipping_address_2.$shipping_city.$shipping_state.$shipping_country.$shipping_postcode;
//  echo '<p class="sub-heading">Delivery Date </p>'.$delivery_date;
                //  echo '<p class="sub-heading">Time Slot</p>'.$time_slot;
  foreach ( $order->get_meta_data() as $meta ) {
if ( $meta->key === 'Delivery Date' || $meta->key === 'Time Slot' ) {
           echo '<p><strong>' . $meta->key . ':</strong> ' . $meta->value . '</p>';
       }
}
 echo '<p><strong>Customer Note :</strong>'.$order_note.'</p>';
             echo '</div>';
           echo '</div>';
       echo '</div>';
echo '<div class="body-section">';
          echo '<h3 class="heading">Ordered Items</h3>';
           echo '<br>';
           echo '<table class="table-bordered">';
               echo '<thead>';
                   echo '<tr>';
                       echo '<th>Product Name</th>';
                       echo '<th>Quantity</th>';
                       echo '<th>HSN Code</th>';
                       echo '<th>Price</th>';
echo '<th class="w-20">Grandtotal</th>';
                   echo '</tr>';
               echo '</thead>';
               echo '<tbody>';
        foreach ( $items as $item ) {
$product=$item->get_product();
$product_name = $item->get_name();
$quantity= $item->get_quantity();
$price = $item->get_product()->get_regular_price();
    $hsn_code=$product->get_meta('_hsn_code');
 $grandtotal = wc_price($price * $quantity);
echo '<tr>';
echo '<td>'.$product_name.'</td>';
echo '<td>'.$quantity.'</td>';
echo '<td>'.$hsn_code.'</td>';
echo '<td>'.$price.'</td>';
echo '<td>'.$grandtotal.'</td>';
echo '</tr>';
 }

echo'<tr>';
                      echo '<td colspan="4" class="text-right">Sub Total</td>';
                      echo '<td>'.$subtotal.'</td>';
                  echo '</tr>';
                   echo '<tr>';
                       echo '<td colspan="4" class="text-right">Tax</td>';
                       echo '<td>'.$total_tax.'</td>';
                   echo '</tr>';
                   echo '<tr>';
                       echo'<td colspan="4" class="text-right">Total</td>';
                       echo'<td>'.$order_total.'</td>';
                   echo'</tr>';
echo'</tbody>';
           echo '</table>';
echo '<div>';
           echo '<p>Footer </p>';
       echo '</div>';      
echo '</div>';
}
}

I'm using the code mentioned above, is there something missing or am I doing something wrong here. I need you guys help to solve this as soon as possible. Thank you!

raavan
  • 1
  • 2
  • Since this is html - https://stackoverflow.com/questions/1664049/can-i-force-a-page-break-in-html-printing – Snuffy May 17 '23 at 08:01

0 Answers0