Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tig-robertgrundeken committed Mar 9, 2020
2 parents 15d7e6a + 48b3d87 commit 3826219
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 70 deletions.
23 changes: 23 additions & 0 deletions assets/js/admin/gls_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
return;
}

// Toggle free shipping fields (via ajax request)
$(document).on("change", '#tig_glstig_gls_freeshipping_enabled', function() {
toggleFreeShippingFields($(this).val());
});

$(document).on("click", '.wc-shipping-zone-method-settings', function() {
toggleFreeShippingFields($('#tig_glstig_gls_freeshipping_enabled').val());
});

function toggleFreeShippingFields(value) {
$("#tig_glstig_gls_freeshipping").parent().parent().parent().css('display','none');
$("#tig_glstig_gls_freeshipping_extra").parent().parent().parent().parent().css('display','none');

if (value == 1 ) {
$("#tig_glstig_gls_freeshipping").parent().parent().parent().css('display','table-row');
$("#tig_glstig_gls_freeshipping_extra").parent().parent().parent().parent().css('display','table-row');
}
}

if ($("#tig_glstig_gls_freeshipping_enabled").length) {
toggleFreeShippingFields($("#tig_glstig_gls_freeshipping_enabled").val());
}

var is_blocked = function ($node) {
return $node.is('.processing') || $node.parents('.processing').length;
};
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"type": "woocommerce-plugin",
"license": "CC-BY-NC-ND-3.0",
"version": "1.1.3",
"version": "1.2.0",
"authors": [
{
"name": "TIG",
Expand Down
2 changes: 1 addition & 1 deletion gls-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: GLS for WooCommerce
* Plugin URI: https://gls-group.eu/NL/nl/home
* Description: GLS offers shipping solutions nationally and internationally in Europe and worldwide. By using this plugin you can integrate GLS shipping methods in WooCommerce.
* Version: 1.1.3
* Version: 1.2.0
* Author: TIG
* Author URI: https://tig.nl/
* License: GPL2v2 or later
Expand Down
1 change: 1 addition & 0 deletions includes/admin/class-gls-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function includes()
include_once dirname(__FILE__) . '/class-gls-admin-assets.php';
include_once dirname(__FILE__) . '/class-gls-admin-meta-boxes.php';
include_once dirname(__FILE__) . '/class-gls-admin-api-notice.php';
include_once dirname(__FILE__) . '/settings/class-gls-settings-woocommerce-shipping-instance.php';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ public function get_gls_shipping_methods($methods, $zone)
if (!empty($methods)) {
foreach ($methods as $method) {
if (get_class($method) == 'GLS_Shipping_Method' ) {
$enabled_string = ($method->enabled == 'yes') ? __('active', 'gls_woocommerce') : __('deactive', 'gls_woocommerce');
$gls_shipping_method[] = '<a href="' . admin_url('admin.php?page=wc-settings&tab=shipping&zone_id=' . $zone) . '">' . $method->title . '</a> &nbsp;<strong>' . $enabled_string. '</strong>';
$enabled_string = ($method->enabled == 'yes') ? __('active', 'gls-woocommerce') : __('deactive', 'gls-woocommerce');
$freeshipping_string = ((int)$method->instance_settings['freeshipping_enabled'] > 0 && (float)$method->instance_settings['freeshipping'] > 0) ? sprintf(__('free shipping above %s %s (excl. tax) ', 'gls-woocommerce'), (float)$method->instance_settings['freeshipping'], get_option('woocommerce_currency')) : __('no free shipping', 'gls-woocommerce');
$gls_shipping_method[] = '<a href="' . admin_url('admin.php?page=wc-settings&tab=shipping&zone_id=' . $zone) . '">' . $method->title . '</a> &nbsp;<strong>' . $enabled_string. '</strong> &nbsp;(' . $freeshipping_string . ')';
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
*
* ..::..
* ..::::::::::::..
* ::'''''':''::'''''::
* ::.. ..: : ....::
* :::: ::: : : ::
* :::: ::: : ''' ::
* ::::..:::..::.....::
* ''::::::::::::''
* ''::''
*
*
* NOTICE OF LICENSE
*
* This source file is subject to the Creative Commons License.
* It is available through the world-wide-web at this URL:
* http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US
* If you are unable to obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact [email protected] for more information.
*
* @copyright Copyright (c) Total Internet Group B.V. https://tig.nl/copyright
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US
*/

defined( 'ABSPATH' ) || exit;

/**
* Class GLS_Settings_Woocommerce_Shipping_Instance
*/
class GLS_Settings_Woocommerce_Shipping_Instance
{
public function __construct()
{
// @formatter:off
add_filter('woocommerce_shipping_instance_form_fields_tig_gls', array(__CLASS__, 'filter_woocommerce_shipping_instance_form_fields_tig_gls'), 10, 1);
// @formatter:on
}

/**
* Add extra formfields to GLS Shipping options instance in WooCommerce Shipping Settings
* @param $form_fields
* @return mixed
*/
public function filter_woocommerce_shipping_instance_form_fields_tig_gls ($form_fields)
{

$form_field_freeshipping_enabled = array(
'title' => __('Enable free shipping','gls-woocommerce'),
'type' => 'select',
'description' => __('Enable free shipping above an order amount','gls-woocommerce'),
'desc_tip' => true,
'options' => array(0 => __('No','gls-woocommerce'), 1 => __('Yes','gls-woocommerce')),
'default' => '0'
);

$form_field_freeshipping = array(
'title' => __('Apply free shipping above order amount','gls-woocommerce'),
'type' => 'price',
'disabled' => false,
'description' => __('Enter the order amount (excl. tax) above (and equal) which free shipping will be applied.','gls-woocommerce'),
'desc_tip' => true,
'default' => '0'
);

$form_field_freeshipping_extra = array(
'title' => __('Apply free shipping to extra services','gls-woocommerce'),
'type' => 'checkbox',
'description' => __('Enable this when extra service costs are also free when free shipping applies.','gls-woocommerce'),
'default' => 'no',
'desc_tip' => true
);


$form_fields['freeshipping_enabled'] = $form_field_freeshipping_enabled;
$form_fields['freeshipping'] = $form_field_freeshipping;
$form_fields['freeshipping_extra'] = $form_field_freeshipping_extra;

return $form_fields;
}
}

new GLS_Settings_Woocommerce_Shipping_Instance();
9 changes: 8 additions & 1 deletion includes/api/label/class-gls-api-label-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public function setBody()
$labelType = $this->get_label_type();
$shipmentId = $order->ID;

// @todo fix correct email when stored. This fixes that sometimes emailaddresses are empty or incomplete
// which stops the label creation
if ($delivery_address['email'] != $order->get_billing_email()) {
$delivery_address['email'] = $order->get_billing_email();
$delivery_address['phone'] = $order->get_billing_phone();
}

$data = GLS_Api::add_shipping_information();
$data["services"] = $this->map_services($delivery_option['details'], $delivery_option['type'], $delivery_address['countryCode']);
$data["trackingLinkType"] = 'u';
Expand Down Expand Up @@ -126,7 +133,7 @@ public function setBody()
private function map_services($details, $type = null, $countryCode = 'NL')
{
$service = [
"shopReturnService" => (bool) ($this->options['shop_return'] && $countryCode == 'NL')
"shopReturnService" => (bool) ($this->options['shop_return'] == 'yes' && $countryCode == 'NL')
];
switch ($type) {
case 'ParcelShop':
Expand Down
93 changes: 92 additions & 1 deletion includes/class-gls-delivery-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,20 @@ function (&$option) use ($enabled_options) {
*/
private function additional_fee($option, $enabled_options)
{

$freeshipping = self::has_gls_freeshipping();
$freeshipping_extra = self::get_shipping_method_instance_setting('freeshipping_extra');
if ($freeshipping && $freeshipping_extra =="yes") {
return '';
}

$code = 'gls_' . strtolower($option->service);
$fee = isset($enabled_options[$code]) ? $enabled_options[$code]->additional_fee : '';

if ($fee < 0 && $freeshipping) {
return '';
}

return $fee;
}

Expand Down Expand Up @@ -389,20 +400,88 @@ public static function adjust_shipping_rate($rates){
return $rates;
}

// Free Shipping
$freeshipping = self::has_gls_freeshipping();
$freeshipping_extra = self::get_shipping_method_instance_setting('freeshipping_extra');

$service = $session->get('gls_service');
$details = $service['details'] ?? [];
$fee = $details['fee'] ?? '';

foreach ($rates as &$rate) {
if ($rate->get_method_id() == 'tig_gls') {
$rate->cost += (float) $fee;
if ($freeshipping) {
$rate->cost = 0;
}

if (!$freeshipping || $freeshipping_extra == "no") {
$rate->cost += (float)$fee;
}

if ($rate->cost <= 0) {
$rate->cost = 0;
$rate->label = $rate->label . __(' (free shipping)','gls-woocommerce');
}

$tax_array = WC_Tax::calc_tax($rate->cost, WC_Tax::get_rates(), false );

$rate->set_taxes($tax_array);
}
}
return $rates;
}

/**
* @return bool
*/
public static function has_gls_freeshipping()
{
$enabled = (int)self::get_shipping_method_instance_setting('freeshipping_enabled');

if (!$enabled) {
return false;
}

$freeshipping_amount = (float)self::get_shipping_method_instance_setting('freeshipping');

if ($freeshipping_amount > 0) {
$session = WC()->session;
$cart_totals = $session->get('cart_totals');
if ($freeshipping_amount <= ($cart_totals['subtotal'] + $cart_totals['subtotal_tax'])) {
return true;
}
}
return false;
}

/**
* @param $setting_name
* @return |null
*/
public static function get_shipping_method_instance_setting($setting_name)
{
$session = WC()->session;
$chosen_shipping_method = $session->get('chosen_shipping_methods');

$shipping_method_id = explode(':',$chosen_shipping_method[0]);
$current_shipping_methods = WC()->shipping->get_shipping_methods();
$current_shipping_method = $current_shipping_methods[$shipping_method_id[1]];

//fallback; manual init instance to get to config settings
if (!isset($current_shipping_method) && $chosen_shipping_method) {
$current_shipping_method = $current_shipping_methods[$shipping_method_id[0]];
$current_shipping_method->instance_id = $shipping_method_id[1];
$current_shipping_method->init_instance_settings();
}

//get instance setting
if ($current_shipping_method->instance_settings[$setting_name]) {
return $current_shipping_method->instance_settings[$setting_name];
}

return null;
}

/**
* @param $order
* @param $data
Expand Down Expand Up @@ -433,6 +512,18 @@ public function format_shop_delivery_fee()
return;
}

$freeshipping = self::has_gls_freeshipping();

//negative fee + free shipping means display no value
if ($additional_fee < 0 && $freeshipping) {
return;
}

$freeshipping_extra = self::get_shipping_method_instance_setting('freeshipping_extra');
if ($freeshipping && $freeshipping_extra == "yes") {
return;
}

$tax = WC_Tax::calc_tax((float) $additional_fee, WC_Tax::get_rates(), false);

return wc_price((float) reset($tax) + (float) $additional_fee);
Expand Down
8 changes: 6 additions & 2 deletions includes/class-gls.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class GLS
*
* @var string
*/
public $version = '1.1.3';
public $version = '1.2.0';

/**
* The single instance of the class.
Expand Down Expand Up @@ -305,7 +305,11 @@ public function ajax_url()
*/
public function post($key = null, $clean = true)
{
if (!$key) {
if (!$key && !$clean) {
return wp_unslash($_POST);
}

if (!$key && $clean) {
return wc_clean(wp_unslash($_POST));
}

Expand Down
Binary file modified languages/gls-woocommerce-nl_NL.mo
Binary file not shown.
Loading

0 comments on commit 3826219

Please sign in to comment.