Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Created December 21, 2025 02:00
Show Gist options
  • Select an option

  • Save rajeshsingh520/b186553b2a6b99cc04c3de01eae095c2 to your computer and use it in GitHub Desktop.

Select an option

Save rajeshsingh520/b186553b2a6b99cc04c3de01eae095c2 to your computer and use it in GitHub Desktop.
add_filter(
'woocommerce_get_cart_item_from_session',
function ( $cart_item, $values ) {
if ( isset( $values['donation_amount'] ) ) {
$cart_item['donation_amount'] = $values['donation_amount'];
$cart_item['is_donation'] = true;
$cart_item['data']->set_price(
(float) $values['donation_amount']
);
$cart_item['data']->set_virtual( true );
}
return $cart_item;
},
20,
2
);
add_action('woocommerce_before_calculate_totals', function ( $cart ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
if ( ! $cart || $cart->is_empty() ) {
return;
}
foreach ( $cart->get_cart() as $cart_item ) {
if ( empty( $cart_item['donation_amount'] ) ) {
continue;
}
$cart_item['data']->set_price( (float) $cart_item['donation_amount'] );
$cart_item['data']->set_virtual( true );
}
}, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment