Solved

no data returned posting to API

Posted on 2014-02-13
2
180 Views
Last Modified: 2015-07-09
I have a Wordpress plugin that posts to a custom shipping API in Australia.

Now recently the API has been updated to .Net MVC and the plugin code no longer works, even though I have updated the URL used etc. Could the params be the issue due to the fact that they're using MVC and the actual url structure has changed?

Website: http://www.portdouglasgiftbaskets.com.au/ - just add a product and view the basket

Here's a working URL on the API:

http://farmapi.fastway.org/v2/psc/lookup/AUK/Elsthorpe/4110/5?api_key=YOUR_API_KEY


I would love it if someone could tell me what is wrong as I am not a PHP developer.

<?php
/*
  Plugin Name: FastWay Courier Shipping Rates for WooCommerce
  Plugin URI: http://www.patsatech.com
  Description: FastWay Courier Shipping Rates based on Weight for WooCommerce
  Version: 1.0
  Author: PatSaTECH
  Author URI: http://www.patsatech.com
  Copyright: © 2008-2012 PatSaTECH.
  License: GNU General Public License v3.0
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

add_action('plugins_loaded', 'woocommerce_cust_init', 0);

function woocommerce_cust_init() {

    if (!class_exists('WC_Shipping_Method'))
        return;

function Unset_AUStates( $states ) {     
	unset($states["AU"]);
	//$states["AU"] = array();
	return $states; 
} 
add_filter( 'woocommerce_states', 'Unset_AUStates', 10, 1 );		
	 	

function Unset_AULocale( $states ) {     
	unset($states["AU"]);
	$states["AU"] = array(
					'state'		=> array(
						'label' 		=> __('Suburb', 'woocommerce'),
						'placeholder' 	=> __('Suburb', 'woocommerce'),
						'required' 		=> true
					));
	return $states; 
} 
add_filter( 'woocommerce_get_country_locale', 'Unset_AULocale', 10, 1 );	
		
// Our hooked in function - $fields is passed via the filter!
function Override_State_Fields( $fields ) {
     $fields['billing']['billing_suburb'] = array(
        'label'     => __('State', 'woocommerce'),
    'placeholder'   => _x('State', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );
     $fields['shipping']['shipping_suburb'] = array(
        'label'     => __('Suburb', 'woocommerce'),
    'placeholder'   => _x('Suburb', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );
     $fields['billing']['billing_state']['label'] = 'Suburb';
     $fields['billing']['billing_state']['placeholder'] = 'Suburb';
     $fields['shipping']['shipping_state']['label'] = 'Suburb';
     $fields['shipping']['shipping_state']['placeholder'] = 'Suburb';
     return $fields;
}

add_filter( 'woocommerce_checkout_fields' , 'Override_State_Fields' );

add_action('woocommerce_checkout_update_order_meta', 'Update_State_Fields');
 
function Update_State_Fields( $order_id ) {
    if ($_POST['shipping_suburb']) update_post_meta( $order_id, '_shipping_state', esc_attr($_POST['shipping_suburb']));
    if ($_POST['billing_suburb']) update_post_meta( $order_id, '_billing_state', esc_attr($_POST['billing_suburb']));
}

class WC_FastWay_Courier extends WC_Shipping_Method {
	
	function __construct() { 
		$this->id = 'FastWay_Courier';
		$this->method_title = __('FastWay Courier', 'woocommerce');
		$this->init();
	} 
	
    function init() {
		// Load the form fields.
		$this->init_form_fields();
		
		// Load the settings.
		$this->init_settings();
		
		// Define user set variables
		$this->enabled		= empty( $this->settings['enabled'] ) ? 'no' : $this->settings['enabled'];
		$this->title		= empty( $this->settings['title'] ) ? '' : $this->settings['title'];
		$this->api_key		= empty( $this->settings['api_key'] ) ? '' : $this->settings['api_key'];
		$this->free_postcode= empty( $this->settings['free_postcode'] ) ? '' : $this->settings['free_postcode'];
		$this->availability	= empty( $this->settings['availability'] ) ? '' : $this->settings['availability'];
		$this->countries	= empty( $this->settings['countries'] ) ? '' : $this->settings['countries'];	
		add_action('woocommerce_update_options_shipping_'.$this->id, array(&$this, 'process_admin_options'));
	}
	 
 
	 
	function calculate_shipping( $data = false ) {
		global $woocommerce;
		$shipping_total = 0;
		$api_key = $this->api_key;
		$free_postcode = $this->free_postcode;
		if(empty($free_postcode)){
			$free_postcode = '0000';
		}
		$free_postcode = explode(',',$free_postcode);

		//$woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
		 
        $customer = $woocommerce->customer;
		
		$weight = $woocommerce->cart->cart_contents_weight;
		
		$state = $customer->get_shipping_state();
		
		$code = $customer->get_shipping_postcode();
		
		//$rates = "http://farmapi.fastway.org/latest/psc/lookup/CNS/$state/$code/$weight?&api_key=$api_key";
		$params = array(
					  	"RFCode" => "CNS",
					  	"Suburb" => $state,
					  	"DestPostcode " => $code,
						"WeightInKg " => $weight,
						"api_key" => $api_key
					  );	
		$rate = $this->getrates($params);
		$shipping_total = $this->api_key;
		if(in_array($code, $free_postcode)){
			$freerate = array(
	    		'id' 	=> 'free_shipping',
	    		'label' => 'Free Shipping',
	    		'cost' 	=> 0,
	    		'taxes' => false
	    	);
	    	$this->add_rate( $freerate );
			
		}else{
			if(!isset($rate['error'])){
				$totweight = $rate["result"]['parcel_weight_kg'];
				foreach($rate['result']['services'] as $id => $value){		
				$rates = "";
				if($value['type'] == 'Satchel' && $totweight <= '2'){			
						$rates = array(
							'id' 		=> $value['type'],
							'label' 	=> $value['type'], 
							'cost' 		=> $value['labelprice_normal']
						);
						$this->add_rate($rates);
				}else if($value['type'] == 'Parcel' && $totweight > '2'){			
						$rates = array(
							'id' 		=> $value['type'],
							'label' 	=> $value['type'], 
							'cost' 		=> $value['labelprice_normal']
						);
						$this->add_rate($rates);
				}
					/*if($value['FranchiseName'] == 'Wollongong'){
						echo $value['FranchiseCode'];
					}*/
				}
			}else{
				//$woocommerce->add_error('Please enter the Suburb in State feild to Calculate the Shipping Correctly.');
				$woocommerce->add_error($rate['error']);
			}
		}
	}
	
	function init_form_fields() {
    	global $woocommerce;
    	$this->form_fields = array(
			'enabled' => array(
				'title' 		=> __( 'Enable', 'woocommerce' ), 
				'type' 			=> 'checkbox', 
				'label' 		=> __( 'Enable FastWay Courier', 'woocommerce' ), 
				'default' 		=> 'yes'
			), 
			'api_key' => array(
				'title' 		=> __( 'API Key', 'woocommerce' ), 
				'type' 			=> 'text', 
				'description' 	=> __( 'API reuired to get rates from FastWay.', 'woocommerce' ), 
				'default'		=> ''
			),
			'free_postcode' => array(
				'title' 		=> __( 'Free PostCodes', 'woocommerce' ), 
				'type' 			=> 'text', 
				'description' 	=> __( 'Seperate with commas(,). These PostCode will be shown a Free Shipping Option Only.', 'woocommerce' ), 
				'default'		=> ''
			),
			'availability' => array(
							'title' 		=> __( 'Method availability', 'woocommerce' ), 
							'type' 			=> 'select', 
							'default' 		=> 'all',
							'class'			=> 'availability',
							'options'		=> array(
								'all' 		=> __('All allowed countries', 'woocommerce'),
								'specific' 	=> __('Specific Countries', 'woocommerce')
							)
						),
			'countries' => array(
							'title' 		=> __( 'Specific Countries', 'woocommerce' ), 
							'type' 			=> 'multiselect', 
							'class'			=> 'chosen_select',
							'css'			=> 'width: 450px;',
							'default' 		=> '',
							'options'		=> $woocommerce->countries->countries
						)	
		);
	}

	function admin_options() {
		global $woocommerce; ?>
		<h3><?php echo $this->method_title; ?></h3>
		
		<table class="form-table">
    		<?php $this->generate_settings_html(); ?>
    	</table> <?php
	}

    function is_available( $package ) {
    	global $woocommerce;
    	
    	if ($this->enabled=="no") return false;
		
		// If post codes are listed, let's use them.
		$codes = '';
		if($this->codes != '') {
			foreach(explode(',',$this->codes) as $code) {
				$codes[] = $this->clean($code);
			}
		}
		
		if (is_array($codes))
			if ( ! in_array($this->clean( $package['destination']['postcode'] ), $codes))
				return false;
		
		// Either post codes not setup, or post codes are in array... so lefts check countries for backwards compatability.
		$ship_to_countries = '';
		if ($this->availability == 'specific') :
			$ship_to_countries = $this->countries;
		else :
			if (get_option('woocommerce_allowed_countries')=='specific') :
				$ship_to_countries = get_option('woocommerce_specific_allowed_countries');
			endif;
		endif; 

		if (is_array($ship_to_countries))
			if (!in_array( $package['destination']['country'] , $ship_to_countries))
				return false;
		
		// Yay! We passed!
		return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true );
    } 
    
    function clean($code) {
    	return str_replace('-','',sanitize_title($code));
    }
	
	function getrates($params){
	
		$url = "http://farmapi.fastway.org/v3/psc/lookup/";
		$curl = curl_init();		
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
		curl_setopt($curl, CURLOPT_USERAGENT, "Fastway API PHP Wrapper");
		$raw_result = curl_exec($curl);
		$result = json_decode($raw_result, true);	
		curl_close($curl);
		return $result;
	}
    
}
function add_fastway_courier_method($methods) { $methods[] = 'WC_FastWay_Courier'; return $methods; }
add_filter('woocommerce_shipping_methods','add_fastway_courier_method');
}

Open in new window

0
Comment
Question by:swgdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39855815
It's a little hard to research this because the plug-in appears to be very thinly used (Example: Google cannot find it at WordPress, and CNET has a lifetime download count of 139 and zero reviews).  But that said, it looks like it's a paid product, and the current version may be 1.5+?  According to the code snippet you've got version 1.0 so that may argue for an upgraded version of the plug-in.

Usually when a publisher has an API, the best practices call for a Versioned API, in other words, the publisher will not make a change to the inputs and outputs for any given version and will only make changes like that to a new version of the API.  So if your existing code suddenly broke, you have a support issue with the API vendor.

There is only one cURL call in the scripts and that is devoid of any error visualization.  I would be reluctant to try to talk you through how to get error displays if you're not at least a medium-advanced PHP programmer (too easy to break something), so my first recommendation is contact both the plugin vendor and Fastway.

Sidebar note: Fastway says they have an API, but there seem to be no links to it (?).
0
 

Author Closing Comment

by:swgdesign
ID: 40873874
I had to get the plugin re-coded by someone else as the original developer disappeared! :( Grrr
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The viewer will learn how to dynamically set the form action using jQuery.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question