PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/class-wc-dropshipping-orders.php

https://bitbucket.org/hokbay/dropshipping-for-woocommerce
PHP | 311 lines | 228 code | 25 blank | 58 comment | 19 complexity | 86d29099186f09df543e67273687ef70 MD5 | raw file
  1. <?php
  2. class WC_Dropshipping_Orders {
  3. public function __construct() {
  4. $this->init();
  5. }
  6. public function init() {
  7. // order processing
  8. add_filter('wc_dropship_manager_send_order_email_html',array($this,'send_order_email_html'));
  9. add_filter('wc_dropship_manager_send_order_attachments',array($this,'send_order_attach_packingslip'),10,3);
  10. add_action('woocommerce_order_actions',array( $this,'add_order_meta_box_order_processing'));
  11. add_action('woocommerce_order_status_processing',array($this,'order_processing'));
  12. add_action('woocommerce_order_action_resend_dropship_supplier_notifications',array($this,'order_processing'));
  13. add_action('wc_dropship_manager_send_order',array($this,'send_order'),10, 2);
  14. }
  15. public function add_order_meta_box_order_processing( $actions ) {
  16. $actions['resend_dropship_supplier_notifications'] = 'Resend Dropship Supplier Notifications';
  17. return $actions;
  18. }
  19. /* Notify Suppliers */
  20. // perform all tasks that happen once an order is set to processing
  21. public function order_processing( $order_id ) {
  22. $order = new WC_Order( $order_id ); // load the order from woocommerce
  23. $this->notify_warehouse($order); // notify the warehouse to ship the order
  24. }
  25. // parse the order, build pdfs, and send orders to the correct suppliers
  26. public function notify_warehouse( $order ) {
  27. $order_info = $this->get_order_info($order);
  28. $supplier_codes = $order_info['suppliers'];
  29. // for each supplier code, loop and send email with product info
  30. foreach($supplier_codes as $code => $supplier_info) {
  31. do_action('wc_dropship_manager_send_order',$order_info,$supplier_info);
  32. }
  33. }
  34. public function get_order_shipping_info($order) {
  35. $keys = explode(',','shipping_first_name,shipping_last_name,shipping_address_1,shipping_address_2,shipping_city,shipping_state,shipping_postcode,shipping_country,billing_phone,shipping_company');
  36. $info = array();
  37. $info['name'] = $order->shipping_first_name.' '.$order->shipping_last_name;
  38. $info['phone'] = $this->formatPhone($order->billing_phone);
  39. $info['shipping_method'] = $order->get_shipping_method();
  40. foreach($keys as $key) {
  41. $info[$key] = $order->$key;
  42. }
  43. return $info;
  44. }
  45. public function get_order_billing_info($order) {
  46. $keys = explode(',','billing_first_name,billing_last_name,billing_address_1,billing_address_2,billing_city,billing_state,billing_postcode,billing_country,billing_phone,billing_email,billing_company');
  47. $info = array();
  48. $info['name'] = $order->billing_first_name.' '.$order->billing_last_name;
  49. $info['phone'] = $this->formatPhone($order->billing_phone);
  50. foreach($keys as $key) {
  51. $info[$key] = $order->$key;
  52. }
  53. return $info;
  54. }
  55. public function get_order_product_info($item,$product) {
  56. $info = array();
  57. $info['sku'] = $product->get_sku();
  58. $info['qty'] = $item['qty'];
  59. $info['name'] = $item['name'];
  60. $product_attributes = maybe_unserialize( get_post_meta( $product->id, '_product_attributes', true ) );
  61. $info['product_attribute_keys'] = array();
  62. if(is_array($product_attributes)) {
  63. $info['product_attribute_keys'] = array_keys($product_attributes);
  64. foreach($product_attributes as $key=>$data) {
  65. $info[$key] = $data['value'];
  66. }
  67. }
  68. // Product Variations
  69. $info['variation_data'] = [];
  70. if($product->variation_id)
  71. {
  72. $info['variation_data'] = $product->get_variation_attributes();
  73. }
  74. // Product Add-Ons Plugin
  75. $info['order_item_meta'] = [];
  76. if(function_exists('get_product_addons'))
  77. {
  78. $info['order_item_meta'] = $item->get_formatted_meta_data();
  79. $info['product_addons'] = get_product_addons($product);
  80. /*for($i=0;$i<count($info['product_addons']);$i++)
  81. {
  82. $addon = $info['product_addons'][$i];
  83. $addon['key'] = $this->get_addon_key_string($addon);
  84. $info['product_addons'][$i] = $addon;
  85. }*/
  86. foreach($info['order_item_meta'] as $key=>$item_meta)
  87. {
  88. $info['order_item_meta'][$key]->display_label = $this->get_addon_display_label($info['order_item_meta'][$key]);
  89. }
  90. }
  91. //$info['shipping_methods'] = $order->get_shipping_methods();
  92. //$info['meta_html'] = wc_display_item_meta($item);
  93. //$info['item'] = $item;
  94. //$info['product'] = $product;
  95. return $info;
  96. }
  97. private function get_addon_display_label($item_meta)
  98. {
  99. $d = $item_meta->display_key;
  100. // remove the price from the meta display name
  101. return trim(preg_replace('/\(\$\d.*\)/','',$d));
  102. }
  103. /*private function get_addon_key_string($addon)
  104. {
  105. $key = $addon['name'];
  106. if ( $addon['price'] > 0 && apply_filters( 'woocommerce_addons_add_price_to_name', true ) ) {
  107. $key .= ' (' . strip_tags( wc_price( get_product_addon_price_for_display( $addon['price'], $values['data'], true ) ) ) . ')';
  108. }
  109. return $key;
  110. }*/
  111. public function get_order_info($order) {
  112. // gather some of the basic order info
  113. $order_info = array();
  114. $order_info['id'] = $order->get_order_number();
  115. $order_info['number'] = $order->get_order_number();
  116. $order_info['options'] = get_option( 'wc_dropship_manager' );
  117. $order_info['shipping_info'] = $this->get_order_shipping_info($order);
  118. $order_info['billing_info'] = $this->get_order_billing_info($order);
  119. $order_info['order'] = $order;
  120. // for each item determine what products go to what suppliers.
  121. // Build product/supplier lists so we can send send out our order emails
  122. $order_info['suppliers'] = array();
  123. $items = $order->get_items();
  124. if ( count( $items ) > 0 ) {
  125. foreach( $items as $item_id => $item ) {
  126. $ds = wc_dropshipping_get_dropship_supplier_by_product_id( intval( $item['product_id'] ) );
  127. if ($ds['id'] > 0) {
  128. $product = $order->get_product_from_item( $item ); // get the product obj
  129. $prod_info = $this->get_order_product_info($item,$product);
  130. if(!array_key_exists($ds['slug'],$order_info['suppliers']))
  131. {
  132. $order_info['suppliers'][$ds['slug']] = $ds; // ...add newly found dropship_supplier to the supplier array
  133. $order_info[$ds['slug']] = array(); // ... and create an empty array to store product info in
  134. }
  135. $order_info[$ds['slug']][] = $prod_info;
  136. //$order_info[$ds['slug'].'_raw'][] = $product;
  137. }
  138. }
  139. } else {
  140. // how did we get here?
  141. //$this->sendAlert('No Products found for order #'.$order_info['id'].'!');
  142. //die;
  143. }
  144. return $order_info;
  145. }
  146. public function formatPhone($pnum) {
  147. return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '($1) $2-$3', $pnum);
  148. }
  149. public function get_from_name() {
  150. return wp_specialchars_decode(get_option( 'woocommerce_email_from_name' ));
  151. }
  152. public function get_from_address() {
  153. return get_option( 'woocommerce_email_from_address' );
  154. }
  155. public function get_content_type() {
  156. return " text/html";
  157. }
  158. // for sending failure notifications
  159. public function sendAlert($text) {
  160. wp_mail( get_bloginfo('admin_email'), 'Alert from '.get_bloginfo('name'), $text );
  161. }
  162. public function make_directory( $path ) {
  163. $upload_dir = wp_upload_dir();
  164. $order_dir = $upload_dir['basedir'].'/'.$path;
  165. if( ! file_exists( $order_dir ) )
  166. wp_mkdir_p( $order_dir );
  167. return $order_dir;
  168. }
  169. // generate packingslip PDF
  170. public function make_pdf($order_info,$supplier_info,$html,$file_name) {
  171. // Include TCPDF library
  172. if (!class_exists('TCPDF')) {
  173. require_once( wc_dropshipping_get_base_path() . '/lib/tcpdf_min/tcpdf.php' );
  174. }
  175. $options = get_option( 'wc_dropship_manager' );
  176. // make a directory for the current order (if it doesn't already exist)
  177. $pdf_path = $this->make_directory($order_info['id']);
  178. // generate a pdf for the current order and the current supplier
  179. $file = $pdf_path.'/'.$file_name;
  180. // create new PDF document
  181. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  182. // set document information
  183. $pdf->SetCreator(PDF_CREATOR);
  184. // set default header data
  185. $pdf->SetHeaderData($options['packing_slip_url_to_logo'], $options['packing_slip_url_to_logo_width'], get_option( 'woocommerce_email_from_name' ).' '.date('Y-m-d'));
  186. // set header and footer fonts
  187. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  188. //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  189. // remove default header/footer
  190. //$pdf->setPrintHeader(false);
  191. $pdf->setPrintFooter(false);
  192. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set default monospaced font
  193. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); // set margins
  194. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  195. //$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  196. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set auto page breaks
  197. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set image scale factor
  198. $pdf->AddPage();
  199. $pdf->writeHTML($html, true, false, true, false, '');
  200. $pdf->Output($file, 'F'); // save PDF
  201. return $file;
  202. }
  203. // get HTML packingslip
  204. public function get_packingslip_html($order_info,$supplier_info) {
  205. $html = '';
  206. $filename = 'packingslip.html';
  207. if (file_exists(get_stylesheet_directory().'/woocommerce-dropshipping/'.$supplier_info['slug'].'_'.$filename))
  208. {
  209. /* User can create a custom supplier packingslip PDF by creating a "woocommerce-dropshipping" directory
  210. inside their theme's directory and placing a custom SUPPLIERCODE_packingslip.html there */
  211. $templatepath = get_stylesheet_directory().'/woocommerce-dropshipping/'.$supplier_info['slug'].'_'.$filename;
  212. }
  213. else if (file_exists(get_stylesheet_directory().'/wc_dropship_manager/'.$supplier_info['slug'].'_'.$filename))
  214. {
  215. /* User can create a custom supplier packingslip PDF by creating a "dropship_manager" directory
  216. inside their theme's directory and placing a custom SUPPLIERCODE_packingslip.html there */
  217. $templatepath = get_stylesheet_directory().'/wc_dropship_manager/'.$supplier_info['slug'].'_'.$filename;
  218. }
  219. else if (file_exists(get_stylesheet_directory().'/woocommerce-dropshipping/'.$filename))
  220. {
  221. /* User can override the default packingslip PDF by creating a "woocommerce-dropshipping" directory
  222. inside their theme's directory and placing a custom packingslip.html there */
  223. $templatepath = get_stylesheet_directory().'/woocommerce-dropshipping/'.$filename;
  224. }
  225. else if (file_exists(get_stylesheet_directory().'/wc_dropship_manager/'.$filename))
  226. {
  227. /* User can override the default packingslip PDF by creating a "dropship_manager" directory
  228. inside their theme's directory and placing a custom packingslip.html there */
  229. $templatepath = get_stylesheet_directory().'/wc_dropship_manager/'.$filename;
  230. }
  231. else
  232. {
  233. $templatepath = wc_dropshipping_get_base_path() . $filename;
  234. }
  235. return $this->get_template_html($templatepath,$order_info,$supplier_info);
  236. }
  237. public function get_template_html($templatepath,$order_info,$supplier_info) {
  238. $html = '';
  239. ob_start();
  240. if (file_exists($templatepath)){
  241. include($templatepath);
  242. } else {
  243. echo '<b>Template '.$templatepath.' not found!</b>';
  244. }
  245. $html = ob_get_clean();
  246. return $html;
  247. }
  248. // send the pdf to the supplier
  249. public function send_order($order_info,$supplier_info) {
  250. $attachments = array();
  251. $attachments = apply_filters('wc_dropship_manager_send_order_attachments',$attachments,$order_info,$supplier_info); // create a pdf packing slip file
  252. $options = get_option( 'wc_dropship_manager' );
  253. $text = '';
  254. $hdrs = array();
  255. $hdrs['From'] = get_option( 'woocommerce_email_from_address' );
  256. $hdrs['To'] = $supplier_info['order_email_addresses'].','.get_option( 'woocommerce_email_from_address' );
  257. $hdrs['CC'] = get_option( 'woocommerce_email_from_address' );
  258. $hdrs['Subject'] = 'New Order #'.$order_info['id'].' From '.get_option( 'woocommerce_email_from_name' );
  259. $hdrs['Content-Type'] = 'multipart/mixed';
  260. if (strlen($supplier_info['account_number']) > 0)
  261. {
  262. $text .= get_option( 'woocommerce_email_from_name' ).' account number: '.$supplier_info['account_number'].'<br/>';
  263. }
  264. $text = $this->get_packingslip_html($order_info,$supplier_info);
  265. $text = $options['email_order_note'] . $text;
  266. $html = apply_filters('wc_dropship_manager_send_order_email_html',$text);
  267. // Filters for the email
  268. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  269. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  270. add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  271. wp_mail( $hdrs['To'], $hdrs['Subject'], $html, $hdrs, $attachments );
  272. // Unhook filters
  273. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  274. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  275. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  276. }
  277. public function send_order_email_html( $text ) {
  278. return '<b>'.$text.'</b>';
  279. }
  280. public function send_order_attach_packingslip($attachments,$order_info,$supplier_info) {
  281. $html = $this->get_packingslip_html($order_info,$supplier_info);
  282. $file_name = $order_info['id'].'_'.$supplier_info['slug'].'.pdf';
  283. $attachments['pdf_packingslip'] = $this->make_pdf($order_info,$supplier_info,$html,$file_name); // create a pdf packing slip file
  284. return $attachments;
  285. }
  286. }