Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: WooCommerce Point of Sale Compatibility for subdirectory installations
Plugin URI: https://github.com/lukasbesch/
Description: You have to flush your rewrite rules manually after activating this plugin.
Version: 1.0.0
Author: Lukas Besch
Author URI: https://lukasbesch.com/
*/
/**
* If you have a non-standard index.php in your WordPress subdirectory, this fixes some issues.
* This is not future proof as there could be some legit uses of site_url() in future releases.
*
* You have to flush your rewrite rules manually after activating this plugin.
*
* Tested with version WooCommerce Point of Sale 5.2.10
*/
if (! defined('ABSPATH')) {
exit;
}
add_action('plugins_loaded', static function () {
if (! is_plugin_active('woocommerce-point-of-sale/woocommerce-point-of-sale.php')
|| ! is_plugin_active('woocommerce/woocommerce.php')
) {
return;
}
/**
* Overwrite the rewrite rule for POS service worker. Use home_url instead of site_url.
*/
add_action('init', static function () {
add_rewrite_rule(
'^point-of-sale/sw/?$',
ltrim(str_replace(get_home_url(), '', WC_POS()->plugin_url()), '/') . '/assets/service-worker.js',
'top'
);
}, 100);
/**
* Filter site_url and replace it with home_url.
*
* Example from woocommerce-point-of-sale/includes/api/class-wc-pos-api.php:
* $register_link = get_home_url( null, '/point-of-sale/' . $outlet->get_slug() . '/' . $register->get_slug() );
*/
add_filter('site_url', static function ($url, $path, $scheme, $blog_id) {
return substr($path, 0, 15) === '/point-of-sale/' ? get_home_url($blog_id, $path, $scheme) : $url;
}, 100, 4);
/**
* Filter the site_url set in wc_pos_params.
*/
add_filter('wc_pos_params', static function ($params) {
if (isset($params['site_url'])) {
$params['site_url'] = str_replace(get_site_url(), get_home_url(), $params['site_url']);
}
return $params;
}, 100, 1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.