P粉8335469532023-08-30 00:08:08
I also spent three hours yesterday and another hour today trying to figure out a very similar problem. I believe I now have a solution, or at least help get you into the right direction. The official WooCommerce documentation seems to differentiate between settings and instance settings (or I can't find documentation on instance settings - more in the next paragraph).
The following is based on what I have discovered so far through trial and error. If anyone has any "official" documentation on this, I'd look forward to comments on this reply.
It is very important to distinguish between "settings" and "instance settings". See this image for a visual comparison on WooCommerce backend:
set up
Settings refer to "Global Shipping Mode Settings", i.e. they appear in the global submenu, but not within the shipping area. The official WooCommerce documentation shows how to use settings and how to add a new settings page:
https://woocommerce.com/document/shipping-method-api/一个>
https://woocommerce.com/document/settings-api/ p>
To use the settings function/API, you must specify
$this->supports = array('settings'); // You may also need to include shipping-zones
In your constructor.
"Settings" are applied/used/stored globally, i.e. independently of individual delivery regions.
Instance settings
Instance settings are settings that can be saved separately for each shipping area. To use them you must use
$this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal');
In your constructor.
The following example lets me understand the subtle but important differences of the instance setup API:
https://gist.github.com/malkafly/57a5e07fd03d6fd5cab84c6182d84c86
Code differences and regarding your original question
Please note that there are subtle differences in the exact functionality and properties of the two APIs. Depending on your situation, you should decide to use one of these APIs and then strictly use that API. You're confusing the two APIs, which is probably why you can't get your custom shipping method to work.
If you wish to use the (global) settings API:
If you wish to use the (delivery region specific) instance settings API:
Make sure your constructor looks like this/contains the following code:
public function __construct( $instance_id = 0 ) { $this->instance_id = absint( $instance_id ); //... }
Use $this->instance_settings['title'] to retrieve saved settings
The links in the corresponding Settings/Instance Settings categories I posted above contain extensive additional (working) code examples for each method.