Last active
July 8, 2021 08:18
-
-
Save smeric/2320dc0344e37c62bd644b24e54d908c to your computer and use it in GitHub Desktop.
This removes the title entirely to get rid of a duplicate sentence, resolving at the same time a <h2> problem... And offers a way to make the checkout choice of the date optionnal !
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
<?php | |
/** | |
* Remove the WooCommerce Local Pickup Time plugin checkout page select field title. | |
* And offers a way to make the checkout choice of the date optionnal ! | |
* | |
* @see https://github.com/WC-Local-Pickup/woocommerce-local-pickup-time/issues/103 | |
* @see https://github.com/WC-Local-Pickup/woocommerce-local-pickup-time/issues/104 | |
*/ | |
if ( class_exists( 'Local_Pickup_Time' ) ) { | |
// Add a checkbox field to make the pickup date choice optionnal | |
add_filter( 'woocommerce_general_settings', 'make_pickup_time_choice_mandatory', 20 ); | |
// Remove the default mandatory check on checkout. | |
remove_action( 'woocommerce_checkout_process', array( Local_Pickup_Time::get_instance(), 'field_process') ); | |
// And add this custom one. | |
add_action( 'woocommerce_checkout_process', 'my_not_mandatory_custom_field_process' ); | |
// Remove the default local pickup time field from the checkout page. | |
remove_action( apply_filters( 'local_pickup_time_select_location', 'woocommerce_after_order_notes' ), array( Local_Pickup_Time::get_instance(), 'time_select') ); | |
// And add this custom one. | |
add_action( apply_filters( 'local_pickup_time_select_location', 'woocommerce_after_order_notes' ), 'my_not_mandatory_custom_time_select' ); | |
// Hide the time select field if an other delivery method is choosen on checkout page | |
add_action( 'wp_footer', 'hide_local_pickup_time_select' ); | |
/** | |
* Add a checkbox field to make the pickup date choice optionnal | |
* | |
* @param array $settings General settings fields. | |
*/ | |
function make_pickup_time_choice_mandatory( $settings ){ | |
$updated_settings = array(); | |
foreach ( $settings as $setting ) { | |
$updated_settings[] = $setting; | |
if ( isset( $setting['id'] ) && 'local_pickup_days_ahead' === $setting['id'] ){ | |
$updated_settings[] = array( | |
'title' => 'Choix d\'une date obligatoire ?', | |
'desc' => 'Si vous ne proposez que le retrait en magasin, vous devriez cocher cette case. Sinon, pensez bien à prévenir vos clients qui choisissent le retrait en boutique qu\'ils doivent penser à indiquer un créneau pour passer chercher leur commande.', | |
'id' => 'local_pickup_pickup_date_mandatory', | |
'type' => 'checkbox', | |
); | |
} | |
} | |
return $updated_settings; | |
} | |
/** | |
* Process the checkout | |
*/ | |
function my_not_mandatory_custom_field_process() { | |
global $woocommerce; | |
// Check if mandatory | |
$required = get_option( 'local_pickup_pickup_date_mandatory' ); | |
// Check if mandatory and set, if its mandatory and not set add an error. | |
if ( 'yes' === $required && ! $_POST['local_pickup_time_select'] ) { | |
wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' ); | |
} | |
} | |
/** | |
* Add the local pickup time field to the checkout page | |
* | |
* @param object $checkout The checkout object. | |
*/ | |
function my_not_mandatory_custom_time_select( $checkout ) { | |
$required = get_option( 'local_pickup_pickup_date_mandatory' ); | |
echo '<div id="local-pickup-time-select">'; | |
$lpt = Local_Pickup_Time::get_instance(); | |
woocommerce_form_field( | |
'local_pickup_time_select', | |
array( | |
'type' => 'select', | |
'class' => array( 'local-pickup-time-select-field form-row-wide' ), | |
'label' => __( 'Pickup Time', 'woocommerce-local-pickup-time' ), | |
'required' => ( 'yes' === $required ? true : false ), | |
'options' => method_exists( $lpt, 'build_pickup_time_options' ) ? $lpt->build_pickup_time_options() : $lpt->get_pickup_time_options(), | |
), | |
$checkout->get_value( 'local_pickup_time_select' ) | |
); | |
echo '</div>'; | |
} | |
/** | |
* Hide the time select field if an other delivery method is choosen on checkout page | |
* | |
* @param object $checkout The checkout object. | |
*/ | |
function hide_local_pickup_time_select() { | |
// Is the time select field required ? | |
$required = 'yes' === get_option( 'local_pickup_pickup_date_mandatory' ) ? true : false; | |
// If it's not required and we are on checkout page | |
if ( is_checkout() && ! $required ) { | |
?> | |
<style> | |
/* Hide the menu by default */ | |
#local-pickup-time-select{display:none;} | |
</style> | |
<script type="text/javascript"> | |
// Wait for the page to be fully loaded | |
// @see https://wordpress.stackexchange.com/questions/342148/woocommerce-documentation-list-of-js-events | |
jQuery('body').on('updated_checkout',function(){ | |
// Show the menu if the right delivery method is choosen | |
if(jQuery('#payment_method_cod').is(':checked')) jQuery('#local-pickup-time-select').show(); | |
// Allow menu showing / hiding to take action depending of the delivery method choice | |
jQuery('#payment input[type=radio]').each(function(){ | |
jQuery(this).change(function() { | |
// Show the menu if local pickup is selected | |
if ('cod' === jQuery(this).val()) jQuery('#local-pickup-time-select').show(); | |
// Hide it if another method is choosen | |
else jQuery('#local-pickup-time-select').hide(); | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
} |
J'ai ajouté un javascript qui cache le menu déroulant si ce n'est pas le retrait en magasin qui est choisi comme méthode de livraison. Pour que le menu reste toujours visible, il suffit de commenter la ligne 21 en plaçant devant deux slash :
// add_action( 'wp_footer', 'hide_local_pickup_time_select' );
Hello, which line of code is the one that is hiding the pop-up message "please choose the time for your pickup" that's showing (even when customer choose another method than pickup) ?
Thanks !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
La case à cocher, qui permet de rendre le choix d'un créneau horaire optionnel, apparait à la suite des champs de configuration du plugin "WooCommerce Local Pickup Time" dans "Woocommerce > Réglages > Général" comme on peut le voir sur cette image.