woocommerce-plugin icon indicating copy to clipboard operation
woocommerce-plugin copied to clipboard

Custom URL routes

Open jozefpinter opened this issue 6 years ago • 19 comments

Hello,

my payment URL looks like

http://localhost/wc-api/WC_Gateway_Blockonomics/?show_order=1C8965rf2BP44Krtm2imutAXAfgFwt9Lth

and I want something like this

http://localhost/crypto/?show_order=1C8965rf2BP44Krtm2imutAXAfgFwt9Lth

Replace /wc-api/WC_Gateway_Blockonomics/ with /crypto/. That's all.

My functions.php looks like

add_action( 'init', 'wpse_rewrites_init' );

function wpse_rewrites_init(){
    add_rewrite_rule(
        'crypto/?show_order=([A-Za-z0-9]+)/?$',
      //'wc-api=WC_Gateway_Blockonomics&show_order=$matches[1]',
        'wc-api=WC_Gateway_Blockonomics&show_order=$1',
        'top' );
}

Somebody on the internet suggested that $matches[1] doesn't work for external redirects, only internal. But anyways, it doesn't work for me.

I was told to consult this issue with Blockonomics dev Support (Here).

jozefpinter avatar Oct 03 '19 16:10 jozefpinter

Thanks for taking out the time to try out our plugin and logging this. We are looking at this on priority

blockonomics avatar Oct 03 '19 18:10 blockonomics

Hi @jozefpinter

I am having success using the below code:

/* Blockonomics Rewrite Rules */
add_action('init', 'blockonomics_rewrite_rules');
function blockonomics_rewrite_rules() {
    add_rewrite_rule( 'crypto/?$', 'index.php?wc-api=WC_Gateway_Blockonomics', 'top' );
}

You will now be able to access the checkout page from http://localhost/crypto/?show_order=1C8965rf2BP44Krtm2imutAXAfgFwt9Lth

However, more changes will still be required to forward the user to this new URL. I will update you here with this information, when available.

DarrenWestwood avatar Oct 04 '19 14:10 DarrenWestwood

Hello @DarrenWestwood ,

exactly as you are saying. With your code I can access payment URL via new URL.

But I need get of rid of old URL because payment gateway is sending me there. So /wc-api/WC_Gateway_Blockonomics/ is still visible.

jozefpinter avatar Oct 04 '19 16:10 jozefpinter

In the callback file here, we can add the following line: $order_url = str_replace('wc-api/WC_Gateway_Blockonomics', 'crypto', $order_url); This will now redirect the users to the new URL during the checkout process.

Please note, this change will need to be applied after updating the Blockonomics plugin.

DarrenWestwood avatar Oct 05 '19 00:10 DarrenWestwood

@DarrenWestwood ,

can you update the callback in the plugin to redirect users to nicer URL (bitcoin, btc, crypto, whatever) instead /wc-api/WC_Gateway_Blockonomics/ ?

Because I think this is really weird URL. It will help to the plugin.

jozefpinter avatar Oct 05 '19 10:10 jozefpinter

@jozefpinter, Could you verify the above changes are working correctly for you as well?

We are assessing this option further for possible inclusion into a later release. Thank you for this suggestion.

DarrenWestwood avatar Oct 07 '19 21:10 DarrenWestwood

@DarrenWestwood , do you mean the code which goes into the file functions.php ? It is working fine, but you know... it not helps because plugin is sending me to /wc-api/WC_Gateway_Blockonomics/

or the second snippet of code which goes into the plugin? I didnt try it. Because if you update the plugin I will lose my own changes.

jozefpinter avatar Oct 08 '19 17:10 jozefpinter

As we are still assessing the viability and compatibility of the changes for all our users, this integration will require more testing before being applied to plugin releases. If you require this feature instantly, you can apply the second snippet to the plugin directly (This will need to be done after updating the plugin). We would like you to test these new changes to validate they are working as desired.

DarrenWestwood avatar Oct 08 '19 17:10 DarrenWestwood

@DarrenWestwood , my website is on localhost. I can't test it.

jozefpinter avatar Oct 08 '19 17:10 jozefpinter

You are welcome to download the plugin with the above changes included from here: https://github.com/blockonomics/woocommerce-plugin/tree/custom-url-routes

or edit the plugin from the Wordpress Plugin Editor: Edit Plugins ‹ Wordpress — WordPress

DarrenWestwood avatar Oct 09 '19 06:10 DarrenWestwood

I didnt try it. Because if you update the plugin I will lose my own changes.

Yes we understand. Just to clarify we want to test if changes @DarrenWestwood mentioned are working. We are seriously considering adding this to the main plugin code soon, but first we need to test a little

blockonomics avatar Oct 09 '19 06:10 blockonomics

@DarrenWestwood ,

I've added

$order_url = str_replace('wc-api/WC_Gateway_Blockonomics', 'crypto', $order_url);

via Plugin editor as you mentioned.

WAIT. Something is wrong here. I'll let you know.

UPDATE2:

It is sending me at new URL. That's OK. http://localhost/crypto/?show_order=1H4BMmAZ5ndvhTzCPapMtHcntfyuvobNRJ but after it redirects me at homepage. I am using a custom code to redirect 404.

So I've disabled a redirect for 404 if(is_404()) { //wp_redirect(home_url()); exit; }

Now it is sending me at new URL. Once again. That's OK. http://localhost/crypto/?show_order=1H4BMmAZ5ndvhTzCPapMtHcntfyuvobNRJ

but WP is showing a message.

Oops! That page can’t be found. Nothing was found at this location. Try searching, or check out the links below.

I am not an expert, but it seems new payment URL is a 404 page.

UPDATE3: Now during the checkout I see (after I removed the line 142)

Unable to generate bitcoin address. Note for site webmaster: Please login to your admin panel, navigate to Settings > Blockonomics and click Test Setup to diagnose the issue

I click "Test setup" then "Save". It doesn't help.

jozefpinter avatar Oct 09 '19 17:10 jozefpinter

@jozefpinter

Both snippets of code need to be applied for this change to work as desired.

  1. The add_rewrite_rule must be added, which you mention was working correctly to display the page via the new URL.
  2. $order_url = str_replace('wc-api/WC_Gateway_Blockonomics', 'crypto', $order_url); added to the plugin, to redirect users to the new page.

The plugin here has both changes applied in the plugin and is working correctly for me on localhost. Screenshot from 2019-10-10 15-11-27

I click "Test setup" then "Save". It doesn't help.

Are you receiving an error message when clicking Test Setup?

DarrenWestwood avatar Oct 10 '19 13:10 DarrenWestwood

Could you also visit Wordpress Dashboard > Settings > Permalink > and Click Save Changes.

This will flush rewrite rules for your website, in case other changes you may have made to rewrite rules are causing the issue.

DarrenWestwood avatar Oct 10 '19 13:10 DarrenWestwood

@DarrenWestwood ,

  1. Thanks, now it works. But I swear I tried it yesterday many times and it not worked. (test setup + save + flush rewrite rules) Today it is working.

  2. OK. I will let you know.

jozefpinter avatar Oct 10 '19 13:10 jozefpinter

Thank you @jozefpinter for assisting in testing this feature.

DarrenWestwood avatar Oct 10 '19 13:10 DarrenWestwood

Thanks @jozefpinter we will try to add this in the upcoming release

blockonomics avatar Oct 10 '19 14:10 blockonomics

@DarrenWestwood ,

It seems that it is working correctly for me. I've sent some test BTC. I had no issue. But remember, I am on localhost. This is not real environment.

jozefpinter avatar Oct 10 '19 23:10 jozefpinter

This merge is deferred until we have a more time / is requested more. Please add a list of test cases to the ticket

blockonomics avatar Oct 28 '19 16:10 blockonomics