Codexshaper / laravel-woocommerce

WooCommerce Rest API for Laravel
MIT License
195 stars 57 forks source link

ErrorException: Undefined index: X-WP-Total #23

Open fredalva opened 4 years ago

fredalva commented 4 years ago

Hello, when i'am trying to get total products $totalResult = WooProduct::count(); i've got error exception: message | "Undefined index: X-WP-Total" file | "/Applications/MAMP/htdocs/swati/vendor/codexshaper/laravel-woocommerce/src/Traits/WooCommerceTrait.php" line | 114

problem seems coming from case used, if i update "X-WP-Total" to "x-wp-total" it's work.

As i need to use for multisite, some of them works only if not lowercase and some other with. How can i fix this to make it works for all ?

Thanks

maab16 commented 4 years ago

Hello @fredalva

Thanks for creating an issue. Maybe It's http2 related issue. You can change your headers from config/woocommerce.php . If you don't find your woocommerce.php file in config folder then run php artisan vendor:publish --tag=woocommerce

headers

Or update .env file like below

WOOCOMMERCE_WP_HEADER_TOTAL='x-wp-total'
WOOCOMMERCE_WP_HEADER_TOTAL_PAGES='x-wp-totalpages'

Or update in runtime. Don't forget to run these before request the endpoint

config('woocommerce.header_total', 'x-wp-total');
config('woocommerce.header_total_pages','x-wp-totalpages');

Note: Make sure your version is 2.5 or above

Best regards, Md Abu Ahsan Basir

Codexshaper commented 4 years ago

Hope your issue solved. If you like these package don't forget to press the star button. You can feedback us if you need any other feature.

Thanks

fredalva commented 4 years ago

i Tried 2 solutions provided but same error: only way to make it work is directly update code in WooCommerceTrait.php ` public function countResults() { // not working return (int) $this->getResponse()->getHeaders()['X-WP-Total'];

// working return (int) $this->getResponse()->getHeaders()['x-wp-Total'];
}`

no other code to try ?

maab16 commented 4 years ago
  1. Can you recheck you are using latest version 2.5 or above?
  2. Can you recheck your config/woocommerce.php and confirm that woocommerce.php contains bellow code
/**
     *================================================================================
     * Total results header
     *================================================================================.
     */
    'header_total'           => env('WOOCOMMERCE_WP_HEADER_TOTAL', 'x-wp-Tota'),

    /**
     *================================================================================
     * Total pages header
     *================================================================================.
     */
    'header_total_pages'           => env('WOOCOMMERCE_WP_HEADER_TOTAL_PAGES', 'x-wp-totalpages'),

If not then add these codes at the end of woocommerce.php.

Or just remove woocommerce.php and run php artisan vendor:publish --tag=woocommerce

If you still facing issue inbox me or mail me.

Thanks

fredalva commented 4 years ago

i upgrade to latest version 2.5 this is config.woocommerce.php file

`<?php

return [ /* -------------------------------------------------------------------------- Home URL to the store you want to connect to here
*/
'store_url' => env('WOOCOMMERCE_STORE_URL', ''),

/*
|--------------------------------------------------------------------------
| Consumer Key
|--------------------------------------------------------------------------
*/
'consumer_key' => env('WOOCOMMERCE_CONSUMER_KEY', ''),

/*
|--------------------------------------------------------------------------
| Consumer Secret
|--------------------------------------------------------------------------
*/
'consumer_secret' => env('WOOCOMMERCE_CONSUMER_SECRET', ''),

/*
|--------------------------------------------------------------------------
| SSL support
|--------------------------------------------------------------------------
*/
'verify_ssl' => env('WOOCOMMERCE_VERIFY_SSL', false),

/*
|--------------------------------------------------------------------------
| API version
|--------------------------------------------------------------------------
*/
'api_version' => env('WOOCOMMERCE_VERSION', 'v3'),

/*
|--------------------------------------------------------------------------
| WP API usage
|--------------------------------------------------------------------------
*/
'wp_api' => env('WOOCOMMERCE_WP_API', false),

/*
|--------------------------------------------------------------------------
| Force Basic Authentication as query string
|--------------------------------------------------------------------------
*/
'query_string_auth' => env('WOOCOMMERCE_WP_QUERY_STRING_AUTH', false),

/*
|--------------------------------------------------------------------------
| WP timeout
|--------------------------------------------------------------------------
*/
'timeout' => env('WOOCOMMERCE_WP_TIMEOUT', 30),

/*
 *================================================================================
 * Total results header
 *================================================================================.
 */
'header_total' => env('WOOCOMMERCE_WP_HEADER_TOTAL', 'X-WP-Total'),

/*
 *================================================================================
 * Total pages header
 *================================================================================.
 */
'header_total_pages' => env('WOOCOMMERCE_WP_HEADER_TOTAL_PAGES', 'X-WP-TotalPages'),

]; ` this work with my localhost woocommerce test site and need to change in lower case to to work with on live test site. with latest release no need to update core file but config file , but how to test headers to know if need lowercase or not ?

Thanks

maab16 commented 4 years ago

@fredalva Sounds good it works. I already mentioned that it maybe happened for http/2. Check your request from your browser network. See attachment to better understand. When your header http version is http/2 then you need to change WordPress headers to lowercase.

headers http version

If you have any other problem open another issue. If you like our support and love this package don't forget to press the star button. If you have any idea you can share with us.

Thanks