• Im trying to do something like this https://github.com/woocommerce/woocommerce/issues/10040
    but since the filter api_request_url exist now im trying to use it.
    I cant figure what i have to do although.
    I have read about filters here: https://wpcandy.com/teaches/how-to-use-wordpress-hooks/ , i know i can put them in functions.php in the way it is described here: https://hookr.io/plugins/woocommerce/2.6.4/filters/woocommerce_api_request_url/
    but what exactly i have to enter on the code of the filter?

    All i want to do is something like this
    if (function_exists(‘pll_current_language’))
    if (pll_current_language() == ‘en’)
    $api_request_url = trailingslashit( home_url( ‘/index.php/wc-api/’ . $request, $scheme ) );

    with request being ‘en’.

    so what am i missing? what i have to enter here to make it work?

    // define the woocommerce_api_request_url callback?
    function filter_woocommerce_api_request_url( $api_request_url, $request, $ssl ) {?
    // make filter magic happen here…?
    return $api_request_url;?
    };?
    ?
    // add the filter?
    add_filter( ‘woocommerce_api_request_url’, ‘filter_woocommerce_api_request_url’, 10, 3 );

    thanks

    • This topic was modified 7 years, 9 months ago by Exoskeletor.
    • This topic was modified 7 years, 9 months ago by Exoskeletor.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Just put your “something like this” code inside the filter_woocommerce_api_request_url() function declaration. (in place of the magic comment)

    There might be an issue with pll_current_language() returning the proper value. Or not. It depends on how and where the current language is determined. The simplest way to find out is just try it and see.

    Thread Starter Exoskeletor

    (@exoskeletor)

    bcworks thanks for the answer. im not able to test it right now, it have to do with a visa payment system and i first have to get some codes from the bank but i know that i will need it.

    This is the filter available:

    public function api_request_url( $request, $ssl = null ) {
        if ( is_null( $ssl ) ) {
          $scheme = parse_url( home_url(), PHP_URL_SCHEME );
        } elseif ( $ssl ) {
          $scheme = 'https';
        } else {
          $scheme = 'http';
        }
    
        if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
          $api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
        } elseif ( get_option( 'permalink_structure' ) ) {
          $api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
        } else {
          $api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
        }
    
        return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
      }

    so finally im thinking of doing this:

    function woocommerce_fix_checkout_url( $request ) {
    	if (function_exists('pll_current_language'))
    		if (pll_current_language() == 'en')
    		{
    			$request = 'en';
    			return $request;
    		}
    }
    
    add_filter( 'woocommerce_api_request_url', 'woocommerce_fix_checkout_url', 10, 1 );

    am i at the right path?

    • This reply was modified 7 years, 9 months ago by Exoskeletor.
    Moderator bcworkz

    (@bcworkz)

    As far as general structure is concerned, it looks good. But you don’t want to return just ‘en’ as an URL! If the language argument is normally passed as an URL parameter like index.php?lang=en, you can use add_query_arg() to add it to the passed $request value. Much like it’s used in the last else condition of the api_request_url() snippet you posted. It accounts for the presence or not of other URL parameters and uses & or ? as is appropriate for an argument separator.

    Thread Starter Exoskeletor

    (@exoskeletor)

    oh i thought that because i have write $request that the parameter i use is $request and not $api_request_url ?? something tells me that the right solution is to combine my code with woocommerce code and to alter the woocomerce code in this way:

    
    function woocommerce_fix_checkout_url( $api_request_url, $request, $ssl ) {
    $language_slug  = '';
    if (function_exists('pll_current_language'))
    		if (pll_current_language() == 'en')
    		   $language_slug  = 'en';
    if ( is_null( $ssl ) ) {
          $scheme = parse_url( home_url(), PHP_URL_SCHEME );
        } elseif ( $ssl ) {
          $scheme = 'https';
        } else {
          $scheme = 'http';
        }
    
        if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
          $api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $language_slug  . $request, $scheme ) );
        } elseif ( get_option( 'permalink_structure' ) ) {
          $api_request_url = trailingslashit( home_url( '/wc-api/' . $language_slug  . $request, $scheme ) );
        } else {
          $api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
        }
    
    add_filter( 'woocommerce_api_request_url', 'woocommerce_fix_checkout_url', 10, 3 );
    
    • This reply was modified 7 years, 9 months ago by Exoskeletor.
    • This reply was modified 7 years, 9 months ago by Exoskeletor.
    • This reply was modified 7 years, 9 months ago by Exoskeletor.
    Moderator bcworkz

    (@bcworkz)

    No, please never alter plugin code. Your edits will be lost when the plugin updates, and WC has fairly frequent updates. While it’s more effort to create a plugin or child theme and utilize filter hooks, in the long run it’ll save you much time and headaches. Filter and action hooks are pretty much the only way we modify a WP installation, so you may as well get used to the concept.

    In your callback function, you can use (almost) any variable name you want to collect the passed parameter and subsequently return it, altered or not. Whatever value you return by any name will be assigned as the apply_filters() return value. In this case, it will be what is returned by a call to api_request_url((). Whatever you return will be used to make an API request. If you return an URL to a Google resource, an API request will be (inappropriately) sent to Google. So clearly it’s important to be sure you return the right value!

    Thread Starter Exoskeletor

    (@exoskeletor)

    hmm ok, i will first try something simple like that

    function woocommerce_fix_checkout_url( $url) {
    if (function_exists(‘pll_current_language’))
    if (pll_current_language() == ‘en’)
    return str_replace(‘/wc-api/’ , ‘/wc-api/en/’, $url);;
    }

    add_filter( ‘woocommerce_api_request_url’, ‘woocommerce_fix_checkout_url’, 10, 1 );

    Moderator bcworkz

    (@bcworkz)

    Right, it’s always a good idea to start simple and build upon it. Be sure to return the passed value unchanged in the rare case when the first conditional is false. Otherwise, aside from minor stylistic shortcomings, it looks good!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to use api_request_url filter’ is closed to new replies.