Created
December 21, 2025 02:00
-
-
Save rajeshsingh520/b186553b2a6b99cc04c3de01eae095c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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