Created
April 23, 2019 12:03
-
-
Save helgatheviking/a0f9f69fed4603fd443992b2baa5bf55 to your computer and use it in GitHub Desktop.
Percentage error strings for Mix and Match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Modify the MNM Javascript error strings | |
* | |
* @param array | |
* @return array | |
*/ | |
function kia_mnm_add_to_cart_params( $args ) { | |
$args['i18n_percentage_message'] = __( 'Your bag is %s percent full.', 'your-text-domain' ); | |
$args['i18n_percentage_error'] = __( '%v Please continue to 100 percent.', 'your-text-domain' ); | |
return $args; | |
} | |
add_filter( 'woocommerce_mnm_add_to_cart_parameters', 'kia_mnm_add_to_cart_params' ); | |
/** | |
* Change error message to be percentage based. | |
* | |
* @return void | |
*/ | |
function kia_percentage_based_validation() { ?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
$( '.mnm_form' ).on( 'wc-mnm-validation', function( event, container, total_qty ) { | |
percent_full = 0; | |
max_container_size = container.$mnm_data.data( 'max_container_size' ); | |
if( max_container_size !== 'undefined' && max_container_size > 0 ) { | |
percent_full = wc_mnm_number_format( total_qty / max_container_size * 100 ); | |
} | |
if( percent_full != 100 ) { | |
container.reset_messages(); | |
percent_message = wc_mnm_params.i18n_percentage_message.replace( '%s', percent_full ); | |
container.add_message( wc_mnm_params.i18n_percentage_error.replace( '%v', percent_message ), 'error' ); | |
} | |
} ); | |
} ); | |
</script> | |
<?php } | |
add_action( 'woocommerce_mix-and-match_add_to_cart', 'kia_percentage_based_validation' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment