swgdesign
asked on
no data returned posting to API
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.
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');
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER