Checkout Cart Redirect Help

[ad_1]

Hello everyone, here is my situation:

I need to redirect customers from one Website ( Website 1) to another website (Website 2) where they enter checkout details. I need the cart items to transfer over from Website 1 to Website 2. I’ve tried to implement a custom plug in to do this, but it refuses to actually transfer the products over. Something pehaps to do with API’s? I have pasted the code below. Please help, thank you.

<?php
/*
Plugin Name: WooCommerce Redirect to External Checkout
Description: Redirects the customer to an external site’s checkout page with items pre-added to the cart.
Version: 1.0
Author: Your Name
*/

if (!defined(‘ABSPATH’)) {
    exit; // Exit if accessed directly
}

class WC_Redirect_To_External_Checkout
{
    private $product_id_map;

    public function __construct()
    {
        // Initialize the product ID mapping array
        $this->product_id_map = array(
            // WooCommerce product ID => External site product ID
            2082 => 843,
            2081 => 840,
            1898 => 685,
            1880 => 672,
            1790 => 478,
            1789 => 480,
            1315 => 124,
            1316 => 121,
            1078 => 123,
            1064 => 120,
            774 => 122,
            789 => 119,
            258 => 116,
            173 => 113,
            257 => 117,
            174 => 114,
            256 => 118,
            172 => 115,
            1460 => 93,
            1459 => 94,
            1458 => 111,
            832 => 112,
            2091 => 1153,
            // Add more mappings as needed
        );

        add_action(‘template_redirect’, array($this, ‘redirect_to_external_checkout’));
    }

    public function redirect_to_external_checkout()
    {
        if (is_checkout() && !is_wc_endpoint_url(‘order-received’)) {
            $cart = WC()->cart->get_cart();

            // Check if cart is empty
            if (empty($cart)) {
                wc_add_notice(__(‘Your cart is empty.’, ‘woocommerce’), ‘error’);
                return;
            }

            $external_cart_data = array();

            foreach ($cart as $cart_item_key => $cart_item) {
                $product_id = $cart_item[‘product_id’];
                $quantity = $cart_item[‘quantity’];

                // Check if the product ID exists in the mapping array
                if (isset($this->product_id_map[$product_id])) {
                    $external_product_id = $this->product_id_map[$product_id];
                    $external_cart_data[] = array(
                        ‘product_id’ => $external_product_id,
                        ‘quantity’   => $quantity,
                    );
                } else {
                    // Handle the case where the product ID is not mapped
                    wc_add_notice(__(‘One or more products in your cart cannot be processed. Please contact support.’, ‘woocommerce’), ‘error’);
                    WC()->cart->empty_cart();
                    return;
                }
            }

            // Generate the external checkout URL with cart data as query parameters
            $external_checkout_url = ‘EXTERNAL CHECKOUT LINK’; // Replace with your external site’s checkout URL
            $external_cart_query = http_build_query(array(‘cart’ => $external_cart_data));
            $external_checkout_url = add_query_arg(‘cart’, urlencode(json_encode($external_cart_data)), $external_checkout_url);

            // Redirect to the external site’s checkout page
            wp_redirect($external_checkout_url);
            exit;
        }
    }
}

new WC_Redirect_To_External_Checkout();

[ad_2]
2 Comments
  1. Did you replace ‘EXTERNAL CHECKOUT LINK’ with the actual url of the external checkout page on Website 2 as their comment say?

  2. Just connect the button with the page in the other site. Buttons allow you to add URL links to buttons directly.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer