Skip to content

Commit

Permalink
Merge pull request #1626 from Automattic/feat/shipping-and-custom-fields
Browse files Browse the repository at this point in the history
feat(ras-acc): support shipping, custom fields, and order notes in modal checkout
  • Loading branch information
dkoo authored Dec 20, 2023
2 parents 1c6abaa + 3d8f573 commit 771b451
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ public static function woocommerce_checkout_fields( $fields ) {
* This should soon be replaced with a logic that allows the customization
* at the Checkout Button Block level.
*/
$cart = \WC()->cart;
if ( $cart->needs_shipping_address() ) { // Don't modify fields if shipping is required.
return $fields;
}

/**
* Filters the billing fields to show in the modal checkout.
*/
$billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [] );
if ( empty( $billing_fields ) ) {
return $fields;
Expand Down Expand Up @@ -638,6 +646,26 @@ public static function should_show_order_details() {
if ( 1 < $cart->get_cart_contents_count() ) {
return true;
}
if ( $cart->needs_shipping_address() ) {
$shipping = \WC()->shipping;
$packages = $shipping->get_packages();
$totals = $cart->get_totals();
$shipping_rates = [];

// Find all the shipping rates that apply to the current transaction.
foreach ( $packages as $package ) {
if ( ! empty( $package['rates'] ) ) {
foreach ( $package['rates'] as $rate_key => $rate ) {
$shipping_rates[ $rate_key ] = $rate;
}
}
}

// Show details if shipping requires a fee or if there are multiple shipping rates to choose from.
if ( (float) $totals['total'] !== (float) $totals['subtotal'] || 1 < count( array_values( $shipping_rates ) ) ) {
return true;
}
}
return false;
}

Expand Down Expand Up @@ -983,15 +1011,17 @@ public static function render_before_terms_and_conditions() {
}

/**
* Disable order notes in the modal checkout.
* Maybe disable order notes in the modal checkout.
*
* @param bool $enable Whether to enable the order notes field.
*
* @return bool
*/
public static function enable_order_notes_field( $enable ) {
if ( self::is_modal_checkout() ) {
return false;
$cart = \WC()->cart;
$billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [], $cart );
return in_array( 'order_comments', $billing_fields, true );
}
return $enable;
}
Expand Down

0 comments on commit 771b451

Please sign in to comment.