Last active
February 18, 2026 09:24
-
-
Save webtoffee-git/e0dcb6cee1253497ff39f5f87588d28f to your computer and use it in GitHub Desktop.
Code snippet to enables Meta-compatible checkout URLs for WooCommerce using WebToffee Product Feed - By WebToffee
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
| <?php //Do not copy this line of code | |
| add_filter( 'wt_feed_facebook_product_checkout_link', 'wbfte_product_feed_change_checkout_url', 10, 2 ); | |
| /** | |
| * Modify the checkout URL for Facebook product feeds. | |
| * | |
| * @param string $checkout_url The original checkout URL. | |
| * @param WC_Product $product The product object. | |
| * | |
| * @return string Modified checkout URL. | |
| */ | |
| function wbfte_product_feed_change_checkout_url( $checkout_url, $product ) { | |
| if ( empty( $product ) || ! $product instanceof WC_Product ) { | |
| return $checkout_url; | |
| } | |
| $product_id = (int) $product->get_id(); | |
| if ( $product_id <= 0 ) { | |
| return $checkout_url; | |
| } | |
| if ( function_exists( 'wc_get_checkout_url' ) ) { | |
| $base_checkout_url = wc_get_checkout_url(); | |
| // Build the URL safely with a query argument. | |
| $modified_checkout_url = add_query_arg( | |
| array( | |
| 'products' => $product_id, | |
| ), | |
| $base_checkout_url | |
| ); | |
| // Store a sanitized URL for further use. | |
| $checkout_url = esc_url_raw( $modified_checkout_url ); | |
| } | |
| return $checkout_url; | |
| } | |
| add_action( 'template_redirect', 'wbfte_product_feed_redirect_products_to_add_to_cart' ); | |
| /** | |
| * Redirect products to add to cart on checkout page. | |
| * | |
| * Handles URL parameter 'products' and redirects to checkout with add-to-cart parameter. | |
| * | |
| * @return void | |
| */ | |
| function wbfte_product_feed_redirect_products_to_add_to_cart() { | |
| if ( is_admin() ) { | |
| return; | |
| } | |
| if ( ! is_page( 'checkout' ) ) { | |
| return; | |
| } | |
| if ( ! isset( $_GET['products'] ) ) { | |
| return; | |
| } | |
| $product_id = absint( $_GET['products'] ); | |
| if ( ! $product_id ) { | |
| return; | |
| } | |
| // Check if WooCommerce is active and product exists. | |
| if ( ! function_exists( 'wc_get_product' ) ) { | |
| return; | |
| } | |
| $product = wc_get_product( $product_id ); | |
| // Validate product. | |
| if ( ! $product || ! $product->is_purchasable() ) { | |
| return; | |
| } | |
| $redirect_url = add_query_arg( | |
| 'add-to-cart', | |
| $product_id, | |
| wc_get_checkout_url() | |
| ); | |
| wp_safe_redirect( $redirect_url ); | |
| exit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment