Advertisement
sparkweb

FoxyShop: UPS WorldShip Integration

Oct 27th, 2011
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.53 KB | None | 0 0
  1. <?php
  2. //UPS WorldShip XML Integration
  3. //Put this code in your functions.php file
  4. //Note: This is for a single-ship store, not a multi-ship store
  5. if (isset($_GET['page'])) {
  6.     if ($_GET['page'] == 'foxyshop_order_management') add_action('foxyshop_order_search_buttons', 'my_ups_button');
  7. }
  8.  
  9. //write buttons to form
  10. function my_ups_button() {
  11.     ?>
  12.     <button class="button" style="margin-left: 15px;" name="my_ups_button" id="my_ups_button" type="submit">Export to UPS WorldShip</button>
  13.     <?php
  14. }
  15.  
  16. //set up handler
  17. if (isset($_GET['my_ups_button'])) add_action('admin_init', 'my_ups_custom_handler');
  18.  
  19. function my_ups_custom_handler() {
  20.     global $foxyshop_settings, $wp_version;
  21.    
  22.     //Setup Defaults
  23.     $description_of_goods = "Retail Goods";
  24.     $billing_option = "PP";
  25.     $package_type = "CP"; //CP = Customer Packaging
  26.     $default_service_type = "GND";
  27.     $default_residential_indicator = ""; //1 = res, 0 = biz. Leave Blank to key off company field
  28.     if (defined('FOXYSHOP_UPS_RESIDENTIAL_INDICATOR')) $default_residential_indicator = FOXYSHOP_UPS_RESIDENTIAL_INDICATOR;
  29.  
  30.     $services_types = array(
  31.         "UPS Express Plus or Worldwide Express Plus" => "EP",
  32.         "UPS Express or Worldwide Express" => "ES",
  33.         "UPS Express Saver or Worldwide Saver" => "1DP",
  34.         "UPS Expedited or Worldwide Expedited" => "EX",
  35.         "UPS Standard" => "ST",
  36.         "UPS 3 Day Select to the United States" => "3DS",
  37.         "UPS Express (NA1)" => "ND",
  38.         "UPS Next Day Air Early AM" => "1DM",
  39.         "UPS Next Day Air" => "1DA",
  40.         "UPS Next Day Air Saver" => "1DP",
  41.         "UPS 2nd Day Air AM" => "2DM",
  42.         "UPS 2nd Day Air" => "2DA",
  43.         "UPS 3 Day Select" => "3DS",
  44.         "UPS Ground" => "GND",
  45.         "UPS Worldwide Express Plus" => "ES",
  46.         "UPS Worldwide Express" => "SV",
  47.         "UPS Worldwide Saver (Express)" => "SV",
  48.         "UPS Worldwide Expedited" => "EX"
  49.     );
  50.  
  51.     //Setup Fields and Defaults
  52.     $foxy_data_defaults = array(
  53.         "is_test_filter" => "0",
  54.         "hide_transaction_filter" => "0",
  55.         "data_is_fed_filter" => "",
  56.         "id_filter" => "",
  57.         "order_total_filter" => "",
  58.         "coupon_code_filter" => "",
  59.         "transaction_date_filter_begin" => date("Y-m-d", strtotime("-10 days")),
  60.         "transaction_date_filter_end" => date("Y-m-d"),
  61.         "customer_id_filter" => "",
  62.         "customer_email_filter" => "",
  63.         "customer_first_name_filter" => "",
  64.         "customer_last_name_filter" => "",
  65.         "customer_state_filter" => "",
  66.         "shipping_state_filter" => "",
  67.         "customer_ip_filter" => "",
  68.         "product_code_filter" => "",
  69.         "product_name_filter" => "",
  70.         "product_option_name_filter" => "",
  71.         "product_option_value_filter" => ""
  72.     );
  73.     $foxy_data = wp_parse_args(array("api_action" => "transaction_list"), $foxy_data_defaults);
  74.    
  75.     if (isset($_GET['foxyshop_search'])) {
  76.         $fields = array("is_test_filter", "hide_transaction_filter", "data_is_fed_filter", "id_filter", "order_total_filter", "coupon_code_filter", "transaction_date_filter_begin", "transaction_date_filter_end", "customer_id_filter", "customer_email_filter", "customer_first_name_filter", "customer_last_name_filter","customer_state_filter", "shipping_state_filter", "customer_ip_filter", "product_code_filter", "product_name_filter", "product_option_name_filter", "product_option_value_filter");
  77.         foreach ($fields as $field) {
  78.             if (isset($_GET[$field])) {
  79.                 $foxy_data[$field] = $_GET[$field];
  80.             }
  81.         }
  82.         $foxy_data['pagination_start'] = (isset($_GET['pagination_start']) ? $_GET['pagination_start'] : 0);
  83.         if ($foxyshop_settings['version'] != "0.7.0") $foxy_data['entries_per_page'] = 50;
  84.     }  
  85.  
  86.     $foxy_response = foxyshop_get_foxycart_data($foxy_data);
  87.     $xml_return = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
  88.     if ((string)$xml_return->result == "ERROR") {
  89.         echo '<h3>' . $xml_return->messages->message . '</h3>';
  90.         die;
  91.     }
  92.  
  93.     // Define the path to file
  94.     $filename = 'ups-worldship-'.Date('d-m-Y').'.xml';
  95.  
  96.     // Set headers
  97.     header("Cache-Control: public");
  98.     header("Content-Description: File Transfer");
  99.     header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
  100.     header("Content-Type: text/xml");
  101.     header("Content-Transfer-Encoding: binary");
  102.  
  103.     $xml = '<?xml version="1.0" encoding="windows-1252"?>'."\n";
  104.     $xml .= '<OpenShipments xmlns="x-schema:OpenShipments.xdr">'."\n";
  105.  
  106.     foreach($xml_return->transactions->transaction as $transaction) {
  107.  
  108.         $shipping_first_name = ((string)$transaction->shipping_first_name != "" ? (string)$transaction->shipping_first_name : (string)$transaction->customer_first_name);
  109.         $shipping_last_name = ((string)$transaction->shipping_last_name != "" ? (string)$transaction->shipping_last_name : (string)$transaction->customer_last_name);
  110.         $shipping_company = ((string)$transaction->shipping_company != "" ? (string)$transaction->shipping_company : (string)$transaction->customer_company);
  111.         $shipping_address1 = ((string)$transaction->shipping_address1 != "" ? (string)$transaction->shipping_address1 : (string)$transaction->customer_address1);
  112.         $shipping_address2 = ((string)$transaction->shipping_address2 != "" ? (string)$transaction->shipping_address2 : (string)$transaction->customer_address2);
  113.         $shipping_city = ((string)$transaction->shipping_city != "" ? (string)$transaction->shipping_city : (string)$transaction->customer_city);
  114.         $shipping_state = ((string)$transaction->shipping_state != "" ? (string)$transaction->shipping_state : (string)$transaction->customer_state);
  115.         $shipping_postal_code = ((string)$transaction->shipping_postal_code != "" ? (string)$transaction->shipping_postal_code : (string)$transaction->customer_postal_code);
  116.         $shipping_country = ((string)$transaction->shipping_country != "" ? (string)$transaction->shipping_country : (string)$transaction->customer_country);
  117.         $shipping_phone = ((string)$transaction->shipping_phone != "" ? (string)$transaction->shipping_phone : (string)$transaction->customer_phone);
  118.         $shipping_phone = preg_replace("/[^0-9]/","", $shipping_phone); //Strip Non-Numberic Characters
  119.         $shipping_phone = apply_filters('foxyshop_ups_phone', $shipping_phone, $transaction);
  120.         $customer_email = (string)$transaction->customer_email;
  121.         $customer_id = (string)$transaction->customer_id;
  122.         $transaction_id = (string)$transaction->id;
  123.         $shipto_shipping_service_description = (string)$transaction->shipto_shipping_service_description;
  124.         $shipping_name = $shipping_company;
  125.         if ($shipping_company) {
  126.             $shipping_attn = $shipping_first_name . ' ' . $shipping_last_name;
  127.         } else {
  128.             $shipping_company = $shipping_first_name . ' ' . $shipping_last_name;
  129.             $shipping_attn = "";
  130.         }
  131.        
  132.         //Get Service Type
  133.         $service_type = $default_service_type;
  134.         if (isset($services_types[$shipto_shipping_service_description])) $service_type = $services_types[$shipto_shipping_service_description];
  135.  
  136.         //Get Weight
  137.         $product_weight = 0;
  138.         foreach($transaction->transaction_details->transaction_detail as $transaction_detail) {
  139.             $product_weight += (double)$transaction_detail->product_weight;
  140.             foreach($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
  141.                 $product_weight += (double)$transaction_detail_option->weight_mod;
  142.             }
  143.         }
  144.        
  145.         //Residential Indicator
  146.         if ($default_residential_indicator != "") {
  147.             $residential_indicator = $default_residential_indicator;
  148.         } else {
  149.             $residential_indicator = $shipping_attn != "" ? 0 : 1;
  150.         }
  151.  
  152.         $xml .= "\t".'<OpenShipment ShipmentOption="" ProcessStatus="">'."\n";
  153.         $xml .= "\t\t".'<ShipTo>'."\n";
  154.         $xml .= "\t\t\t".'<CustomerID>' . $customer_id . '</CustomerID>'."\n";
  155.         $xml .= "\t\t\t".'<CompanyOrName>' . $shipping_company . '</CompanyOrName>'."\n";
  156.         $xml .= "\t\t\t".'<Attention>' . $shipping_attn . '</Attention>'."\n";
  157.         $xml .= "\t\t\t".'<Address1>' . $shipping_address1 . '</Address1>'."\n";
  158.         if ($shipping_address2) $xml .= "\t\t\t".'<Address2>' . $shipping_address2 . '</Address2>'."\n";
  159.         $xml .= "\t\t\t".'<CityOrTown>' . $shipping_city . '</CityOrTown>'."\n";
  160.         $xml .= "\t\t\t".'<CountryTerritory>' . $shipping_country . '</CountryTerritory>'."\n";
  161.         $xml .= "\t\t\t".'<PostalCode>' . $shipping_postal_code . '</PostalCode>'."\n";
  162.         $xml .= "\t\t\t".'<StateProvinceCounty>' . $shipping_state . '</StateProvinceCounty>'."\n";
  163.         if ($shipping_phone) $xml .= "\t\t\t".'<Telephone>' . $shipping_phone . '</Telephone>'."\n";
  164.         $xml .= "\t\t\t".'<EmailAddress>' . $customer_email . '</EmailAddress>'."\n";
  165.         $xml .= "\t\t\t".'<ResidentialIndicator>' . apply_filters('foxyshop_ups_residential', $residential_indicator, $transaction) . '</ResidentialIndicator>'."\n";
  166.         $xml .= "\t\t".'</ShipTo>'."\n";
  167.         $xml .= "\t\t".'<ShipmentInformation>'."\n";
  168.         $xml .= "\t\t\t".'<ServiceType>' . $service_type . '</ServiceType>'."\n";
  169.         $xml .= "\t\t\t".'<NumberOfPackages>' . apply_filters('foxyshop_ups_package_number', 1, $transaction) . '</NumberOfPackages>'."\n";
  170.         $xml .= "\t\t\t".'<ShipmentActualWeight>' . $product_weight . '</ShipmentActualWeight>'."\n";
  171.         $xml .= "\t\t\t".'<DescriptionOfGoods>' . apply_filters('foxyshop_ups_goods_descript', $description_of_goods, $transaction) . '</DescriptionOfGoods>'."\n";
  172.         $xml .= "\t\t\t".'<BillingOption>' . apply_filters('foxyshop_ups_billing_option', $billing_option, $transaction) . '</BillingOption>'."\n";
  173.         $xml .= "\t\t".'</ShipmentInformation>'."\n";
  174.         $xml .= "\t\t".'<Package>'."\n";
  175.         $xml .= "\t\t\t".'<PackageType>' . apply_filters('foxyshop_ups_package_type', $package_type, $transaction) . '</PackageType>'."\n";
  176.         $xml .= "\t\t\t".'<Reference1>' . $transaction_id . '</Reference1>'."\n";
  177.         $xml .= "\t\t".'</Package>'."\n";
  178.         $xml .= "\t".'</OpenShipment>'."\n";
  179.     }
  180.     $xml .= '</OpenShipments>'."\n";
  181.     echo $xml;
  182.     die;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement