We wrote a small custom plugin for a client to pull some custom options (set in the WP dashboard) to be used to calculate custom shipping costs in Woocommerce.
The issue right now is that changes to the options from the admin panel don't seem to be reflected in the checkout. The "old" (previous) values are in use.
Here is a snippet showing how we set these options (in our plugin's main .php
file):
add_action('admin_init', 'vds_settings_init');
function vds_settings_init() {
register_setting('pluginPage', 'vds_settings');
add_settings_field(
'vds_pick_base_rate',
__('Pick n Dropp Base Rate', 'wordpress'),
'vds_pick_base_field_render',
'pluginPage',
'vds_pluginPage_section'
);
...
/** Other settings fields are defined... **/
...
function vds_pick_base_field_render() {
$options = get_option('vds_settings');
?>
<input type='text' name='vds_settings[vds_pick_base_rate]' value='<?php echo $options['vds_pick_base_rate']; ?>'>
<?php
}
I had a hard time finding good information on how transients and option caches work from a beginner/intermediate level - so my question is, how can I flush those options anytime a change is made in the dashboard and should this be done at the plugin level on page load or through some other mechanism? Once we hand the site off we can't expect its owner to manually blow away any cached stuff.
This is the hook we used to update the shipping totals via AJAX when fields were updated, etc.
add_action('woocommerce_checkout_update_order_review', 'update_shipping_costs');
And an example function running in that callback which gets and sets the rates defined above:
function update_shipping_costs($post_data) {
// Clear transients to make sure shipping is re-calculated
// The transients were apparently causing problems with a freelancer and he had used this nuclear option to get rid of them. We left it in as we assumed it helped.
global $wpdb;
$wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('%\_transient\_%')");
Further down in this callback, we have some helper functions. This is one of them:
function set_distance_rate($vd_dist) {
$vds_options = get_option('vds_settings');
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
// Use of the max function ensures our shipping rate is at least $5
if (has_term('courier', 'product_cat', $product->id)) {
$vd_rate = max($vds_options['vds_pick_base_rate'] + ($vd_dist * $vds_options['vds_pick_multiplier']), 5);
} else {
$vd_rate = max($vds_options['vds_shop_base_rate'] + ($vd_dist * $vds_options['vds_shop_multiplier']), 5);
}
}
WC()->session->set('vdropp_shipping_charges', $vd_rate);
}
This isn't the complete plugin file obviously, but you can see we're clearly asking for the vds_settings
set in the Dashboard. Yet, they don't reflect changes.
You may notice in the comment that we know that query
is a bit nuclear. But without better understanding transients and options in WP, we borrowed it from some partially functioning code we outsourced. That dev moved and we lost touch, so we decided to do a full re-write and left the transient-deleting query in there because we knew he had issues with cache/transient objects.
We don't believe it's a Woocommerce issue because the shipping and checkout process works -- it's the values coming from get_options
that don't seem to change when we play with them and refresh the checkout.
We had some issues early on with people arriving on the checkout page and seeing a previous session's shipping costs already there, so I nuked them with this (but I don't think it should be affecting retrieval of updated values):
// Always clear session vars and get new stuff
add_action('woocommerce_before_checkout_form', 'clear_session_vars');
function clear_session_vars($a) {
WC()->session->set('pickup_postal_code', null);
WC()->session->set('delivery_postal_code', null);
WC()->session->set('vdropp_shipping_charges', null);
WC()->session->set('shipping_total', null);
}
The options themselves are set with this, which shows them in the dashboard, but oddly I don't see them when browsing the database manually with phpMyAdmin:
function vds_options_page() {
?>
<form action='options.php' method='post'>
<h2>Distance Shipping</h2>
<?php
settings_fields('pluginPage');
do_settings_sections('pluginPage');
submit_button();
?>
</form>
<?php
}
EDIT: Added a bunch of code to hopefully better illustrate the situation! EDIT 2: Added more snippets showing session vars and option setting
vds_settings
being set?vds_settings
is being set by the top snippet and thefunction vds_options_page()
block is doing the rest as far as I'm aware. Again, the values are there in the fields when in the WP Dashboard, it's just that changing them seems to have no effect even though the values are updated in the fields on the Options page as well. When "used" at checkout, I'm not getting updated values.