Created
February 16, 2023 14:44
-
-
Save wpflames/7f2cd0c4d15f27ad2f0dc82f1ea2248e to your computer and use it in GitHub Desktop.
Add discount checkbox field to WooCommerce checkout, save meta data, display in admin and in email
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 | |
// Add a custom checkbox fields after billing fields | |
add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_checkbox', 20 ); | |
function add_custom_checkout_checkbox(){ | |
// Add a custom checkbox field | |
if (ICL_LANGUAGE_CODE == "hu") { | |
woocommerce_form_field( 'discount_for_members', array( | |
'type' => 'checkbox', | |
'label' => __(' Tagja vagyok az alábbi szervezetek egyikének: MKSSZ, IOKK, IGKS'), | |
'class' => array( 'form-row-wide' ), | |
), '' ); | |
} else { | |
woocommerce_form_field( 'discount_for_members', array( | |
'type' => 'checkbox', | |
'label' => __('I am a member of one of the individual organizations: MKSSZ, IOKK, IGKS'), | |
'class' => array( 'form-row-wide' ), | |
), '' ); | |
} | |
} | |
// Remove "(optional)" label | |
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 ); | |
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) { | |
// Only on checkout page for Order notes field | |
if( 'discount_for_members' === $key && is_checkout() ) { | |
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>'; | |
$field = str_replace( $optional, '', $field ); | |
} | |
return $field; | |
} | |
// jQuery - Ajax script | |
add_action( 'wp_footer', 'checkout_fee_script' ); | |
function checkout_fee_script() { | |
// Only on Checkout | |
if( is_checkout() && ! is_wc_endpoint_url() ) : | |
if( WC()->session->__isset('enable_fee') ) | |
WC()->session->__unset('enable_fee') | |
?> | |
<script type="text/javascript"> | |
jQuery( function($){ | |
if (typeof wc_checkout_params === 'undefined') | |
return false; | |
$('form.checkout').on('change', 'input[name=discount_for_members]', function(e){ | |
var fee = $(this).prop('checked') === true ? '1' : ''; | |
$.ajax({ | |
type: 'POST', | |
url: wc_checkout_params.ajax_url, | |
data: { | |
'action': 'enable_fee', | |
'enable_fee': fee, | |
}, | |
success: function (result) { | |
$('body').trigger('update_checkout'); | |
}, | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
endif; | |
} | |
// Get Ajax request and saving to WC session | |
add_action( 'wp_ajax_enable_fee', 'get_enable_fee' ); | |
add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' ); | |
function get_enable_fee() { | |
if ( isset($_POST['enable_fee']) ) { | |
WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) ); | |
} | |
die(); | |
} | |
// Add a custom dynamic 20% fee | |
add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 ); | |
function custom_percetage_fee( $cart ) { | |
// Only on checkout | |
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() ) | |
return; | |
$percent = -20; | |
if (ICL_LANGUAGE_CODE == "hu") { | |
if( WC()->session->get('enable_fee') ) | |
$cart->add_fee( __( 'Kedvezmény tagoknak', 'woocommerce')." ($percent%)", ($cart->get_subtotal() * $percent / 100) ); | |
} else { | |
if( WC()->session->get('enable_fee') ) | |
$cart->add_fee( __( 'Discount for members', 'woocommerce')." ($percent%)", ($cart->get_subtotal() * $percent / 100) ); | |
} | |
} | |
/** | |
* Save & Display Custom Field @ WooCommerce Order | |
*/ | |
// Save meta data | |
function save_new_checkout_field_discount_for_memberss( $order_id ) { | |
if ( $_POST['discount_for_members'] ) update_post_meta( $order_id, '_discount_for_members_checked', esc_attr( $_POST['discount_for_members'] ) ); | |
} | |
add_action( 'woocommerce_checkout_update_order_meta', 'save_new_checkout_field_discount_for_memberss' ); | |
// Display in admin order | |
function display_discount_for_members( $order ) { | |
if ( get_post_meta( $order->get_id(), '_discount_for_members_checked', true ) == 1 ) { | |
echo '<p><strong>Tagság:</strong> Tag vagyok</p>'; | |
} else { | |
echo '<p><strong>Tagság:</strong> Nem vagyok tag </p>'; | |
} | |
} | |
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_discount_for_members' ); | |
// Display in emails | |
add_action( 'woocommerce_email_after_order_table', 'display_discount_for_members', 20, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment