Skip to content

Commit

Permalink
Ajax Refresh for Parcel Delivery Checkbox
Browse files Browse the repository at this point in the history
added functionality to refresh parcel delivery checkbox (e.g. hide when changing shipping method) via AJAX (fragments).
  • Loading branch information
dennisnissle committed May 19, 2017
1 parent a3bf75b commit db22f8b
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 253 deletions.
1 change: 1 addition & 0 deletions assets/js/add-to-cart-variation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
;(function ( $, window, document, undefined ) {

$.fn.wc_gzd_variation_form = function () {

var $form = this,
$wrapper = $form.parents( wc_gzd_add_to_cart_variation_params.wrapper );

Expand Down
Binary file modified i18n/languages/woocommerce-germanized-de_DE.mo
Binary file not shown.
234 changes: 117 additions & 117 deletions i18n/languages/woocommerce-germanized-de_DE.po

Large diffs are not rendered by default.

Binary file modified i18n/languages/woocommerce-germanized-de_DE_formal.mo
Binary file not shown.
234 changes: 117 additions & 117 deletions i18n/languages/woocommerce-germanized-de_DE_formal.po

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions includes/class-wc-gzd-customer-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function __construct() {
// Send customer account notification
add_action( 'woocommerce_email', array( $this, 'email_hooks' ), 0, 1 );
// Add Title to user profile
add_filter( 'woocommerce_customer_meta_fields', array( $this, 'profile_field_title' ), 10, 1 );
add_filter( 'woocommerce_customer_meta_fields', array( $this, 'profile_field_title' ), 10, 1 );

if ( $this->is_double_opt_in_enabled() ) {

// Check for customer activation
Expand Down Expand Up @@ -319,16 +319,20 @@ public function customer_account_activate( $activation_code, $login = false ) {
}

public function init_gettext_replacement() {

if ( is_checkout() && get_option( 'woocommerce_gzd_customer_account_checkout_checkbox' ) == 'yes' )
add_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 );
}

public function set_customer_account_checkbox_text( $translated, $original, $domain ) {

$search = "Create an account?";

if ( $domain === 'woocommerce' && $original === $search ) {
remove_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 );
return wc_gzd_get_legal_text( get_option( 'woocommerce_gzd_customer_account_text' ) );
}

return $translated;
}

Expand Down
26 changes: 21 additions & 5 deletions includes/wc-gzd-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,28 @@ function woocommerce_gzd_parcel_delivery_checkbox() {
} else {
array_push( $titles, $rate->label );
}
}

if ( wc_gzd_is_parcel_delivery_data_transfer_checkbox_enabled( $ids ) ) {
wc_get_template( 'checkout/terms-parcel-delivery.php', array( 'titles' => $titles ) );
}
}

wc_get_template( 'checkout/terms-parcel-delivery.php', array(
'titles' => $titles,
'show' => wc_gzd_is_parcel_delivery_data_transfer_checkbox_enabled( $ids )
) );
}
}

if ( ! function_exists( 'woocommerce_gzd_refresh_parcel_delivery_checkbox_fragment' ) ) {

function woocommerce_gzd_refresh_parcel_delivery_checkbox_fragment( $fragments ) {

ob_start();
woocommerce_gzd_parcel_delivery_checkbox();
$delivery_checkbox = ob_get_clean();

$fragments[ '.data-parcel-delivery' ] = $delivery_checkbox;

return $fragments;
}

}

