Skip to content

Instantly share code, notes, and snippets.

@yeriepiscesa
Created April 22, 2019 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeriepiscesa/12a708b9f1e05f8a3f18c5980eee82c4 to your computer and use it in GitHub Desktop.
Save yeriepiscesa/12a708b9f1e05f8a3f18c5980eee82c4 to your computer and use it in GitHub Desktop.
<?php
class SP_Ajax_CalculateOngkir_Action extends WP_AJAX {
protected $action = 'calculate-ongkir';
protected function run() {
if( $this->requestType( 'POST' ) ) {
$data_to_send = array(
'origin' => $_POST['origin'],
'destination' => $_POST['destination'],
'weight' => $_POST['weight'],
'courier' => $_POST['courier']
);
$remote = wp_remote_post( RAJAONGKIR_BASE_URI . '/cost', array(
'method' => 'POST',
'headers' => array(
'key' => RAJAONGKIR_API_KEY,
'content-type' => 'application/x-www-form-urlencoded'
),
'body' => $data_to_send
) );
$data = array();
if( $remote['response']['code'] == 200 ) {
$remote_body = json_decode( $remote['body'] );
$rows = $remote_body->rajaongkir->results;
foreach( $rows as $row ) {
$data['name'] = $row->name;
$data['costs'] = array();
foreach( $row->costs as $cost ) {
array_push( $data[ 'costs' ], array(
'service' => $cost->service,
'description' => $cost->description,
'cost' => $cost->cost[0]->value,
'etd' => $cost->cost[0]->etd
) );
}
}
}
echo json_encode( $data, true );
}
}
}
SP_Ajax_CalculateOngkir_Action::listen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment