Plugin Directory

Changeset 3475978


Ignore:
Timestamp:
03/05/2026 10:39:51 PM (3 weeks ago)
Author:
srdjan121
Message:

Added elex_discount_eligible metadata to manage eligible users.

Location:
businesscentralconnector/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • businesscentralconnector/trunk/businesscentralconnector.php

    r3433773 r3475978  
    44 * Plugin URI: https://help.synfynal.com/articles/index.html
    55 * Description: Extends WooCommerce API for Business Central integration via Synfynal Connector. Requires Business Central/WooCommerce Connector from AppSource.
    6  * Version: 1.0.2
     6 * Version: 1.1.4
    77 * Author: Synfynal
    88 * Author URI: https://www.synfynal.com/
     
    2525 * =====================================================================
    2626 */
     27
    2728function bccentralconnector_sync_user_role($user, $request, $creating) {
    2829
     
    104105/**
    105106 * =====================================================================
     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.
     120define('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 */
     125function 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 */
     134function 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 */
     160add_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 */
     190function 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}
     211add_action('woocommerce_rest_insert_customer', 'bccentralconnector_sync_elex_pricing', 20, 3);
     212
     213/**
     214 * =====================================================================
    106215 * Add a "WooCommerce Connector" download link in the Plugins list
    107216 * =====================================================================
  • businesscentralconnector/trunk/readme.txt

    r3433764 r3475978  
    55Requires PHP: 7.0
    66Tested up to: 6.8
    7 Stable tag: 1.0.2
     7Stable tag: 1.1.4
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5252
    5353== Changelog ==
     54= 1.1.4 =
     55* Added elex_discount_eligible metadata to manage eligible users.
     56
    5457= 1.0.2 =
    5558* Added additional description on the plugin page.
     
    6467
    6568== Upgrade Notice ==
     69= 1.1.4 =
     70This release adds the possibility to assign users eligible for discounts via the API.
     71
    6672= 1.0.2 =
    6773This release adds additional description for users.
  • businesscentralconnector/trunk/uninstall.php

    r3418430 r3475978  
    55}
    66
    7 // Remove custom user meta "user_role"
     7// Remove custom user meta added by this plugin
    88$users = get_users(array('fields' => 'ID'));
    99foreach ($users as $user_id) {
    1010    delete_user_meta($user_id, 'user_role');
     11    delete_user_meta($user_id, 'elex_discount_eligible');
    1112}
Note: See TracChangeset for help on using the changeset viewer.