if ( ! function_exists( 'woocommerce_gzd_checkout_validation' ) ) {
Expand Down
5 changes: 5 additions & 0 deletions includes/wc-gzd-template-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@
add_action( 'woocommerce_review_order_after_submit', 'woocommerce_gzd_template_set_order_button_show_filter', PHP_INT_MAX );
add_action( 'woocommerce_gzd_review_order_before_submit', 'woocommerce_gzd_template_set_order_button_show_filter', PHP_INT_MAX );

// Refresh (show/hide) parcel delivery checkbox when changing address (which may lead to shipping method change)
if ( get_option( 'woocommerce_gzd_checkout_legal_parcel_delivery_checkbox' ) === 'yes' )
add_filter( 'woocommerce_update_order_review_fragments', 'woocommerce_gzd_refresh_parcel_delivery_checkbox_fragment', 10, 1 );

function woocommerce_gzd_checkout_load_ajax_relevant_hooks() {

if ( is_ajax() )
return;

add_action( 'woocommerce_checkout_order_review', 'woocommerce_gzd_template_order_submit', wc_gzd_get_hook_priority( 'checkout_order_submit' ) );

add_action( 'woocommerce_review_order_after_payment', 'woocommerce_gzd_template_checkout_legal', wc_gzd_get_hook_priority( 'checkout_legal' ) );
add_action( 'woocommerce_review_order_after_payment', 'woocommerce_gzd_template_checkout_set_terms_manually', wc_gzd_get_hook_priority( 'checkout_set_terms' ) );

Expand Down
2 changes: 1 addition & 1 deletion templates/checkout/terms-parcel-delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

?>

<p class="form-row data-parcel-delivery terms legal">
<p class="form-row data-parcel-delivery terms legal" style="<?php echo ( ! $show ) ? 'display: none;' : ''; ?>">
<input type="checkbox" class="input-checkbox" name="parcel-delivery" id="parcel-delivery-checkbox" />
<label for="parcel-delivery-checkbox" class="checkbox"><?php echo wc_gzd_get_legal_text_parcel_delivery( $titles ); ?></label>
</p>
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct() {
tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc_germanized' ) );

// install WC
tests_add_filter( 'setup_theme', array( $this, 'install_wc_germanized' ) );
tests_add_filter( 'init', array( $this, 'install_wc_germanized' ) );

// load the WP testing environment
require_once( $this->wp_tests_dir . '/includes/bootstrap.php' );
Expand Down
43 changes: 33 additions & 10 deletions woocommerce-germanized.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ final class WooCommerce_Germanized {

public $compatibilities = array();

private $localized_scripts = array();

/**
* Main WooCommerceGermanized Instance
*
Expand Down Expand Up @@ -179,10 +181,13 @@ public function init() {
add_filter( 'woocommerce_get_settings_pages', array( $this, 'add_settings' ) );

add_filter( 'woocommerce_enqueue_styles', array( $this, 'add_styles' ) );

// Load after WooCommerce Frontend scripts
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ), 15 );
add_action( 'wp_enqueue_scripts', array( $this, 'add_inline_styles' ) );
add_action( 'wp_print_scripts', array( $this, 'localize_scripts' ), 5 );
add_action( 'wp_print_footer_scripts', array( $this, 'localize_scripts' ), 5 );

add_filter( 'woocommerce_email_classes', array( $this, 'add_emails' ) );
add_filter( 'woocommerce_locate_core_template', array( $this, 'email_templates' ), 0, 3 );
add_action( 'woocommerce_email_order_meta', array( $this, 'email_small_business_notice' ), 1 );
Expand Down Expand Up @@ -600,24 +605,33 @@ public function add_scripts() {
$assets_path = str_replace( array( 'http:', 'https:' ), '', WC_germanized()->plugin_url() ) . '/assets/';
$frontend_script_path = $assets_path . 'js/';

wp_register_script( 'wc-gzd-revocation', $frontend_script_path . 'revocation' . $suffix . '.js', array(
'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n'
), WC_GERMANIZED_VERSION, true );

wp_register_script( 'wc-gzd-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array(
'jquery',
'wc-checkout',
), WC_GERMANIZED_VERSION, true );

wp_register_script( 'wc-gzd-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array(
'jquery', 'woocommerce', 'wc-add-to-cart-variation'
), WC_GERMANIZED_VERSION, true );

if ( is_page() && is_object( $post ) && has_shortcode( $post->post_content, 'revocation_form' ) ) {
wp_enqueue_script( 'wc-gzd-revocation', $frontend_script_path . 'revocation' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), WC_GERMANIZED_VERSION, true );
wp_enqueue_script( 'wc-gzd-revocation' );
}

if ( is_checkout() ) {

wp_enqueue_script( 'wc-gzd-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array(
'jquery',
'wc-checkout',
), WC_GERMANIZED_VERSION, true );

wp_enqueue_script( 'wc-gzd-checkout' );
}

if ( is_singular( 'product' ) ) {
$product = wc_get_product( $post->ID );

if ( $product && $product->is_type( 'variable' ) ) {
// Enqueue variation scripts
wp_enqueue_script( 'wc-gzd-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-add-to-cart-variation' ), WC_GERMANIZED_VERSION, true );
wp_enqueue_script( 'wc-gzd-add-to-cart-variation' );
}
}

Expand All @@ -629,14 +643,23 @@ public function add_scripts() {
*/
public function localize_scripts() {
global $wp;

$assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';

if ( wp_script_is( 'wc-gzd-revocation' ) ) {
if ( wp_script_is( 'wc-gzd-revocation' ) && ! in_array( 'wc-gzd-revocation', $this->localized_scripts ) ) {

$this->localized_scripts[] = 'wc-gzd-revocation';

wp_localize_script( 'wc-gzd-revocation', 'wc_gzd_revocation_params', apply_filters( 'wc_gzd_revocation_params', array(
'ajax_url' => WC()->ajax_url(),
'ajax_loader_url' => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/[email protected]' ),
) ) );
} else if ( wp_script_is( 'wc-gzd-add-to-cart-variation' ) ) {
}

if ( wp_script_is( 'wc-gzd-add-to-cart-variation' ) && ! in_array( 'wc-gzd-add-to-cart-variation', $this->localized_scripts ) ) {

$this->localized_scripts[] = 'wc-gzd-add-to-cart-variation';

wp_localize_script( 'wc-gzd-add-to-cart-variation', 'wc_gzd_add_to_cart_variation_params', apply_filters( 'woocommerce_gzd_add_to_cart_variation_params', array(
'wrapper' => '.type-product',
) ) );
Expand Down

0 comments on commit db22f8b

Please sign in to comment.