Skip to content

Commit

Permalink
Composer update. Added payment method binding for checkboxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed May 27, 2024
1 parent b422771 commit 1e004dc
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 92 deletions.
95 changes: 71 additions & 24 deletions assets/js/blocks/checkout/checkout-checkboxes/block.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* External dependencies
*/
import { useEffect, useState, useCallback } from '@wordpress/element';
import { useEffect, useState, useCallback, useRef } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { extensionCartUpdate } from '@woocommerce/blocks-checkout';
import _ from 'lodash';
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';

import Modal from './modal';
import LegalCheckbox from "./checkboxes/legal-checkbox";
import PrivacyCheckbox from "./checkboxes/privacy-checkbox";
import SepaCheckbox from "./checkboxes/sepa-checkbox";

const Block = ({
children,
checkoutExtensionData,
Expand All @@ -19,11 +22,25 @@ const Block = ({
const { setExtensionData } = checkoutExtensionData;
const gzdCartData = extensions.hasOwnProperty( 'woocommerce-germanized' ) ? extensions['woocommerce-germanized'] : {};
const availableCheckboxes = gzdCartData.hasOwnProperty( 'checkboxes' ) ? gzdCartData['checkboxes'] : [];
/**
* Default state
*/
const cartCheckboxes = availableCheckboxes.reduce(( acc, cur ) => (
{ ...acc, [cur.id]: cur }
{ ...acc, [ cur.id ]: { ...cur, 'hidden': cur.default_hidden, 'checked': cur.default_checked } }
), {} );
const [ checkboxes, setCheckboxes ] = useState( cartCheckboxes );
const [ modalUrl, setModalUrl ] = useState( '' );
const hasRendered = useRef( false );

const {
currentPaymentMethod
} = useSelect( ( select ) => {
const paymentStore = select( PAYMENT_STORE_KEY );

return {
currentPaymentMethod: paymentStore.getActivePaymentMethod(),
}
} );
const getExtensionDataFromCheckboxes = ( checkboxes ) => {
return Object.values( checkboxes ).filter( ( checkbox ) => {
if ( checkbox.checked || ( ! checkbox.has_checkbox && ! checkbox.hidden ) ) {
Expand All @@ -34,24 +51,6 @@ const Block = ({
} );
};

// Check for new/adjusted cart data, e.g. retrieved via cart updates
useEffect( () => {
let newCheckboxes = {};

Object.keys( cartCheckboxes ).map( ( checkboxId ) => {
const currentCheckbox = checkboxes.hasOwnProperty( checkboxId ) ? { 'checked': checkboxes[ checkboxId ].checked, 'hidden': checkboxes[ checkboxId ].hidden } : {};

newCheckboxes[ checkboxId ] = { ...cartCheckboxes[ checkboxId ], ...currentCheckbox }
});

if( !_.isEqual( newCheckboxes, checkboxes ) ) {
setCheckboxes( newCheckboxes );
}
}, [
cartCheckboxes,
setCheckboxes
] );

// Update extension data
useEffect( () => {
setExtensionData(
Expand All @@ -63,18 +62,43 @@ const Block = ({
checkboxes
] );

useEffect( () => {
Object.keys( checkboxes ).map( ( checkboxId ) => {
if ( checkboxes[ checkboxId ].show_for_payment_methods.length > 0 ) {
onChangeCheckbox( checkboxes[ checkboxId ] );
}
});
}, [
currentPaymentMethod
] );

const onChangeCheckbox = useCallback(
( checkbox ) => {

setCheckboxes( ( currentCheckboxes ) => {
const needsUpdate = currentCheckboxes[ checkbox.id ].checked !== checkbox.checked;
const updatedCheckboxes = { ...currentCheckboxes, [checkbox.id]: { ...checkbox } };

/**
* This is a tweak that overrides current checkbox hidden state
* in case the checkbox is conditionally shown for certain payment methods only
* as current payment method is only available client-side.
*/
if ( checkbox.show_for_payment_methods.length > 0 ) {
let isHidden = checkbox.default_hidden;

if ( ! isHidden ) {
checkbox.hidden = ! _.includes( checkbox.show_for_payment_methods, currentPaymentMethod );
} else {
checkbox.hidden = isHidden;
}
}

const updatedCheckboxes = { ...currentCheckboxes, [ checkbox.id ]: { ...checkbox } };

if ( needsUpdate ) {
extensionCartUpdate( {
namespace: 'woocommerce-germanized-checkboxes',
data: {
'checkboxes': getExtensionDataFromCheckboxes( updatedCheckboxes ),
'checkboxes': getExtensionDataFromCheckboxes( updatedCheckboxes )
},
} );
}
Expand All @@ -86,10 +110,33 @@ const Block = ({
setExtensionData,
checkboxes,
setCheckboxes,
extensionCartUpdate
extensionCartUpdate,
currentPaymentMethod
]
);

// Check for new/adjusted cart data, e.g. retrieved via cart updates
useEffect( () => {
if ( hasRendered.current ) {
let newCheckboxes = {};

Object.keys( cartCheckboxes ).map( ( checkboxId ) => {
const currentCheckbox = checkboxes.hasOwnProperty( checkboxId ) ? checkboxes[ checkboxId ] : {};
const newCheckbox = checkboxes.hasOwnProperty( checkboxId ) ? { 'checked': checkboxes[ checkboxId ].checked, 'hidden': checkboxes[ checkboxId ].hidden } : {};

newCheckboxes[ checkboxId ] = { ...cartCheckboxes[ checkboxId ], ...newCheckbox };

if ( newCheckboxes[ checkboxId ] !== currentCheckbox ) {
onChangeCheckbox( newCheckboxes[ checkboxId ] );
}
});
}

hasRendered.current = true;
}, [
availableCheckboxes
] );

return (
<div className="wc-gzd-checkboxes">
<Modal
Expand Down
54 changes: 35 additions & 19 deletions includes/class-wc-gzd-legal-checkbox-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ public function show_conditionally_order( $order, $location = 'pay_for_order' )
'create_account' => false,
'order' => $order,
'needs_age_verification' => wc_gzd_order_has_age_verification( $order->get_id() ),
'payment_method' => $order->get_payment_method(),
);

foreach ( $items as $key => $item ) {
Expand Down Expand Up @@ -547,6 +548,7 @@ public function show_conditionally_checkout() {
'company' => WC_GZD_Checkout::instance()->get_checkout_value( 'shipping_company' ) ? WC_GZD_Checkout::instance()->get_checkout_value( 'shipping_company' ) : WC_GZD_Checkout::instance()->get_checkout_value( 'billing_company' ),
'create_account' => WC_GZD_Checkout::instance()->get_checkout_value( 'createaccount' ) ? WC_GZD_Checkout::instance()->get_checkout_value( 'createaccount' ) : false,
'needs_age_verification' => WC()->cart && wc_gzd_cart_needs_age_verification(),
'payment_method' => WC_GZD_Checkout::instance()->get_checkout_value( 'payment_method' ) ? WC_GZD_Checkout::instance()->get_checkout_value( 'payment_method' ) : '',
);

$args = array_merge( $args, $this->get_cart_product_data() );
Expand All @@ -570,6 +572,7 @@ public function update_show_conditionally( $location, $args = array(), $context
'create_account' => false,
'order' => false,
'needs_age_verification' => false,
'payment_method' => '',
)
);

Expand Down Expand Up @@ -708,14 +711,26 @@ public function update_show_conditionally( $location, $args = array(), $context
/**
* Do only apply global hide/show logic in case the checkbox is visible by default
*/
if ( $checkbox_args['is_shown'] && ( $checkbox->get_show_for_countries() || $checkbox->get_show_for_categories() ) ) {
$show_for_country_is_valid = $checkbox->get_show_for_countries() ? false : true;
$show_for_categories_is_valid = $checkbox->get_show_for_categories() ? false : true;
if ( $checkbox_args['is_shown'] && ( $checkbox->get_show_for_countries() || $checkbox->get_show_for_categories() || $checkbox->get_show_for_payment_methods() ) ) {
$show_for_country_is_valid = $checkbox->get_show_for_countries() ? false : true;
$show_for_categories_is_valid = $checkbox->get_show_for_categories() ? false : true;
$show_for_payment_methods_is_valid = $checkbox->get_show_for_payment_methods() ? false : true;

if ( $checkbox->get_show_for_countries() && $checkbox->show_for_country( $args['country'] ) ) {
$show_for_country_is_valid = true;
}

if ( $checkbox->get_show_for_payment_methods() && $checkbox->show_for_payment_method( $args['payment_method'] ) ) {
$show_for_payment_methods_is_valid = true;
}

/**
* Checkout block payment method validation is triggered client-side.
*/
if ( WC_germanized()->is_rest_api_request() ) {
$show_for_payment_methods_is_valid = true;
}

if ( $category_ids = $checkbox->get_show_for_categories() ) {
$intersected = array_intersect( $category_ids, $args['product_category_ids'] );

Expand All @@ -724,7 +739,7 @@ public function update_show_conditionally( $location, $args = array(), $context
}
}

if ( $show_for_country_is_valid && $show_for_categories_is_valid ) {
if ( $show_for_country_is_valid && $show_for_categories_is_valid && $show_for_payment_methods_is_valid ) {
$checkbox_args['is_shown'] = true;
} else {
$checkbox_args['is_shown'] = false;
Expand Down Expand Up @@ -921,21 +936,22 @@ public function register( $id, $args ) {
$args = wp_parse_args(
$args,
array(
'html_name' => '',
'html_id' => '',
'is_mandatory' => false,
'locations' => array(),
'supporting_locations' => array(),
'html_wrapper_classes' => array(),
'html_classes' => array(),
'label_args' => array(),
'hide_input' => false,
'error_message' => '',
'admin_name' => '',
'show_for_categories' => array(),
'show_for_countries' => array(),
'refresh_fragments' => true,
'is_shown' => true,
'html_name' => '',
'html_id' => '',
'is_mandatory' => false,
'locations' => array(),
'supporting_locations' => array(),
'html_wrapper_classes' => array(),
'html_classes' => array(),
'label_args' => array(),
'hide_input' => false,
'error_message' => '',
'admin_name' => '',
'show_for_categories' => array(),
'show_for_countries' => array(),
'show_for_payment_methods' => array(),
'refresh_fragments' => true,
'is_shown' => true,
)
);

Expand Down
66 changes: 42 additions & 24 deletions includes/class-wc-gzd-legal-checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@ class WC_GZD_Legal_Checkbox {
private $id = '';

private $settings = array(
'admin_name' => '',
'admin_desc' => '',
'html_id' => '',
'html_name' => '',
'html_classes' => array(),
'html_wrapper_classes' => array(),
'html_style' => '',
'hide_input' => 'no',
'is_mandatory' => 'no',
'is_shown' => 'yes',
'is_enabled' => 'yes',
'is_core' => 'no',
'refresh_fragments' => 'no',
'value' => '1',
'label' => '',
'label_args' => array(),
'template_name' => 'checkboxes/default.php',
'template_args' => array(),
'error_message' => '',
'priority' => 10,
'locations' => array(),
'supporting_locations' => array(),
'show_for_categories' => array(),
'show_for_countries' => array(),
'admin_name' => '',
'admin_desc' => '',
'html_id' => '',
'html_name' => '',
'html_classes' => array(),
'html_wrapper_classes' => array(),
'html_style' => '',
'hide_input' => 'no',
'is_mandatory' => 'no',
'is_shown' => 'yes',
'is_enabled' => 'yes',
'is_core' => 'no',
'refresh_fragments' => 'no',
'value' => '1',
'label' => '',
'label_args' => array(),
'template_name' => 'checkboxes/default.php',
'template_args' => array(),
'error_message' => '',
'priority' => 10,
'locations' => array(),
'supporting_locations' => array(),
'show_for_categories' => array(),
'show_for_countries' => array(),
'show_for_payment_methods' => array(),
);

public function __construct( $id, $args = array() ) {
Expand Down Expand Up @@ -475,6 +476,23 @@ public function show_for_category( $category_id ) {
return in_array( absint( $category_id ), $this->get_show_for_categories(), true );
}

/**
* Payment methods to show the checkbox for.
*
* @return array
*/
public function get_show_for_payment_methods() {
return $this->settings['show_for_payment_methods'];
}

public function set_show_for_payment_methods( $payment_methods ) {
$this->settings['show_for_payment_methods'] = array_filter( (array) $payment_methods );
}

public function show_for_payment_method( $payment_method ) {
return in_array( trim( $payment_method ), $this->get_show_for_payment_methods(), true );
}

/**
* Countries show the checkbox for.
*
Expand Down
Loading

0 comments on commit 1e004dc

Please sign in to comment.