Support » Plugin: WooCommerce » woocommerce_api_request_url filter

  • fkoomek

    (@fkoomek)


    Hi.
    I am trying to solve a problem with PayPal Checkout when using Polylang and found the following:
    I changed /plugins/woocommerce/includes/class-woocommerce.php function on line 616 from:

    	public function api_request_url( $request, $ssl = null ) {
    		if ( is_null( $ssl ) ) {
    			$scheme = wp_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 ) );
    	}

    to

    
    public function api_request_url( $request, $ssl = null ) {
    		    $currentlang = get_bloginfo('language');
                switch ($currentlang) {
                case "cs-CZ":
                $lang = "/cs";
                break;
                case "en-GB":
                $lang = '/en';
                break;
                default: $lang = '/en';
        };
    		if ( is_null( $ssl ) ) {
    			$scheme = wp_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( $lang . '/index.php/wc-api/' . $request, $scheme ) );
        } elseif ( get_option( 'permalink_structure' ) ) {
            $api_request_url = trailingslashit( home_url( $lang . '/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 ) );
    	}

    It works, but anyone would know how to use a filter, so that I woudn’t have to modify Woocommerce original files?
    The filter is like:

    add_filter( 'woocommerce_api_request_url', 'filter_function_name_3164', 10, 3 );
    function filter_function_name_3164( $api_request_url, $request, $ssl ){
    	// filter...
    
    	return $api_request_url;
    }

    Thanks 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support wbrubaker

    (@wbrubaker)

    Howdy 🙂

    anyone would know how to use a filter

    The answer to that is copied/pasted twice in the question. The filter name is woocommerce_api_request_url and that can be used to modify the URL.

    Kind regards,

    Hi. Thank you for your reply.
    Well, I hoped that someone could write me the exact code 🙂
    I am not a developer and I can’t figure it out on my own. Trying to simply change the code like this:

    	add_filter( 'woocommerce_api_request_url', 'filter_function_name_3164', 10, 3 );
    function filter_function_name_3164( $api_request_url, $request, $ssl ){
    			    $currentlang = get_bloginfo('language');
                switch ($currentlang) {
                case "cs-CZ":
                $lang = "/cs";
                break;
                case "en-GB":
                $lang = '/en';
                break;
                default: $lang = '/en';
        };
    		if ( is_null( $ssl ) ) {
    			$scheme = wp_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( $lang . '/index.php/wc-api/' . $request, $scheme ) );
        } elseif ( get_option( 'permalink_structure' ) ) {
            $api_request_url = trailingslashit( home_url( $lang . '/wc-api/' . $request, $scheme ) );
    		} else {
    			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
    		}
    
    	return $api_request_url;
    }

    doesn’t work 🙂 Thanks

    • This reply was modified 9 months, 3 weeks ago by  fkoomek.

    The correct answer is:

    add_filter('woocommerce_api_request_url', 'wt_woocommerce_api_request_url', 10, 3);
    function wt_woocommerce_api_request_url($api_request_url, $request, $ssl) {
        $currentlang = get_bloginfo('language');
        switch ($currentlang) {
            case "cs-CZ":
                $lang = "/cs";
                break;
            case "en-GB":
                $lang = '/en';
                break;
            default: $lang = '/en';
        }
        if (is_null($ssl)) {
            $scheme = wp_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($lang . '/index.php/wc-api/' . $request, $scheme));
        } elseif (get_option('permalink_structure')) {
            $api_request_url = trailingslashit(home_url($lang . '/wc-api/' . $request, $scheme));
        } else {
            $api_request_url = add_query_arg('wc-api', $request, trailingslashit(home_url('', $scheme)));
        }
        return $api_request_url;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.