Skip to content

Commit

Permalink
Version 2.5.5
Browse files Browse the repository at this point in the history
Version 2.5.5
  • Loading branch information
mntzrr committed Nov 28, 2023
2 parents d16aed6 + db3f96c commit 51d1ef9
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 607 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright, 2018, Wasa Kredit AB
Copyright, 2023, Wasa Kredit AB

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 9 additions & 3 deletions README.txt
Expand Up @@ -3,11 +3,11 @@ Contributors: aos06wasakredit
Donate link: https://developer.wasakredit.se
Tags: woocommerce, ecommerce, e-commerce, checkout
Requires at least: 4.0.0
Tested up to: 6.3.2
Tested up to: 6.4.1
Requires PHP: 7.2
WC requires at least: 5.0.0
WC tested up to: 8.2.0
Stable tag: 2.5.4
WC tested up to: 8.2.1
Stable tag: 2.5.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -55,6 +55,12 @@ In the settings for Wasa's checkout, there are two new settings with the heading
2. In the text field below, enter the path to the default checkout. For example, in my checkout, it is the "checkout". If it is not the same for you, you can easily get it by activating the standard checkout and copying the uri. Dy activates it at Settings> Advanced> Page Settings> Checkout Page> "Checkout".

== Changelog ==
= 2023-11-28 - version 2.5.5 =
* Tweak - You can now enable leasing and invoicing separately.
* Fix - The monthly price widget should now update to reflect a change in variable product and quantity.
* Fix - Fixed an issue related to discounts and coupons with leasing.
* Fix - Whitespace in the phone number should no longer cause the phone validation to fail. There is still an upper limit of 15 characters.

= 2023-10-09 - version 2.5.4 =
* Fix - Fixed a critical error that sometimes happened when attempting to show an error notice.

Expand Down
112 changes: 83 additions & 29 deletions assets/js/monthly-cost-widget.js
@@ -1,36 +1,90 @@

if (!window.wasaKreditMonthlyCostWidget) {
if ( ! window.wasaKreditMonthlyCostWidget ) {
var monthlyCostWidget = {
showWasaKreditFinancingModal: function (guid, monthlyCost) {
const es = document.querySelector(`.wasa-kredit-information-overlay[data-id='${guid}']`);
es.setAttribute('style', 'display:block;');
if (window.wasaKreditAnalyticsTracker) {
window.wasaKreditAnalyticsTracker.trackEvent({
action: 'Read more',
category: 'Monthly Cost Widget',
label: 'Monthly Cost Widget 2.0',
value: monthlyCost
});
window.wasaKreditAnalyticsTracker.lastOpenModal = Math.floor(Date.now() / 1000);
showWasaKreditFinancingModal: function ( guid, monthlyCost ) {
const es = document.querySelector( `.wasa-kredit-information-overlay[data-id='${ guid }']` )
es.setAttribute( "style", "display:block;" )
if ( window.wasaKreditAnalyticsTracker ) {
window.wasaKreditAnalyticsTracker.trackEvent( {
action: "Read more",
category: "Monthly Cost Widget",
label: "Monthly Cost Widget 2.0",
value: monthlyCost,
} )
window.wasaKreditAnalyticsTracker.lastOpenModal = Math.floor( Date.now() / 1000 )
}
},
closeWasaKreditFinancingModal: function (e) {
if (e.target === e.currentTarget) {
const es = document.getElementsByClassName("wasa-kredit-information-overlay");
for (let i = 0; i < es.length; i++) {
es[i].removeAttribute('style');
closeWasaKreditFinancingModal: function ( e ) {
if ( e.target === e.currentTarget ) {
const es = document.getElementsByClassName( "wasa-kredit-information-overlay" )
for ( let i = 0; i < es.length; i++ ) {
es[ i ].removeAttribute( "style" )
}
if (window.wasaKreditAnalyticsTracker) {
const overlay_open_in_seconds = Math.floor(Date.now() / 1000) - window.wasaKreditAnalyticsTracker.lastOpenModal;
window.wasaKreditAnalyticsTracker.trackEvent({
action: 'Closed',
category: 'Monthly Cost Widget',
label: 'Monthly Cost Widget 2.0',
value: overlay_open_in_seconds
});
if ( window.wasaKreditAnalyticsTracker ) {
const overlay_open_in_seconds =
Math.floor( Date.now() / 1000 ) - window.wasaKreditAnalyticsTracker.lastOpenModal
window.wasaKreditAnalyticsTracker.trackEvent( {
action: "Closed",
category: "Monthly Cost Widget",
label: "Monthly Cost Widget 2.0",
value: overlay_open_in_seconds,
} )
}
}
}
};
window.wasaKreditMonthlyCostWidget = monthlyCostWidget;
},
}
window.wasaKreditMonthlyCostWidget = monthlyCostWidget

if ( wasaKreditParams !== undefined ) {
jQuery( function ( $ ) {
const widget = {
update_monthly_widget: function ( price ) {
const url = new URL( wasaKreditParams.wasa_kredit_update_monthly_widget_url, window.location )
$.ajax( {
url: url.href,
type: "POST",
data: {
price: price.toFixed( 2 ),
nonce: wasaKreditParams.wasa_kredit_update_monthly_widget_nonce,
},
dataType: "json",
crossDomain: false,
success: function ( res ) {
const container = $( ".wasa-kredit-product-widget-container" )
container.replaceWith( $.parseHTML( res.data ) )
},
complete: function ( res ) {
console.log( res )
},
} )
},
}

$( document ).on( "found_variation", function ( e, variation ) {
let price = Math.round( variation.display_price )
const quantity = parseInt( $( "form.cart input[name=quantity]" ).val() )
if ( ! isNaN( quantity ) ) {
price *= quantity
}
widget.update_monthly_widget( price )
} )

$( "form.cart" ).on( "change", "input.qty", function () {
const quantity = parseInt( $( this ).val() )

let price = $( "form.cart .woocommerce-variation-price .amount" )
if ( 0 === price.length ) {
price = $( ".summary .price .amount" )
}

const unit_price = parseFloat( price.text().replace( /[^\d.,]+/g, "" ) )

if ( quantity > 0 ) {
const total_price = quantity * unit_price
if ( ! isNaN( total_price ) ) {
widget.update_monthly_widget( total_price )
}
}
} )
} )
}
}
63 changes: 63 additions & 0 deletions classes/class-wasa-kredit-checkout-ajax.php
@@ -0,0 +1,63 @@
<?php
/**
* Ajax class file.
*
* @package Wasa_Kredit_Checkout/Classes
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Ajax class.
*/
class Wasa_Kredit_Ajax extends WC_AJAX {
/**
* Hook in ajax handlers.
*/
public static function init() {
self::add_ajax_events();
}

/**
* Hook in methods - uses WordPress ajax handlers (admin-ajax).
*/
public static function add_ajax_events() {
$ajax_events = array(
'wasa_kredit_update_monthly_widget' => true,
);
foreach ( $ajax_events as $ajax_event => $nopriv ) {
add_action( 'wp_ajax_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) );
// WC AJAX can be used for frontend ajax requests.
add_action( 'wc_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
}
}
}


/**
* Update the monthly widget with new price.
*
* @return void An HTML snippet with the updated cost.
*/
public static function wasa_kredit_update_monthly_widget() {
$nonce = isset( $_POST['nonce'] ) ? sanitize_key( $_POST['nonce'] ) : '';
if ( ! wp_verify_nonce( $nonce, 'wasa_kredit_update_monthly_widget' ) ) {
wp_send_json_error( 'bad_nonce' );
}

$price = filter_input( INPUT_POST, 'price', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
if ( empty( $price ) ) {
wp_send_json_error( 'invalid price' );
}

$price = number_format( $price, 2, '.', '' );
$product_widget = Wasa_Kredit_WC()->product_widget->get_product_widget( $price );
empty( $product_widget ) ? wp_send_json_error( 'widget update failed' ) : wp_send_json_success( $product_widget );

}
}
Wasa_Kredit_Ajax::init();
Expand Up @@ -149,7 +149,7 @@ function ( $tc ) {
'purchaser_email' => $order->get_billing_email(),
'purchaser_phone' => $order->get_billing_phone(),
'recipient_name' => $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(),
'recipient_phone' => $order->get_billing_phone(),
'recipient_phone' => preg_replace( '/\s/', '', $order->get_billing_phone() ),
'billing_address' => array(
'company_name' => $order->get_billing_company(),
'street_address' => $order->get_billing_address_1(),
Expand Down
Expand Up @@ -45,37 +45,29 @@ protected function get_body() {
return;
}

$order_data = $order->get_data();
$currency = get_woocommerce_currency();
$shipping_cost = $order_data['shipping_total'];
$cart_items = WC()->cart->get_cart();
$currency = $order->get_currency();
$shipping_cost = $order->get_shipping_total();
$wasa_cart_items = array();

foreach ( $cart_items as $cart_item_key => $cart_item ) {
$product = apply_filters(
'woocommerce_cart_item_product',
$cart_item['data'],
$cart_item,
$cart_item_key
);

$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
foreach ( $order->get_items() as $order_item ) {
$id = ( empty( $order_item['variation_id'] ) ) ? $order_item['product_id'] : $order_item['variation_id'];
$product = wc_get_product( $id );

if ( ! empty( $tax_rates ) ) {
$tax_rate = reset( $tax_rates )['rate'];
} else {
$tax_rate = '0';
// Check if the product has been permanently deleted.
if ( empty( $product ) ) {
continue;
}

$id = $cart_item['product_id'];
$name = $product->get_name();
$tax_rate = intval( $order_item->get_tax_class() );
if ( empty( $tax_rate ) ) {
$tax_rate = 0;
}

$price_inc_vat = number_format( wc_get_price_including_tax( $product ), 2, '.', '' );
$price_ex_vat = number_format( wc_get_price_excluding_tax( $product ), 2, '.', '' );
$vat_percentage = round( $tax_rate );
$price_vat = number_format( $price_inc_vat - $price_ex_vat, 2, '.', '' );
$shipping_ex_vat = number_format( $shipping_cost, 2, '.', '' );
$quantity = $cart_item['quantity'];
$name = $order_item->get_name();
$quantity = $order_item->get_quantity();
$price_ex_vat = number_format( $order_item->get_total() / $quantity, 2, '.', '' );
$vat_percentage = $tax_rate;
$price_vat = number_format( $order_item->get_total_tax() / $quantity, 2, '.', '' );

$wasa_cart_items[] = array(
'product_id' => $id,
Expand All @@ -99,7 +91,7 @@ protected function get_body() {
'purchaser_email' => $order->get_billing_email(),
'purchaser_phone' => $order->get_billing_phone(),
'recipient_name' => $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(),
'recipient_phone' => $order->get_billing_phone(),
'recipient_phone' => preg_replace( '/\s/', '', $order->get_billing_phone() ),
'billing_address' => array(
'company_name' => $order->get_billing_company(),
'street_address' => $order->get_billing_address_1(),
Expand All @@ -126,7 +118,7 @@ protected function get_body() {
),
'cart_items' => $wasa_cart_items,
'shipping_cost_ex_vat' => array(
'amount' => $shipping_ex_vat,
'amount' => $shipping_cost,
'currency' => $currency,
),
'request_domain' => get_site_url(),
Expand Down
3 changes: 1 addition & 2 deletions includes/class-wasa-kredit-checkout-payment-gateway.php
Expand Up @@ -268,8 +268,7 @@ public function is_available() {

public function process_payment( $order_id = null ) {
// When clicking Proceed button, create a on-hold order.
global $woocommerce;
$order = new WC_Order( $order_id );
$order = wc_get_order( $order_id );

return array(
'result' => 'success',
Expand Down
35 changes: 23 additions & 12 deletions includes/class-wasa-kredit-checkout-product-widget.php
Expand Up @@ -170,17 +170,28 @@ public function extend_settings( $settings ) {
/**
* HTML for product widget.
*/
private function get_product_widget() {
$product = wc_get_product();

if ( ! $product ) {
return;
}

if ( $product->is_type( 'variable' ) ) {
$price = $product->get_variation_price( 'min' );
} else {
$price = wc_get_price_to_display( $product );
public function get_product_widget( $price = false ) {
if ( empty( $price ) ) {
$product = wc_get_product();

if ( ! $product ) {
return false;
}

if ( $product->is_type( 'variable' ) ) {
$price = $product->get_variation_price( 'min' );
} elseif ( $product->is_type( 'bundle' ) ) {
$price = $product->get_bundle_price( 'min' );
} else {
$price = wc_get_price_to_display( $product );
}

// WOO-DISCOUNT-RULES: Check if the filter for retrieving the discounted price exists. Note: by default, quantity is 1.
// Link: https://gist.github.com/AshlinRejo/c37a155a42c0e30beafbbad183f0c4e8
if ( has_filter( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price' ) ) {
$maybe_price = apply_filters( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price, $product, 1, $price, 'discounted_price', true );
$price = false !== $maybe_price ? $maybe_price : $price;
}
}

// Don't display widget if price is lower thant lower threshold setting.
Expand All @@ -189,7 +200,7 @@ private function get_product_widget() {
$level = 'info';
Wasa_Kredit_Logger::log( $log, $level, 'monthly_cost' );

return;
return false;
}

$response = Wasa_Kredit_WC()->api->get_monthly_cost_widget( $price, $this->widget_format );
Expand Down

0 comments on commit 51d1ef9

Please sign in to comment.