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

Set config on run time instead of config file

Open lucafilosofi opened this issue 5 years ago • 2 comments

Hi, first of all, thank you for your work!

i guess it would be very nice to have the ability to set config as arguments to the WoocommerceApi Class, bypassing the static config file, this would help in case of batch calling on multiple endpoints, i know you can use the builtin config([]) function but in my test it does not work if i run a loop through php artisan command

example code:

foreach(['A', 'B', 'C'] as $endpoint){
   config(['woocommerce.store_url' => '...']);
   config(['woocommerce.consumer_key' => '...']);
   config(['woocommerce.consumer_secret' => '...']);

  $orders = WooCommerce::all('orders', ...);
  // $orders will output the same results for every endpoint, 
  // because the config changes only for the first endpoint
}

hope this make sense! ;-)

lucafilosofi avatar Mar 29 '20 21:03 lucafilosofi

Hello @aSeptik, Thanks for your information and complement. I fixed your issues, please make sure you are using version v2.2 or above. Follow the code sample

$endpoints = [
    [
	'store_url' => 'http://www.example1.com',
	'consumer_key' => 'store_1_consume_key',
	'consumer_secret' => 'store_1_consume_secret',
	'verify_ssl' => false,
   ],
   [
	'store_url' => 'https://www.example2.com',
	'consumer_key' => 'store_2_consume_key',
	'consumer_secret' => 'store_2_consume_secret',
	'verify_ssl' => true,
   ],
];
foreach($endpoints as $endpoint){
    config([
        'woocommerce.store_url' => $endpoint['store_url'],
        'woocommerce.consumer_key' => $endpoint['consumer_key'],
        'woocommerce.consumer_secret' => $endpoint['consumer_secret'],
        'woocommerce.verify_ssl' => $endpoint['verify_ssl'],
    ]);
    $products = Product::all();
    var_dump($products);
}

don't forget to add verify_ssl if your site is SSL protected otherwise it won't work. Reply me if you face any other issue.

Best regards, Md Abu Ahsan Basir

maab16 avatar Mar 30 '20 11:03 maab16

Maybe a stupid question :) How did you select the store you want use in your example ?

piloufaces avatar Feb 14 '21 14:02 piloufaces