Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Created March 1, 2025 02:14
Show Gist options
  • Save rajeshsingh520/aa54b96a970867a34ab815cd6fd0d992 to your computer and use it in GitHub Desktop.
Save rajeshsingh520/aa54b96a970867a34ab815cd6fd0d992 to your computer and use it in GitHub Desktop.
class Custom_Checkout_Field_20250301 {
static $variation_ids = [ 2, 3, 4, 17 ];
/**
* Constructor to set up all hooks.
*/
public function __construct() {
// Enqueue the custom JavaScript for checkout
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Add the custom field to checkout
add_filter( 'woocommerce_checkout_fields', array( $this, 'add_handling_charge_field' ) );
// Store the field value in session during order review update
add_action( 'woocommerce_checkout_update_order_review', array( $this, 'store_handling_charge_in_session' ) );
// Add the fee based on the field value
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_handling_charge_fee' ) );
}
static function needHandling(){
// Check if the cart contains products with IDs 2, 3, or 4
$product_ids = self::$variation_ids;
$show_field = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) || in_array( $cart_item['variation_id'], $product_ids ) ) {
$show_field = true;
break;
}
}
return $show_field;
}
/**
* Add the "Handling charge" dropdown to the checkout fields.
*
* @param array $fields The existing checkout fields.
* @return array The modified checkout fields.
*/
public function add_handling_charge_field( $fields ) {
if(!self::needHandling()) return $fields;
$fields['order']['handling_charge'] = array(
'type' => 'select',
'label' => __( 'This delivery requires a 2 person drop, Can you help the driver unload?' ),
'required' => true,
'options' => array(
'' => __( 'Select option', 'your-text-domain' ),
'no' => __( 'No', 'your-text-domain' ),
'yes' => __( 'Yes', 'your-text-domain' ),
),
'default' => '',
);
return $fields;
}
/**
* Store the "Handling charge" value in the session during order review update.
*
* @param string $posted_data The serialized form data from the checkout.
*/
public function store_handling_charge_in_session( $posted_data ) {
if(!self::needHandling()){
WC()->session->set( 'handling_charge', 'no' );
return;
}
// Parse the serialized form data
parse_str( $posted_data, $data );
if ( isset( $data['handling_charge'] ) ) {
WC()->session->set( 'handling_charge', $data['handling_charge'] );
}
}
/**
* Add a fee to the cart if "Handling charge" is "Yes".
*
* @param WC_Cart $cart The cart object.
*/
public function add_handling_charge_fee( $cart ) {
// Skip if in admin and not an AJAX request
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$handling_charge = WC()->session->get( 'handling_charge' );
if ( $handling_charge === 'yes' ) {
$shipping_total = $cart->shipping_total;
$fee = $shipping_total * 0.5;
$cart->add_fee( __( 'Handling charge', 'your-text-domain' ), $fee );
}
}
/**
* Enqueue custom JavaScript for triggering update_order_review
*/
public function enqueue_scripts() {
if ( is_checkout() ) {
// Inline JS to trigger update_order_review on field change
wc_enqueue_js( "jQuery(function($) { $(document.body).on('change', 'select[name=\"handling_charge\"]', function() { $(document.body).trigger('update_checkout'); }); });" );
}
}
}
// Instantiate the class
new Custom_Checkout_Field_20250301();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment