I try to integrate WooCommerce into my existing non WordPress website design, I got the WooCommerce in a sub folder (/shop)
and checked that the shop works perfect before I try to integrate it on my existing website design, I made so far 3 pages, shop, cart, checkout, and a custom functions plugin with some code.
The shop works with my design, even if I missed some custom styles as a button and some text messages.
The cart show whatever I put in it from the shop, but the ‘update basket’ and the ‘checkout’ buttons send me to the wrong URL, like https://shop.mydomain.dk/cart.php
even my page is at https://www.mydomain.dk/cart.php
So I tried to fix URLs at all sites, with the following code:
<?php
define('WP_USE_THEMES', false);
require('./shop/wp-load.php');
if (!function_exists('WC')) {
die('WooCommerce er ikke aktiv');
}
function get_correct_domain() {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
function custom_cart_url($url) {
return get_correct_domain() . '/cart.php';
}
add_filter('woocommerce_get_cart_url', 'custom_cart_url');
function custom_checkout_url($url) {
return get_correct_domain() . '/checkout.php';
}
add_filter('woocommerce_get_checkout_url', 'custom_checkout_url');
function custom_shop_url($url) {
return get_correct_domain() . '/shop.php';
}
add_filter('woocommerce_return_to_shop_redirect', 'custom_shop_url');
But now, the cart link sends me to this URL: https://shop.mydomain.dk/www.mydomain.dk/cart.php
How can I replace the “shop” subdomain with “www” from those specific WooCommerce links?
1
You can try the following simplified code version, that will replace “shop” subdomain with “www” from return to shop, cart and checkout WooCommerce links (URLs):
<?php
defined( 'ABSPATH' ) or exit; // Exit if accessed directly.
define('WP_USE_THEMES', false);
require('./shop/wp-load.php');
if (!function_exists('WC')) {
die('WooCommerce er ikke aktiv');
}
// Filter URL replacing "shop" subdomain with "www"
function replace_shop_subdomain( $url ) {
return str_replace('://shop.', '://www.', $url);
}
add_filter('woocommerce_get_cart_url', 'replace_shop_subdomain', 20, 1 );
add_filter('woocommerce_get_checkout_url', 'replace_shop_subdomain', 20, 1 );
add_filter('woocommerce_return_to_shop_redirect', 'replace_shop_subdomain', 20, 1 );
It should work.