Skip to content

Commit

Permalink
separate the logic for custom surcharge
Browse files Browse the repository at this point in the history
  • Loading branch information
iyut committed Dec 26, 2024
1 parent 6ee75ab commit 5e38237
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
33 changes: 33 additions & 0 deletions classes/class-wc-connect-custom-surcharge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* A class for working around the quirks and different versions of WordPress/WooCommerce
* This is for versions higher than 2.6 (3.0 and higher)
*/

// No direct access please.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if ( ! class_exists( 'WC_Connect_Custom_Surcharge' ) ) {
/**
* WC_Connect_Compatibility class.
*/
class WC_Connect_Custom_Surcharge {

/**
* Get US Colorado custom surcharge eligibility.
*
* @param WC_Cart|null $cart Cart object or null.
*
* @return boolean.
*/
public static function get_us_co_eligibility( $cart = null ) {
if ( $cart instanceof WC_Cart ) {
return true;
}

return false;
}
}
}
21 changes: 13 additions & 8 deletions classes/class-wc-connect-taxjar-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1537,18 +1537,24 @@ public function add_custom_state_surcharge_fee( $cart ) {
return;
}

if ( ! is_array( $this->get_custom_surcharges() ) ) {
return;
}

$customer = WC()->customer;

foreach ( $this->get_custom_surcharges() as $custom_info ) {
// Making sure all info is not empty.
if ( ! isset( $custom_info['country'] ) || ! isset( $custom_info['state'] ) || ! isset( $custom_info['fee'] ) || ! isset( $custom_info['fee_text'] ) || ! is_float( $custom_info['fee'] ) ) {
foreach ( $this->get_custom_surcharges() as $key => $custom_info ) {
if ( $customer->get_shipping_country() . ':' . $customer->get_shipping_state() !== $key ) {
continue;
}

if ( $customer->get_shipping_country() !== $custom_info['country'] || $customer->get_shipping_state() !== $custom_info['state'] ) {
// Making sure all info is not empty.
if ( ! isset( $custom_info['function'] ) || ! isset( $custom_info['fee'] ) || ! isset( $custom_info['fee_text'] ) || ! is_float( $custom_info['fee'] ) || ! is_callable( $custom_info['function'], true ) ) {
continue;
}

$eligible_to_add_fee = call_user_func( $custom_info['function'], $cart );

/**
* Filter for toggling whether apply the custom surcharge or not.
* Set to `false` to disable the custom surcharge.
Expand All @@ -1559,7 +1565,7 @@ public function add_custom_state_surcharge_fee( $cart ) {
* @param array Custom surcharge info.
* @param WC_Cart WooCommerce cart object.
*/
if ( true === apply_filters( 'wc_services_apply_custom_surcharge_fee', true, $custom_info, $cart ) ) {
if ( true === apply_filters( 'wc_services_apply_custom_surcharge_fee', $eligible_to_add_fee, $custom_info, $cart ) ) {
$cart->add_fee( $custom_info['fee_text'], $custom_info['fee'], true, 'standard' );
}
}
Expand All @@ -1581,9 +1587,8 @@ public function get_custom_surcharges() {
return apply_filters(
'wc_services_custom_surcharges',
array(
array(
'country' => 'US',
'state' => 'CO',
'US-CO' => array(
'function' => array( 'WC_Connect_Custom_Surcharge', 'get_us_co_custom_surcharge' ),
'fee' => 0.27,
'fee_text' => __( 'Retail Delivery Fee', 'woocommerce_services' ),
)
Expand Down

0 comments on commit 5e38237

Please sign in to comment.