https://codewp.ai/snippet/6q8aylqgcsigx0rjtwuv6wmwfxx8k/
Copy URL

Share this on your favorite social networks:

OR
Please log in to use this feature.
Email To Yourself Or A Developer
verified
Verified working as of March 20, 2023
GenID: 41143

Add To Cart Redirect Code Snippet For WooCommerce

Redirect users when they add a product to the cart. Support for a Global Redirect, Product Redirect, and even a variation redirect that occurs on the WooCommerce Add To Cart function.

Original Prompt: Create a class named CwpaiAtcRedirect using OOP that generates a "add to cart redirect" feature for WooCommerce. Add a new field named "Global CWPAI Redirect" to the "General" tab on the WooCommerce Plugin Settings page. This redirect should apply to any product that does not have its own redirect. On a Variable product, add a new URL field called "Variation CWPAI Redirect" to each variation, placed directly under the Variation Description textfield. Different variations may have different redirect URLs.On a simple product, add a new URL field named "Product CWPAI Redirect" to the advanced tab of the product data metabox.When a product is added to the cart, redirect to the highest priority URL.

Original
class CwpaiAtcRedirect {
    public function __construct() {
        add_action( 'woocommerce_add_to_cart', array( $this, 'cwpai_redirect_to_highest_priority_url' ), 10, 6 );
        add_filter( 'woocommerce_general_settings', array( $this, 'cwpai_add_global_redirect_field' ) );
        add_action( 'woocommerce_product_options_advanced', array( $this, 'cwpai_add_product_redirect_field' ) );
        add_action( 'woocommerce_save_product_variation', array( $this, 'cwpai_save_variation_redirect_field' ), 10, 2 );
        add_action( 'woocommerce_product_after_variable_attributes', array( $this, 'cwpai_add_variation_redirect_field' ), 10, 3 );
    }

    public function cwpai_redirect_to_highest_priority_url( $cart_item_key, $product_id, $quantity, $variation_id = '', $variation = '', $cart_item_data = array() ) {
        $redirect_url = '';
        if ( $variation_id ) {
            $redirect_url = get_post_meta( $variation_id, 'cwpai_variation_redirect_url', true );
        }
        if ( ! $redirect_url ) {
            $redirect_url = get_post_meta( $product_id, 'cwpai_product_redirect_url', true );
        }
        if ( ! $redirect_url ) {
            $redirect_url = get_option( 'cwpai_global_redirect_url' );
        }
        if ( $redirect_url ) {
            wp_redirect( $redirect_url );
            exit;
        }
    }

    public function cwpai_add_global_redirect_field( $settings ) {
        $updated_settings = array();
        foreach ( $settings as $section ) {
            $updated_settings[] = $section;
            if ( isset( $section['id'] ) && 'general_options' == $section['id'] && isset( $section['type'] ) && 'sectionend' == $section['type'] ) {
                $updated_settings[] = array(
                    'name'     => 'Global CWPAI Redirect',
                    'desc_tip' => 'true',
                    'id'       => 'cwpai_global_redirect_url',
                    'type'     => 'text',
                    'css'      => 'min-width:300px;',
                );
            }
        }
        return $updated_settings;
    }

    public function cwpai_add_product_redirect_field() {
        woocommerce_wp_text_input(
            array(
                'id'          => 'cwpai_product_redirect_url',
                'label'       => 'Product CWPAI Redirect',
                'desc_tip'    => 'true',
                'description' => 'Enter the URL to redirect to when this product is added to the cart.',
                'wrapper_class' => 'form-field-wide',
            )
        );
    }

    public function cwpai_save_variation_redirect_field( $variation_id, $i ) {
        $variation_redirect_url = $_POST['cwpai_variation_redirect_url'][$i];
        if ( ! empty( $variation_redirect_url ) ) {
            update_post_meta( $variation_id, 'cwpai_variation_redirect_url', esc_url_raw( $variation_redirect_url ) );
        } else {
            delete_post_meta( $variation_id, 'cwpai_variation_redirect_url' );
        }
    }

    public function cwpai_add_variation_redirect_field( $loop, $variation_data, $variation ) {
        woocommerce_wp_text_input(
            array(
                'id'          => 'cwpai_variation_redirect_url[' . $loop . ']',
                'label'       => 'Variation CWPAI Redirect',
                'desc_tip'    => 'true',
                'description' => 'Enter the URL to redirect to when this variation is added to the cart.',
                'value'       => get_post_meta( $variation->ID, 'cwpai_variation_redirect_url', true ),
                'wrapper_class' => 'form-row form-row-full',
            )
        );
    }
}
new CwpaiAtcRedirect();

Snippet Explanation

This code defines a class named CwpaiAtcRedirect for a WordPress WooCommerce plugin that handles custom redirects after adding a product to the cart. The class provides methods for setting up global, product-specific, and variation-specific redirect URLs, and triggers a redirection to the highest priority URL (variation-specific first, then product-specific, and finally global) when a product is added to the cart. This functionality can be useful for redirecting customers to specific pages for upsells, promotions, or other purposes, based on the product or variation they have added to their cart.

The CwpaiAtcRedirect class hooks into various WooCommerce actions and filters, such as woocommerce_add_to_cart and woocommerce_general_settings, to integrate the custom redirect settings into the WooCommerce admin interface. It allows store owners to set a global redirect URL, as well as individual redirect URLs for specific products or product variations. The code takes care of storing and retrieving these settings, and handles the actual redirection process when a product is added to the cart.

Read More

FAQs About This Snippet

What does this code do?

This code creates a class called CwpaiAtcRedirect that adds functionality to redirect the user to a specified URL when adding a product to the cart in WooCommerce. The URLs can be set globally for all products or individually for each product or variation.

How does the redirect work?

When a product is added to the cart, the cwpai_redirect_to_highest_priority_url() function is called, which checks if a specific redirect URL is set for the variation ID, product ID, or globally. The redirect URL with the highest priority is used, and the user is redirected to that URL using the wp_redirect() function.

How can I set a global redirect URL for all products?

You can set a global redirect URL for all products in the WooCommerce settings. After adding the cwpai_add_global_redirect_field() function to the woocommerce_general_settings filter, a new field called "Global CWPAI Redirect" will appear in the "General" section of the WooCommerce settings where you can enter the desired URL.

How can I set a product-specific redirect URL?

To set a product-specific redirect URL, go to the product editing page in WooCommerce and add the cwpai_add_product_redirect_field() function to the woocommerce_product_options_advanced action hook. This will add a new field to the product editing page where you can enter the desired URL.

How can I set a variation-specific redirect URL?

To set a variation-specific redirect URL, go to the product editing page in WooCommerce and select the variation for which you want to set the redirect URL. Then, add the cwpai_add_variation_redirect_field() function to the woocommerce_product_after_variable_attributes action hook. This will add a new field for each variation where you can enter the desired URL. The URL will be saved when you save the variation.

Read More
publishLoad Prompt
preview
Make Public
share
Share Snippet
download
Export Snippet
Mode: WooCommerce PHP
Language: PHP
Visibility: Public
Date:
March 3, 2023
Creator:
@James LePa...
cross