Created
October 21, 2025 11:19
-
-
Save xlplugins/081620e37a62a088a3bbb0aa8e3a803a to your computer and use it in GitHub Desktop.
change free gift price from 0 to 0.01
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
| if ( ! class_exists( 'ASEPTA_FreeGift_PriceFix' ) ) { | |
| class ASEPTA_FreeGift_PriceFix { | |
| public function __construct() { | |
| // Remove FKCart filters early to prevent conflicts | |
| add_action( 'init', [ $this, 'detach_fkcart_price_filters' ], 5 ); | |
| // Set 0.01 PLN for free gift items | |
| add_action( 'woocommerce_before_calculate_totals', [ $this, 'set_one_grosz' ], 99 ); | |
| add_action( 'woocommerce_cart_loaded_from_session', [ $this, 'set_one_grosz' ], 99 ); | |
| // Force display 0.01 PLN instead of "Free" in cart | |
| add_filter( 'woocommerce_cart_item_price', [ $this, 'force_display_one_grosz' ], 9999, 3 ); | |
| add_filter( 'woocommerce_cart_item_subtotal', [ $this, 'force_display_one_grosz' ], 9999, 3 ); | |
| // Add "GRATIS DO ZAMÓWIENIA" badge for free gifts | |
| add_filter( 'woocommerce_cart_item_name', [ $this, 'badge_for_free_gift' ], 10, 3 ); | |
| } | |
| /** | |
| * Remove FKCart's price filters that force 0.00 | |
| */ | |
| public function detach_fkcart_price_filters() { | |
| if ( class_exists( '\FKCart\Pro\Rewards' ) ) { | |
| $rewards = \FKCart\Pro\Rewards::getInstance(); | |
| remove_filter( 'woocommerce_product_get_price', [ $rewards, 'handle_reward_free_product' ], 10000 ); | |
| remove_filter( 'woocommerce_product_variation_get_price', [ $rewards, 'handle_reward_free_product' ], 10000 ); | |
| remove_filter( 'woocommerce_product_variation_get_regular_price', [ $rewards, 'handle_reward_free_product' ], 10000 ); | |
| } | |
| } | |
| /** | |
| * Set 0.01 PLN price for FKCart free gift products | |
| * | |
| * @param WC_Cart $cart | |
| */ | |
| public function set_one_grosz( $cart ) { | |
| if ( ! $cart || ( is_admin() && ! wp_doing_ajax() ) ) { | |
| return; | |
| } | |
| foreach ( $cart->get_cart_contents() as $key => $item ) { | |
| if ( empty( $item['data'] ) || ! ( $item['data'] instanceof WC_Product ) ) { | |
| continue; | |
| } | |
| // Check if item is a FKCart free gift | |
| $is_gift = isset( $item['_fkcart_free_gift'] ) || | |
| 'yes' === $item['data']->get_meta( '_fkcart_free_gift' ); | |
| if ( $is_gift ) { | |
| // Set price to 0.01 PLN | |
| $item['data']->set_price( 0.01 ); | |
| // Extra safety: Remove filters again inside the loop | |
| $this->detach_fkcart_price_filters(); | |
| } | |
| } | |
| } | |
| /** | |
| * Force display 0.01 PLN instead of "Free" text | |
| * | |
| * @param string $html | |
| * @param array $cart_item | |
| * @param string $cart_item_key | |
| * @return string | |
| */ | |
| public function force_display_one_grosz( $html, $cart_item, $cart_item_key ) { | |
| if ( empty( $cart_item['data'] ) || ! ( $cart_item['data'] instanceof WC_Product ) ) { | |
| return $html; | |
| } | |
| $price = (float) $cart_item['data']->get_price(); | |
| // If price is 0.01 (with floating point tolerance), force display | |
| if ( $price > 0 && $price <= 0.010001 ) { | |
| $html = wc_price( 0.01 ); | |
| } | |
| return $html; | |
| } | |
| /** | |
| * Add "GRATIS DO ZAMÓWIENIA" badge for free gift items | |
| * | |
| * @param string $name Product name | |
| * @param array $cart_item | |
| * @param string $cart_item_key | |
| * @return string | |
| */ | |
| public function badge_for_free_gift( $name, $cart_item, $cart_item_key ) { | |
| if ( empty( $cart_item['data'] ) || ! ( $cart_item['data'] instanceof WC_Product ) ) { | |
| return $name; | |
| } | |
| $price = (float) $cart_item['data']->get_price(); | |
| // Add badge if price is 0.01 or less (with floating point tolerance) | |
| if ( $price <= 0.01 + 0.000001 ) { | |
| $badge = '<div class="fkcart-free-badge" aria-label="Free gift">' . | |
| esc_html__( 'GRATIS DO ZAMÓWIENIA', 'woocommerce' ) . | |
| '</div>'; | |
| $name = $badge . $name; | |
| } | |
| return $name; | |
| } | |
| } | |
| new ASEPTA_FreeGift_PriceFix(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment