Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //What I ended up doing was a little tricky as there aren't wordpress actions/filters really to hook into in the desktop version of your plugin. I ended up creating a php class from within the theme files of the website, not physically touching any myworks plugin files. I started by programmatically backing up the public/class-mw-qbo-desktop-public.php file and then changing a line that includes the partials/mw-qbo-desktop-public-qwc-server.php to include my own version of that file. The top of that file has a require_once spot for the includes/class-mw-qbo-desktop-qwc-server-lib.php file. In this spot I require_once'd my own qbc-server-lib file.
- // Change file
- $file_name = 'public/class-mw-qbo-desktop-public.php';
- $file_uri = $this->myworks_plugin_dir_path . $file_name;
- // Make a copy
- if ( !file_exists( $file_uri . '.bak' ) ) {
- copy( $file_uri, $file_uri . '.bak' );
- }
- if ( !is_dir( $this->child_mwqb_dir_path . 'public' ) ) {
- mkdir( $this->child_mwqb_dir_path . 'public', 0777, true );
- }
- copy( $file_uri . '.bak', str_replace( 'public/class-mw-qbo-desktop-public.php', 'public/temp_class-mw-qbo-desktop-public.php', $file_uri ) . '.bak' );
- rename( str_replace( 'public/class-mw-qbo-desktop-public.php', 'public/temp_class-mw-qbo-desktop-public.php', $file_uri ) . '.bak', $this->child_mwqb_dir_path . $file_name . '.bak' );
- $file = fopen( $file_uri, "r+" );
- $text = fread( $file, filesize( $file_uri ) );
- $lines = explode( PHP_EOL, $text );
- foreach ( $lines as $key => $line ) {
- if ( strpos( $line, 'array_key_exists( \'mw_qbo_desktop_qwc_server\', $wp->query_vars )' ) !== false ) {
- $next_key = $key + 1;
- if ( strpos( $lines[$next_key], 'require_once plugin_dir_path( __FILE__ ) . \'partials/mw-qbo-desktop-public-qwc-server.php\';' ) !== false ) {
- // Change line in file
- $lines[$next_key] = 'require_once \'' . $this->child_mwqb_dir_path . 'partials/child-mw-qbo-desktop-public-qwc-server.php\';';
- }
- }
- }
- $lines = implode( PHP_EOL, $lines );
- rewind( $file );
- fwrite( $file, $lines );
- //The part in question though, the tracking, I had to add a couple lines in my own qwc-server.php file. In the array under the comment "Map QuickBooks actions to hander functions", I added the importing invoices quickbooks handlers. The original adding invoice request and response are still in place and I added my code at the bottom. I think I found these extra handlers flipping through the includes/lib/qbo-lib files in the depths of the plugin. (I actually had to implement a way to trigger customer sync's as well with Customer_MOD_Import_Request but that was for some different functionality with price levels. My client was demanding.).
- // Map QuickBooks actions to handler functions
- $map = array(
- QUICKBOOKS_ADD_CUSTOMER => array( array( $MWQDC_QWC, 'AddCustomerRequest' ), array( $MWQDC_QWC, 'AddCustomerResponse' ) ),
- QUICKBOOKS_ADD_GUEST => array( array( $MWQDC_QWC, 'AddGuestRequest' ), array( $MWQDC_QWC, 'AddGuestResponse' ) ),
- 'CustomerJobAdd' => array( array( $MWQDC_QWC, 'AddCustomerJobRequest' ), array( $MWQDC_QWC, 'AddCustomerJobResponse' ) ),
- QUICKBOOKS_ADD_INVOICE => array( array( $MWQDC_QWC, 'AddInvoiceRequest' ), array( $MWQDC_QWC, 'AddInvoiceResponse' ) ),
- QUICKBOOKS_ADD_SALESRECEIPT => array( array( $MWQDC_QWC, 'AddSalesReceiptRequest' ), array( $MWQDC_QWC, 'AddSalesReceiptResponse' ) ),
- //
- 'SalesReceiptMod_GPI' => array( array( $MWQDC_QWC, 'UpdateSalesReceiptRequest_GPI' ), array( $MWQDC_QWC, 'UpdateSalesReceiptResponse_GPI' ) ),
- /**/
- 'InvoiceDataExt' => array( array( $MWQDC_QWC, 'OrderDataExtAddRequest' ), array( $MWQDC_QWC, 'OrderDataExtAddResponse' ) ),
- 'SalesReceiptDataExt' => array( array( $MWQDC_QWC, 'OrderDataExtAddRequest' ), array( $MWQDC_QWC, 'OrderDataExtAddResponse' ) ),
- 'SalesOrderDataExt' => array( array( $MWQDC_QWC, 'OrderDataExtAddRequest' ), array( $MWQDC_QWC, 'OrderDataExtAddResponse' ) ),
- 'EstimateDataExt' => array( array( $MWQDC_QWC, 'OrderDataExtAddRequest' ), array( $MWQDC_QWC, 'OrderDataExtAddResponse' ) ),
- QUICKBOOKS_ADD_SALESORDER => array( array( $MWQDC_QWC, 'AddSalesOrderRequest' ), array( $MWQDC_QWC, 'AddSalesOrderResponse' ) ),
- //
- QUICKBOOKS_ADD_ESTIMATE => array( array( $MWQDC_QWC, 'AddEstimateRequest' ), array( $MWQDC_QWC, 'AddEstimateResponse' ) ),
- //
- QUICKBOOKS_ADD_PURCHASEORDER => array( array( $MWQDC_QWC, 'AddPurchaseOrderRequest' ), array( $MWQDC_QWC, 'AddPurchaseOrderResponse' ) ),
- 'ReceivePaymentAdd' => array( array( $MWQDC_QWC, 'AddPaymentRequest' ), array( $MWQDC_QWC, 'AddPaymentResponse' ) ),
- 'OrderPaymentAdd' => array( array( $MWQDC_QWC, 'AddOrderPaymentRequest' ), array( $MWQDC_QWC, 'AddOrderPaymentResponse' ) ),
- /*Update*/
- 'CustomerMod_Query' => array( array( $MWQDC_QWC, 'CustomerMod_Query_Request' ), array( $MWQDC_QWC, 'CustomerMod_Query_Response' ) ),
- QUICKBOOKS_MOD_CUSTOMER => array( array( $MWQDC_QWC, 'UpdateCustomerRequest' ), array( $MWQDC_QWC, 'UpdateCustomerResponse' ) ),
- //
- 'CheckAdd' => array( array( $MWQDC_QWC, 'AddRefundRequest' ), array( $MWQDC_QWC, 'AddRefundResponse' ) ),
- 'CreditMemoAdd' => array( array( $MWQDC_QWC, 'AddRefundRequest' ), array( $MWQDC_QWC, 'AddRefundResponse' ) ),
- QUICKBOOKS_ADD_INVENTORYITEM => array( array( $MWQDC_QWC, 'AddInventoryItemRequest' ), array( $MWQDC_QWC, 'AddInventoryItemResponse' ) ),
- QUICKBOOKS_ADD_NONINVENTORYITEM => array( array( $MWQDC_QWC, 'AddNonInventoryRequest' ), array( $MWQDC_QWC, 'AddNonInventoryResponse' ) ),
- QUICKBOOKS_ADD_SERVICEITEM => array( array( $MWQDC_QWC, 'AddServiceItemRequest' ), array( $MWQDC_QWC, 'AddServiceItemResponse' ) ),
- 'V_'.QUICKBOOKS_ADD_INVENTORYITEM => array( array( $MWQDC_QWC, 'AddInventoryItemRequest' ), array( $MWQDC_QWC, 'AddInventoryItemResponse' ) ),
- 'V_'.QUICKBOOKS_ADD_NONINVENTORYITEM => array( array( $MWQDC_QWC, 'AddNonInventoryRequest' ), array( $MWQDC_QWC, 'AddNonInventoryResponse' ) ),
- 'V_'.QUICKBOOKS_ADD_SERVICEITEM => array( array( $MWQDC_QWC, 'AddServiceItemRequest' ), array( $MWQDC_QWC, 'AddServiceItemResponse' ) ),
- 'InventoryAdjustmentAdd' => array( array( $MWQDC_QWC, 'InventoryAdjustmentAddRequest' ), array( $MWQDC_QWC, 'InventoryAdjustmentAddResponse' ) ),
- 'V_InventoryAdjustmentAdd' => array( array( $MWQDC_QWC, 'InventoryAdjustmentAddRequest' ), array( $MWQDC_QWC, 'InventoryAdjustmentAddResponse' ) ),
- /** @author M.E. */
- QUICKBOOKS_IMPORT_INVOICE => array( array( $MWQDC_QWC, 'Invoice_Import_Request' ), array( $MWQDC_QWC, 'Invoice_Import_Response') ),
- 'New_' . QUICKBOOKS_IMPORT_INVOICE => array( array( $MWQDC_QWC, 'Invoice_Import_Request' ), array( $MWQDC_QWC, 'Invoice_Import_Response') ),
- 'Manage_' . QUICKBOOKS_MOD_CUSTOMER => array( array( $MWQDC_QWC, 'Customer_MOD_Import_Request' ), array( $MWQDC_QWC, 'Customer_Import_Response' ) ),
- );
- //Then within the includes/class-mw-qbo-desktop-qwc-server-lib.php file, Within the Hook_Login_Success() method - which runs every web connector connection - I added the New_Quickbooks_Import_Invoice action to the $Queue.
- / **
- * @author M.E.
- */
- if ( class_exists( 'Child_MWQB' ) ) {
- if (!$this->_quickbooks_get_last_run($user, 'New_'.QUICKBOOKS_IMPORT_ITEM)){
- $this->_quickbooks_set_last_run($user, 'New_'.QUICKBOOKS_IMPORT_ITEM, $date);
- }
- $Child_MWQB = new Child_MWQB();
- //$Child_MWQB->new_customer_import_response( $arr['ListID'] );
- $Queue->enqueue( 'New_' . QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_REFRESH_DATA_IMPORT, array( 'new'=>true, 'timestamp_from'=>$timestamp_from ), $this->get_qbun() );
- }
- /* End @author M.E. */
- //The Invoice_Import_Request and Response, I created by mostly borrowing parts from elsewhere within the file, I believe. I added a spot within the response that looks for the tracking number.
- /**
- * @author M.E.
- */
- public function Invoice_Import_Request( $requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale ) {
- if(!$this->is_qwc_connected()){
- return false;
- }
- $attr_iteratorID = '';
- $attr_iterator = ' iterator="Start" ';
- if (empty($extra['iteratorID'])){
- $last = $this->_quickbooks_get_last_run($user, $action);
- $this->_quickbooks_set_last_run($user, $action);
- $this->_quickbooks_set_current_run($user, $action, $last);
- }else{
- $attr_iteratorID = ' iteratorID="' . $extra['iteratorID'] . '" ';
- $attr_iterator = ' iterator="Continue" ';
- $last = $this->_quickbooks_get_current_run($user, $action);
- }
- $dr = '';
- if(is_array($extra) && isset($extra['new']) && $extra['new'] && isset($extra['timestamp_from']) && !empty($extra['timestamp_from'])){
- //$dr = '<FromModifiedDate>' . $last . '</FromModifiedDate>';
- $timestamp_from = $extra['timestamp_from'];
- //$timestamp_from = '2020-12-28T09:04:30';
- $dr = '<FromModifiedDate>' . $timestamp_from . '</FromModifiedDate>';
- }
- // Build the request
- $xml = '<?xml version="1.0" encoding="utf-8"?>
- <?qbxml version="' . $version . '"?>
- <QBXML>
- <QBXMLMsgsRq onError="'.$this->getonError().'">
- <InvoiceQueryRq ' . $attr_iterator . ' ' . $attr_iteratorID . ' requestID="' . $requestID . '">
- <MaxReturned>' . QB_QUICKBOOKS_MAX_RETURNED . '</MaxReturned>
- <ModifiedDateRangeFilter>
- <FromModifiedDate>' . $timestamp_from . '</FromModifiedDate>
- </ModifiedDateRangeFilter>
- <IncludeLineItems>1</IncludeLineItems>
- <IncludeLinkedTxns>1</IncludeLinkedTxns>
- <OwnerID>0</OwnerID>
- </InvoiceQueryRq>
- </QBXMLMsgsRq>
- </QBXML>';
- return $xml;
- }
- /**
- * @author M.E.
- */
- public function Invoice_Import_Response( $requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents ) {
- if(!$this->is_qwc_connected()){
- return false;
- }
- $is_only_new = false;
- if(is_array($extra) && isset($extra['new']) && $extra['new']){
- $is_only_new = true;
- }
- //$this->add_test_log($xml);
- //$this->add_test_log(print_r($idents,true));
- global $wpdb;
- if (!empty($idents['iteratorRemainingCount'])){
- $Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
- if($is_only_new){
- $Queue->enqueue('New_'.QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_REFRESH_DATA_IMPORT, array( 'iteratorID' => $idents['iteratorID'], 'new'=>$extra['new'], 'timestamp_from'=>$extra['timestamp_from'] ),$this->get_qbun());
- }else{
- $Queue->enqueue(QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_REFRESH_DATA_IMPORT, array( 'iteratorID' => $idents['iteratorID'] ),$this->get_qbun());
- }
- }
- $iteratorID = (isset($idents['iteratorID']) && !empty($idents['iteratorID']))?$idents['iteratorID']:'';
- $errnum = 0;
- $errmsg = '';
- $Parser = new QuickBooks_XML_Parser($xml);
- $tot_imported = 0;
- if ($Doc = $Parser->parse($errnum, $errmsg)){
- $Root = $Doc->getRoot();
- $List = $Root->getChildAt('QBXML/QBXMLMsgsRs/InvoiceQueryRs');
- /*
- $table = $wpdb->prefix.'mw_wc_qbo_desk_qbd_customers';
- /*Delete Old Data*
- if(!$is_only_new && $this->get_session_val('import_customer_iteratorID','',true)!=$iteratorID){
- $wpdb->query("DELETE FROM {$table} WHERE `id` > 0");
- $wpdb->query("TRUNCATE TABLE {$table}");
- }
- */
- /**
- * @author M.E.
- */
- if ( class_exists( 'Child_MWQB' ) ) {
- $Child_MWQB = new Child_MWQB();
- $Child_MWQB->new_invoice_response( $List );
- }
- /* End @author M.E. */
- //
- $this->set_session_val('import_invoice_iteratorID',$iteratorID);
- }
- if($tot_imported>0){
- $log_title = "Recognized Invoice Successfully";
- $details = "Total Invoices Recognized:{$tot_imported}";
- $status = 1;
- //Clear Invalid Mappings
- /*
- if (!$is_only_new && empty($idents['iteratorRemainingCount'])){
- $this->clear_customer_invalid_mappings();
- }
- */
- }else{
- $log_title = "Recognized Invoice Error";
- $errmsg = (!empty($errmsg))?$errmsg:"No Invoice Found";
- $details = ($errnum)?"Error Number:{$errnum}\n"."Error:{$errmsg}":"Error:{$errmsg}";
- $status = 0;
- }
- if(!$is_only_new || ($is_only_new && $status)){
- $this->save_log(array('log_type'=>'Invoice','log_title'=>$log_title,'details'=>$details,'status'=>$status),true);
- }
- return true;
- }
- //Within that new_invoice_response(), it looks for a tracking number and if so adds it to the woocommerce shipping tracking plugin.
- /**
- * Invoice Response
- */
- public function new_invoice_response( $List ) {
- //$listID = $list->getChildDataAt('InvoiceRet ListID');
- foreach ( $List->children() as $invoice ) {
- $txnDate = $invoice->getChildDataAt( 'InvoiceRet TxnDate');
- $invoice_number = $invoice->getChildDataAt( 'InvoiceRet RefNumber' );
- //$customerListID = $invoice->getChildDataAt( 'InvoiceRet CustomerRef ListID' );
- $so_number = $invoice->getChildDataAt( 'InvoiceRet LinkedTxn RefNumber' );
- //$web_order_id = $invoice->getChildDataAt( 'InvoiceRet Memo' );
- if ( empty( $so_number ) ) {
- continue;
- }
- $ship_date = $invoice->getChildDataAt( 'InvoiceRet ShipDate' );
- $carrier = '';
- foreach ( $invoice->children() as $extras ) {
- if ( 'DataExtRet' === $extras->name() ) {
- if ( 'Carrier' === $extras->getChildDataAt( 'DataExtRet DataExtName' ) ) {
- $carrier = $extras->getChildDataAt( 'DataExtRet DataExtValue' );
- break;
- }
- }
- }
- $method = $invoice->getChildDataAt( 'InvoiceRet ShipMethodRef FullName' );
- $tracking_number = '';
- foreach ( $invoice->children() as $extras ) {
- if ( 'DataExtRet' === $extras->name() ) {
- if ( 'Tracking Number' === $extras->getChildDataAt( 'DataExtRet DataExtName' ) ) {
- $tracking_number = $extras->getChildDataAt( 'DataExtRet DataExtValue' );
- break;
- }
- }
- }
- // Invoice Tracking
- if ( ! empty( $tracking_number ) ) {
- if ( ! $this->get_tracked_invoice( $invoice_number ) ) {
- $invoice_date = $txnDate;
- $this->add_invoice_tracking( $invoice_number, $invoice_date, sanitize_title( $carrier ), $tracking_number );
- }
- }
- $orders = wc_get_orders( array(
- 'limit' => 1,
- 'meta_key' => '_so_number',
- 'meta_value' => $so_number
- ));
- if ( $orders ) {
- foreach ( $orders as $order ) {
- if ( $order->get_meta( '_invoice_number' ) !== $invoice_number ) {
- $order->update_meta_data( '_invoice_number', $invoice_number );
- $order->add_order_note( __( 'Invoice Order #: ' . $invoice_number, 'woocommerce' ) );
- $order->set_status( 'completed' );
- }
- if ( empty( $order->get_meta( '_tracking_number' ) ) ) {
- if ( ! empty( $ship_date ) && ! empty( $carrier ) && ! empty( $method ) && ! empty( $tracking_number ) ) {
- $date = explode( '-', $ship_date );
- $timestamp = mktime( 0, 0, 0, $date[1], $date[2], $date[0] );
- $ship_date = date( 'Y-F-d', $timestamp );
- $meta_ship_date = date( 'n/j/Y', $timestamp );
- $order->update_meta_data( '_ship_date', $meta_ship_date );
- $order->update_meta_data( '_ship_carrier', $carrier );
- $order->update_meta_data( '_ship_method', $method );
- $order->update_meta_data( '_tracking_number', $tracking_number );
- $tracking = WC_Shipment_Tracking();
- $has_trackings = $tracking->actions->get_tracking_items( $order->get_id() );
- if ( ! empty( $has_trackings ) ) {
- foreach ( $has_trackings as $has_tracking ) {
- if ( $has_tracking['tracking_number'] != $tracking_number ) {
- $tracking->actions->add_tracking_item( $order->get_id(), array(
- 'tracking_provider' => sanitize_title( $carrier ),
- 'tracking_number' => $tracking_number,
- 'date-shipped' => $ship_date
- ));
- $order->add_order_note( __( 'Added ' . $carrier . ' Tracking #: ' . $tracking_number, 'woocommerce' ) );
- }
- }
- } else {
- $tracking->actions->add_tracking_item( $order->get_id(), array(
- 'tracking_provider' => sanitize_title( $carrier ),
- 'tracking_number' => $tracking_number,
- 'date-shipped' => $ship_date
- ));
- $order->add_order_note( __( 'Added ' . $carrier . ' Tracking #: ' . $tracking_number, 'woocommerce' ) );
- }
- }
- }
- $order->save();
- }
- }
- }
- }
- // That was about it for the tracking code used to achieve this. Like I said, I'm re-working my client's website and am hoping to remove some of these after-the-fact customizations.
- Thanks,
- Matt Ediger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement