Plugin Directory

Changeset 2774354


Ignore:
Timestamp:
08/23/2022 05:31:29 PM (4 years ago)
Author:
smallpay
Message:

Updated check on mandatory fields in checkout page

Location:
apppago/trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • apppago/trunk/apppago.php

    r2767968 r2774354  
    55 * Plugin URI:
    66 * Description: Official APPpago plugin.
    7  * Version: 1.0.2
     7 * Version: 1.0.3
    88 * Author: SmallPay Srl
    99 * Author URI: https://www.smallpay.it
     
    2222}
    2323
    24 define('APWC_PLUGIN_VERSION', '1.0.2');
     24define('APWC_PLUGIN_VERSION', '1.0.3');
    2525
    2626class WC_APPpago
  • apppago/trunk/assets/css/apppago.css

    r2767968 r2774354  
    33 * Plugin URI:
    44 * Description: Official APPpago plugin.
    5  * Version: 1.0.2
     5 * Version: 1.0.3
    66 * Author: SmallPay Srl
    77 * Author URI: SmallPay Srl
  • apppago/trunk/assets/js/apppago.js

    r2764961 r2774354  
     1class ErrorHandler {
     2
     3    constructor()
     4    {
     5        this.wrapper = document.querySelector('.woocommerce-notices-wrapper');
     6        this.messagesList = document.querySelector('ul.woocommerce-error');
     7    }
     8
     9    appendPreparedErrorMessageElement(errorMessageElement)
     10    {
     11        if(this.messagesList === null) {
     12            this.prepareMessagesList();
     13        }
     14
     15        this.messagesList.replaceWith(errorMessageElement);
     16    }
     17
     18    message(text, persist = false)
     19    {
     20        if(! typeof String || text.length === 0){
     21            throw new Error('A new message text must be a non-empty string.');
     22        }
     23
     24        if(this.messagesList === null){
     25            this.prepareMessagesList();
     26        }
     27
     28        if (persist) {
     29            this.wrapper.classList.add('ppcp-persist');
     30        } else {
     31            this.wrapper.classList.remove('ppcp-persist');
     32        }
     33
     34        let messageNode = this.prepareMessagesListItem(text);
     35        this.messagesList.appendChild(messageNode);
     36
     37        jQuery.scroll_to_notices(jQuery('.woocommerce-notices-wrapper'))
     38    }
     39
     40    prepareMessagesList()
     41    {
     42        if(this.messagesList === null){
     43            this.messagesList = document.createElement('ul');
     44            this.messagesList.setAttribute('class', 'woocommerce-error');
     45            this.messagesList.setAttribute('role', 'alert');
     46            this.wrapper.appendChild(this.messagesList);
     47        }
     48    }
     49
     50    prepareMessagesListItem(message)
     51    {
     52        const li = document.createElement('li');
     53        li.innerHTML = message;
     54
     55        return li;
     56    }
     57
     58    clear()
     59    {
     60        if (this.messagesList === null) {
     61            return;
     62        }
     63
     64        this.messagesList.innerHTML = '';
     65    }
     66}
     67
     68
    169jQuery(document).ready(function($) {
    270    function paymentCompleted(response) {
     
    775
    876    async function buttonClicked() {
     77        if (checkRequiredInput()) {
     78            throw new Error();
     79        }
     80
     81
    982        let possRates = $('#apppago_button_possiblerates').val().split(',').map(str => {
    1083            return Number(str);
     
    35108});
    36109
     110function checkRequiredInput() {
     111    const errorHandler = new ErrorHandler();
     112    errorHandler.clear();
     113
     114    const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
     115    requiredFields.each((i, input) => {
     116        jQuery(input).trigger('validate');
     117    });
     118    const invalidFields = Array.from(jQuery('form.woocommerce-checkout .validate-required.woocommerce-invalid:visible'));   
     119    if (invalidFields.length) {
     120        const messages = invalidFields.map(el => {
     121            const name = el.querySelector('[name]')?.getAttribute('name');
     122           
     123            let label = el.querySelector('label').textContent
     124                .replaceAll('*', '')
     125                .trim();
     126            return "%s è un campo richiesto."
     127                .replace('%s', `<strong>${label}</strong>`)
     128        }).filter(s => s.length > 2);
     129
     130        if (messages.length) {1
     131            messages.forEach((s, i) => errorHandler.message(s));
     132        }
     133
     134        return messages.length > 0;
     135    }
     136}
     137
    37138function checkPaymentMethod() {
     139    const setVisible = (selectorOrElement, show, important = false) => {
     140        const element = document.querySelector(selectorOrElement);
     141        if (!element) {
     142            return;
     143        }
     144        const currentValue = element.style.getPropertyValue('display');
     145        if (!show) {
     146            if (currentValue === 'none') {
     147                return;
     148            }
     149
     150            console.log(element, "Display none");
     151            element.style.setProperty('display', 'none', important ? 'important' : '');
     152           
     153        } else {
     154            if (currentValue === 'none') {
     155                element.style.removeProperty('display');
     156            } // still not visible (if something else added display: none in CSS)
     157            const isVisible = !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
     158            if (!isVisible) {
     159                element.style.setProperty('display', 'block');
     160            }
     161            console.log("Remove Display none");
     162        }
     163    };
     164
    38165    jQuery(document).ready(function($) {
    39166        if($('#payment_method_apppago').is(':checked')){
    40             $('#place_order').hide()
     167            setVisible('#place_order', false);
    41168        }
    42         $('#payment').on('change',function (){
    43             if($('#payment_method_apppago').is(':checked')) {
    44                 $('#place_order').hide()
    45             } else {
    46                 $('#place_order').show()
    47             }
     169        $('#payment').on('change', function (){
     170            setVisible('#place_order', !$('#payment_method_apppago').is(':checked'));
    48171            clearInterval(intervalId);
    49172        })
  • apppago/trunk/includes/class-wc-gateway-apppago.php

    r2767968 r2774354  
    77    protected $module_version;
    88    protected $oConfig;
     9    protected $amount_handler;
    910    public static $lastId = 0;
    1011    public static $lastColumn = null;
     
    1617    {
    1718        require_once "class-wc-gateway-apppago-api.php";
     19        require_once "class-wc-gateway-apppago-amount.php";
     20        require_once "class-wc-gateway-apppago-amount-models.php";
    1821        require_once "constant_apppago.php";
     22
    1923        $this->id = static::GATEWAY_ID;
    2024        $this->method_title = __('APPpago', 'apppago');
    2125        $this->method_description = __('Allow the customer to pay by installments.', 'apppago');
    2226
    23         $this->module_version = '1.0.2';
     27        $this->module_version = '1.0.3';
    2428
    2529        $this->has_fields = true;
     
    3640        $this->oConfig = new WC_Gateway_APPpago_Configuration($this->settings);
    3741        $this->form_fields = $this->oConfig->get_form_fields();
     42
     43        $this->amount_handler = new AmountFactory();
    3844
    3945        $this->title = __('APPpago', 'apppago');
     
    548554        $installmentsInfo = $this->oConfig->get_installments_number(WC()->cart);
    549555
    550         $amount = preg_replace('#[^\d' . wc_get_price_decimal_separator() . ']#', '', strip_tags(WC()->cart->get_total()));
    551         $amount = absint(round(wc_format_decimal(((float) $amount * 100), wc_get_price_decimals())));
     556        $amount = $this->amount_handler->from_wc_cart(WC()->cart)->value_str();
    552557
    553558        $serviceID = $this->oConfig->ap_service;
  • apppago/trunk/readme.txt

    r2767968 r2774354  
    88WC Requires at least: 3.0.0
    99WC Tested up to: 6.5.1
    10 Stable tag: 1.0.2
     10Stable tag: 1.0.3
    1111License: GNU General Public License v3.0
    1212License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    6565
    6666= 1.0.1 =
    67  * Updated environment variables for developmente and other generic fix
     67 * Updated environment variables for development and other generic fix
    6868
    6969= 1.0.2 =
    7070 * Amount calculation update
     71
     72 = 1.0.3 =
     73  * Updated check on mandatory fields in checkout page
Note: See TracChangeset for help on using the changeset viewer.