Changeset 3475978
- Timestamp:
- 03/05/2026 10:39:51 PM (3 weeks ago)
- Location:
- businesscentralconnector/trunk
- Files:
-
- 3 edited
-
businesscentralconnector.php (modified) (3 diffs)
-
readme.txt (modified) (3 diffs)
-
uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
businesscentralconnector/trunk/businesscentralconnector.php
r3433773 r3475978 4 4 * Plugin URI: https://help.synfynal.com/articles/index.html 5 5 * Description: Extends WooCommerce API for Business Central integration via Synfynal Connector. Requires Business Central/WooCommerce Connector from AppSource. 6 * Version: 1. 0.26 * Version: 1.1.4 7 7 * Author: Synfynal 8 8 * Author URI: https://www.synfynal.com/ … … 25 25 * ===================================================================== 26 26 */ 27 27 28 function bccentralconnector_sync_user_role($user, $request, $creating) { 28 29 … … 104 105 /** 105 106 * ===================================================================== 107 * ELEX Role-Based Pricing — discount eligibility 108 * 109 * Exposes `elex_discount_eligible` (boolean) on every WooCommerce 110 * customer REST response. Reads/writes directly from the ELEX option 111 * so it is always in sync with the ELEX plugin settings. 112 * 113 * IMPORTANT: Verify the option name before deploying. 114 * Run this SQL in phpMyAdmin to confirm: 115 * SELECT option_name FROM wp_options WHERE option_name LIKE '%elex%' 116 * ===================================================================== 117 */ 118 119 // Option name ELEX uses to store its eligible users list. 120 define('BCCENTRALCONNECTOR_ELEX_OPTION', 'eh_pricing_discount_product_on_users'); 121 122 /** 123 * Returns true if the given user ID is in the ELEX eligible users list. 124 */ 125 function bccentralconnector_elex_is_eligible($user_id) { 126 $settings = get_option(BCCENTRALCONNECTOR_ELEX_OPTION, array()); 127 $stored = isset($settings['users']) ? array_map('intval', (array) $settings['users']) : array(); 128 return in_array((int) $user_id, $stored, true); 129 } 130 131 /** 132 * Adds or removes a user from the ELEX eligible users list. 133 */ 134 function bccentralconnector_elex_set_eligible($user_id, $eligible) { 135 $settings = get_option(BCCENTRALCONNECTOR_ELEX_OPTION, array()); 136 $stored = isset($settings['users']) ? array_map('intval', (array) $settings['users']) : array(); 137 $user_id = (int) $user_id; 138 $already_in = in_array($user_id, $stored, true); 139 140 if ($eligible && !$already_in) { 141 $stored[] = $user_id; 142 $settings['users'] = $stored; 143 update_option(BCCENTRALCONNECTOR_ELEX_OPTION, $settings); 144 } elseif (!$eligible && $already_in) { 145 $settings['users'] = array_values( 146 array_filter($stored, fn($id) => $id !== $user_id) 147 ); 148 update_option(BCCENTRALCONNECTOR_ELEX_OPTION, $settings); 149 } 150 } 151 152 /** 153 * Inject `elex_discount_eligible` into the meta_data array of every 154 * WooCommerce customer REST response, matching the same key/value 155 * format used by user_role. 156 * 157 * GET /wp-json/wc/v3/customers/{id} 158 * → meta_data will contain: { "key": "elex_discount_eligible", "value": true|false } 159 */ 160 add_filter('woocommerce_rest_prepare_customer', function($response, $customer, $request) { 161 $data = $response->get_data(); 162 $meta_data = isset($data['meta_data']) ? (array) $data['meta_data'] : array(); 163 164 // Remove any raw stored meta entries for this key (WooCommerce saves it as 165 // real user meta when sent in meta_data, producing a duplicate with value "1"/""). 166 $meta_data = array_values(array_filter($meta_data, function($m) { 167 $key = is_array($m) ? ($m['key'] ?? null) : (method_exists($m, 'get_data') ? $m->get_data()['key'] ?? null : null); 168 return $key !== 'elex_discount_eligible'; 169 })); 170 171 $meta_data[] = array( 172 'id' => null, 173 'key' => 'elex_discount_eligible', 174 'value' => bccentralconnector_elex_is_eligible($data['id']), 175 ); 176 177 $data['meta_data'] = $meta_data; 178 $response->set_data($data); 179 180 return $response; 181 }, 10, 3); 182 183 /** 184 * Sync ELEX eligibility when BC connector sends `elex_discount_eligible` 185 * inside meta_data on customer create/update. 186 * 187 * Example meta_data payload from BC: 188 * { "key": "elex_discount_eligible", "value": "true" } 189 */ 190 function bccentralconnector_sync_elex_pricing($user, $request, $creating) { 191 192 // Only process Synfynal requests 193 $user_agent = $request->get_header('user_agent'); 194 if (empty($user_agent) || stripos($user_agent, 'Synfynal') === false) { 195 return; 196 } 197 198 $meta_data = $request->get_param('meta_data'); 199 if (!is_array($meta_data)) { 200 return; 201 } 202 203 foreach ($meta_data as $meta) { 204 if (isset($meta['key'], $meta['value']) && $meta['key'] === 'elex_discount_eligible') { 205 $eligible = filter_var($meta['value'], FILTER_VALIDATE_BOOLEAN); 206 bccentralconnector_elex_set_eligible($user->ID, $eligible); 207 return; 208 } 209 } 210 } 211 add_action('woocommerce_rest_insert_customer', 'bccentralconnector_sync_elex_pricing', 20, 3); 212 213 /** 214 * ===================================================================== 106 215 * Add a "WooCommerce Connector" download link in the Plugins list 107 216 * ===================================================================== -
businesscentralconnector/trunk/readme.txt
r3433764 r3475978 5 5 Requires PHP: 7.0 6 6 Tested up to: 6.8 7 Stable tag: 1. 0.27 Stable tag: 1.1.4 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 52 52 53 53 == Changelog == 54 = 1.1.4 = 55 * Added elex_discount_eligible metadata to manage eligible users. 56 54 57 = 1.0.2 = 55 58 * Added additional description on the plugin page. … … 64 67 65 68 == Upgrade Notice == 69 = 1.1.4 = 70 This release adds the possibility to assign users eligible for discounts via the API. 71 66 72 = 1.0.2 = 67 73 This release adds additional description for users. -
businesscentralconnector/trunk/uninstall.php
r3418430 r3475978 5 5 } 6 6 7 // Remove custom user meta "user_role"7 // Remove custom user meta added by this plugin 8 8 $users = get_users(array('fields' => 'ID')); 9 9 foreach ($users as $user_id) { 10 10 delete_user_meta($user_id, 'user_role'); 11 delete_user_meta($user_id, 'elex_discount_eligible'); 11 12 }
Note: See TracChangeset
for help on using the changeset viewer.