Skip to content

Commit

Permalink
Merge pull request #34 from wuunder/development
Browse files Browse the repository at this point in the history
added value and weight to rest api endpoint orders
  • Loading branch information
timoj authored Oct 11, 2019
2 parents 4a56d37 + 83b4c93 commit d847a3d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Released

## [2.7.7](https://github.com/kabisa/wuunder-webshopplugin-woocommerce/tag/2.7.7) - 2019-10-11

### Added
- Support for value and weight via mywuunder import (REST API)


## [2.7.6](https://github.com/kabisa/wuunder-webshopplugin-woocommerce/tag/2.7.6) - 2019-10-07

### Added
Expand Down
142 changes: 99 additions & 43 deletions includes/checkout.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php
if ( !defined('ABSPATH') ) {
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly

add_action('wp_enqueue_scripts', 'wcwp_callback_for_setting_up_scripts');
// add_action('woocommerce_review_order_before_submit', 'parcelshop_html');
add_action('woocommerce_review_order_after_submit', 'wcwp_parcelshop_html');

function wcwp_callback_for_setting_up_scripts() {
if ( class_exists('WC_wuunder_parcelshop' ) ) {
$style_file_parcelshop_locator = dirname ( plugin_dir_url( __FILE__ ) ) . '/assets/css/parcelshop.css';
$style_file_checkout_fields = dirname ( plugin_dir_url( __FILE__ ) ) . '/assets/css/wuunder-checkout.css';
$google_api_key = get_option( 'wc_wuunder_google_maps_api_key' );
function wcwp_callback_for_setting_up_scripts()
{
if (class_exists('WC_wuunder_parcelshop')) {
$style_file_parcelshop_locator = dirname(plugin_dir_url(__FILE__)) . '/assets/css/parcelshop.css';
$style_file_checkout_fields = dirname(plugin_dir_url(__FILE__)) . '/assets/css/wuunder-checkout.css';
$google_api_key = get_option('wc_wuunder_google_maps_api_key');
$script_file = '//maps.googleapis.com/maps/api/js?key=' . $google_api_key;
wp_register_style( 'wuunderCSSParcelshopLocator', $style_file_parcelshop_locator );
wp_enqueue_style( 'wuunderCSSParcelshopLocator' );
wp_register_style( 'wuunderCSSCheckout', $style_file_checkout_fields );
wp_enqueue_style( 'wuunderCSSCheckout' );
wp_register_style('wuunderCSSParcelshopLocator', $style_file_parcelshop_locator);
wp_enqueue_style('wuunderCSSParcelshopLocator');
wp_register_style('wuunderCSSCheckout', $style_file_checkout_fields);
wp_enqueue_style('wuunderCSSCheckout');

wp_register_script( 'googleMapsJS', $script_file );
wp_enqueue_script( 'googleMapsJS' );
wp_register_script('googleMapsJS', $script_file);
wp_enqueue_script('googleMapsJS');
}
}

Expand All @@ -35,7 +36,7 @@ function wcwp_parcelshop_html()
$baseApiUrl = substr($tmpEnvironment->getStageBaseUrl(), 0, -3);
$carrierConfigList = get_option('woocommerce_wuunder_parcelshop_settings')['select_carriers'] ? get_option('woocommerce_wuunder_parcelshop_settings')['select_carriers'] : [];
$carrierList = implode(',', $carrierConfigList);
if ( 0 !== strlen($carrierList) ) {
if (0 !== strlen($carrierList)) {
$availableCarriers = $carrierList;
} else {
$defaultCarrierConfig = get_option('default_carrier_list') ? get_option('default_carrier_list') : [];
Expand All @@ -59,45 +60,48 @@ function wcwp_parcelshop_html()
* @param $shipping_method
* @return
*/
function wcwp_get_filter_from_shippingmethod($shipping_method ) {
if ( false !== strpos( $shipping_method, ':' ) ) {
$shipping_method = explode( ':', $shipping_method )[0];
function wcwp_get_filter_from_shippingmethod($shipping_method)
{
if (false !== strpos($shipping_method, ':')) {
$shipping_method = explode(':', $shipping_method)[0];
}
if ( $shipping_method === get_option( 'wc_wuunder_mapping_method_1' ) ) {
return get_option( 'wc_wuunder_mapping_filter_1' );
} elseif ( $shipping_method === get_option( 'wc_wuunder_mapping_method_2' ) ) {
return get_option( 'wc_wuunder_mapping_filter_2' );
} elseif ( $shipping_method === get_option( 'wc_wuunder_mapping_method_3') ) {
return get_option( 'wc_wuunder_mapping_filter_3' );
} elseif ( $shipping_method === get_option( 'wc_wuunder_mapping_method_4' ) ) {
return get_option( 'wc_wuunder_mapping_filter_4' );
if ($shipping_method === get_option('wc_wuunder_mapping_method_1')) {
return get_option('wc_wuunder_mapping_filter_1');
} elseif ($shipping_method === get_option('wc_wuunder_mapping_method_2')) {
return get_option('wc_wuunder_mapping_filter_2');
} elseif ($shipping_method === get_option('wc_wuunder_mapping_method_3')) {
return get_option('wc_wuunder_mapping_filter_3');
} elseif ($shipping_method === get_option('wc_wuunder_mapping_method_4')) {
return get_option('wc_wuunder_mapping_filter_4');
} else {
return '';
}
}

// Field added for the parcelshop_id, so that it can be requested from backend
add_action( 'woocommerce_after_order_notes', 'wcwp_add_parcelshop_id_field' );
function wcwp_add_parcelshop_id_field($checkout ) {
add_action('woocommerce_after_order_notes', 'wcwp_add_parcelshop_id_field');
function wcwp_add_parcelshop_id_field($checkout)
{
woocommerce_form_field('parcelshop_id', array(
'type' => 'text',
'class' => array(
'wuunder-hidden-checkout-field form-row-wide'
),
), $checkout->get_value( 'parcelshop_id' ) );
), $checkout->get_value('parcelshop_id'));

woocommerce_form_field('parcelshop_country', array(
'type' => 'text',
'class' => array(
'wuunder-hidden-checkout-field form-row-wide'
),
), $checkout->get_value( 'parcelshop_country' ) );
), $checkout->get_value('parcelshop_country'));
}

/*
* Add a referanse field to the Order API response.
*/
function prefix_wc_rest_prepare_order_object( $response, $object, $request ) {
function prefix_wc_rest_prepare_order_object($response, $object, $request)
{
$shipping_method_id = null;
try {
$order = new WC_Order($object->get_id());
Expand All @@ -108,40 +112,92 @@ function prefix_wc_rest_prepare_order_object( $response, $object, $request ) {
}
$response->data['wuunder_preferred_service_level'] = $shipping_method_id;

$bookingTokenData = get_post_meta( $object->get_id(), '_wuunder_label_booking_token' );
$bookingTokenData = get_post_meta($object->get_id(), '_wuunder_label_booking_token');

if (count($bookingTokenData)) {
$bookingToken = $bookingTokenData[0];
} else {
$bookingToken = uniqid();
update_post_meta( $object->get_id(), '_wuunder_label_booking_token', $bookingToken );
update_post_meta($object->get_id(), '_wuunder_label_booking_token', $bookingToken);
}

$response->data['wuunder_booking_token'] = $bookingToken;


$total_order_weight = wcwp_get_order_weight($object->get_id());
$response->data['wuunder_total_order_weight'] = $total_order_weight;


return $response;
}
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3 );

add_filter('woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3);

function wcwp_get_order_weight($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
//global $_product;
$items = $order->get_items();
$total_weight = 0;

if (sizeof($items) > 0) {
foreach ($items as $item) {
// Create the product
$product = $order->get_product_from_item($item);
// Set item weight
$weight = $product->get_weight();
$weight_unit = get_option('woocommerce_weight_unit');
$quantity = $item['qty'];
switch ($weight_unit) {
case 'kg':
$data['weight'] = $weight * 1000;
break;
case 'g':
$data['weight'] = $weight;
break;
case 'lbs':
$data['weight'] = $weight * 0.45359237;
break;
case 'oz':
$data['weight'] = $weight * 0.0283495231;
break;
default:
$data['weight'] = $weight;
break;
}

$total_product_weight = $quantity * $data['weight'];
$total_weight += $total_product_weight;
}
}

return $total_weight;
}


// Save / Send the parcelshop id
add_action( 'woocommerce_checkout_update_order_meta', 'wcwp_update_parcelshop_id' );
function wcwp_update_parcelshop_id( $order_id ) {
if ( ! empty( $_POST['parcelshop_id'] ) ) {
update_post_meta( $order_id, 'parcelshop_id', sanitize_text_field( $_POST['parcelshop_id'] ) );
add_action('woocommerce_checkout_update_order_meta', 'wcwp_update_parcelshop_id');
function wcwp_update_parcelshop_id($order_id)
{
if (!empty($_POST['parcelshop_id'])) {
update_post_meta($order_id, 'parcelshop_id', sanitize_text_field($_POST['parcelshop_id']));
}
}

// Check to see if a parcelshop is selected when parcel method is selected && Check if shipping country == parcelshop country
add_action( 'woocommerce_checkout_process', 'wcwp_check_parcelshop_selection' );
function wcwp_check_parcelshop_selection() {
if ( 'wuunder_parcelshop' === $_POST['shipping_method'][0] ) {
if ( !$_POST['parcelshop_id'] ) {
wc_add_notice( __( 'Kies eerst een <strong>parcelshop</strong>' ), 'error' );
add_action('woocommerce_checkout_process', 'wcwp_check_parcelshop_selection');
function wcwp_check_parcelshop_selection()
{
if ('wuunder_parcelshop' === $_POST['shipping_method'][0]) {
if (!$_POST['parcelshop_id']) {
wc_add_notice(__('Kies eerst een <strong>parcelshop</strong>'), 'error');
}

if ( $_POST['shipping_country'] != $_POST['parcelshop_country'] ) {
wc_add_notice( __( 'Het <strong>land van de verzendgegevens</strong> moet overeenkomen met het <strong>land van de parcelshop</strong> '), 'error' );
if ($_POST['shipping_country'] != $_POST['parcelshop_country']) {
wc_add_notice(__('Het <strong>land van de verzendgegevens</strong> moet overeenkomen met het <strong>land van de parcelshop</strong> '), 'error');
}
}
}

?>
2 changes: 1 addition & 1 deletion includes/wcwuunder-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct() {
$this->version_obj = array(
'product' => 'Woocommerce extension',
'version' => array(
'build' => '2.7.6 ',
'build' => '2.7.7 ',
'plugin' => '2.0' ),
'platform' => array(
'name' => 'Woocommerce',
Expand Down
4 changes: 2 additions & 2 deletions woocommerce-wuunder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WooCommerce Wuunder
* Plugin URI: https://wearewuunder.com/wuunder-voor-webshops/
* Description: Wuunder shipping plugin
* Version: 2.7.6
* Version: 2.7.7
* Author: Wuunder
* Author URI: http://wearewuunder.com
*/
Expand Down Expand Up @@ -57,7 +57,7 @@ class Woocommerce_Wuunder {
public static $plugin_path;
public static $plugin_basename;

const VERSION = '2.7.6';
const VERSION = '2.7.7';

public function __construct() {

Expand Down

0 comments on commit d847a3d

Please sign in to comment.