Plugin Directory

Changeset 3451370


Ignore:
Timestamp:
02/01/2026 12:48:20 PM (2 weeks ago)
Author:
philiasolutions
Message:
  • Added Order Status for Synchronization
Location:
philia-integration
Files:
17 added
4 edited

Legend:

Unmodified
Added
Removed
  • philia-integration/trunk/includes/class-api-settings.php

    r3402346 r3451370  
    115115            __('Phone Meta Key', 'philia-integration'),
    116116            array($this, 'phone_meta_field_callback'),
     117            'philia-api-settings',
     118            'philia_api_settings_section'
     119        );
     120
     121        // Order Status for Sync
     122        add_settings_field(
     123            'order_sync_status',
     124            __('Order Status for Synchronization', 'philia-integration'),
     125            array($this, 'order_sync_status_field_callback'),
    117126            'philia-api-settings',
    118127            'philia_api_settings_section'
     
    179188                if( isset( $settings['api_url'] ) && !empty( $settings['api_url'] ) && isset( $settings['api_key'] ) && !empty( $settings['api_key'] ) )
    180189                {
    181                     $res = self::create_and_send_woocommerce_api_key( $settings['api_url'] , $settings['api_key'], $settings['phone_meta'], $settings['data_sync'] );
     190                    $res = self::create_and_send_woocommerce_api_key( $settings['api_url'] , $settings['api_key'], $settings['phone_meta'], $settings['data_sync'], $settings['order_sync_status'] );
    182191                    if( wp_remote_retrieve_response_code( $res ) == 200 )
    183192                    {
     
    293302
    294303    /**
     304     * Outputs the dropdown for order status synchronization.
     305     *
     306     * @return void
     307     */
     308    public function order_sync_status_field_callback()
     309    {
     310        $settings = get_option('philia_api_settings');
     311       
     312        // Get the selected order status, default to 'completed'
     313        $selected_status = isset($settings['order_sync_status']) ? $settings['order_sync_status'] : 'completed';
     314       
     315        // Get all available WooCommerce order statuses
     316        $order_statuses = wc_get_order_statuses();
     317       
     318        echo '<select name="philia_api_settings[order_sync_status]" id="philia-order-sync-status">';
     319        foreach ($order_statuses as $status_key => $status_label) {
     320            // Remove 'wc-' prefix from status key
     321            $status_value = str_replace('wc-', '', $status_key);
     322            $selected = selected($selected_status, $status_value, false);
     323            echo '<option value="' . esc_attr($status_value) . '" ' . $selected . '>' . esc_html($status_label) . '</option>';
     324        }
     325        echo '</select>';
     326        ?>
     327        <p class="description"><?php echo esc_attr__('Select which order status should trigger synchronization with Philia. Only orders with this status will be sent.', 'philia-integration'); ?></p>
     328        <?php
     329    }
     330
     331    /**
    295332     * Checkbox field to show "Login to Philia" in My Account menu.
    296333     */
     
    466503        }
    467504
     505        // Sanitize order sync status
     506        if (isset($input['order_sync_status'])) {
     507            $sanitized_input['order_sync_status'] = sanitize_text_field($input['order_sync_status']);
     508        } else {
     509            // Default to 'completed' if not set
     510            $sanitized_input['order_sync_status'] = 'completed';
     511        }
     512
    468513        return $sanitized_input;
    469514    }
     
    493538     * @return array|WP_Error The response from the remote server or WP_Error on failure.
    494539     */
    495     public static function create_and_send_woocommerce_api_key($philia_url, $philia_key, $philia_phone_key, $philia_data_sync)
     540    public static function create_and_send_woocommerce_api_key($philia_url, $philia_key, $philia_phone_key, $philia_data_sync, $order_sync_status = NULL)
    496541    {
    497542        if (!class_exists('WooCommerce')) {
     
    549594
    550595        $body = json_encode([
    551             'philia_url'   => $philia_url,
    552             'url'          => get_site_url(),
    553             'key'          => $consumer_key,
    554             'secret'       => $consumer_secret,
    555             'type'         => 'woocommerce',
    556             'version'      => $version,
    557             'rest_prefix'  => $rest_prefix, // Include the current REST API prefix,
    558             'data_sync'    => $philia_data_sync,
    559             'extra_field_1' => $philia_phone_key
     596            'philia_url'        => $philia_url,
     597            'url'               => get_site_url(),
     598            'key'               => $consumer_key,
     599            'secret'            => $consumer_secret,
     600            'type'              => 'woocommerce',
     601            'version'           => $version,
     602            'plugin_version'    => PHILIA_PLUGIN_VERSION,
     603            'order_sync_status' => ( isset( $order_sync_status ) && ! is_null( $order_sync_status ) ) ? sanitize_text_field( $order_sync_status ) : 'completed',
     604            'rest_prefix'       => $rest_prefix, // Include the current REST API prefix,
     605            'data_sync'         => $philia_data_sync,
     606            'extra_field_1'     => $philia_phone_key
    560607        ]);
    561608
     
    643690            $response = wp_remote_post($philia_settings['api_url'].'/v1/api/integration/wordpress/connection-test', [
    644691                'method'    => 'GET',
    645                 'body'      => $body,
    646692                'headers'   => [
    647693                    'Content-Type' => 'application/json',
     
    808854    }
    809855
     856    /**
     857     * Get the order status that should trigger synchronization.
     858     *
     859     * @return string The order status slug (e.g., 'completed', 'processing')
     860     */
     861    public static function get_order_sync_status()
     862    {
     863        // Get the entire settings array from the database
     864        $settings = get_option( 'philia_api_settings' );
     865
     866        // Return the saved status or default to 'completed'
     867        return isset( $settings['order_sync_status'] ) ? $settings['order_sync_status'] : 'completed';
     868    }
     869
    810870}
    811871
  • philia-integration/trunk/includes/class-webhook-sender.php

    r3426712 r3451370  
    8989        add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'add_philia_id_to_product_response' ), 10, 3 );
    9090        add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'add_philia_id_to_product_response' ), 10, 3 );
     91        add_filter( 'woocommerce_rest_pre_insert_shop_coupon_object', array($this, 'map_coupons_product_ids_before_insert'), 10, 3 );
    9192
    9293        // order status completed
     
    203204            // Prepare event-specific data based on the triggered action, formatted as per WC REST API
    204205            $event_data = $this->prepare_event_data($current_action, $args);
    205             error_log("CURRENT ACTION: " . $current_action);
    206             error_log("EVENT DATA: " . print_r($event_data, true ) );
    207206
    208207            // If no relevant data could be prepared, log and exit
     
    251250                $response_code = wp_remote_retrieve_response_code($response);
    252251                $response_body = wp_remote_retrieve_body($response);
    253                 error_log("Philia_Webhook_Sender: RESPONSE for {$current_action} - Code: {$response_code}, Body: " . $response_body);
     252                //error_log("Philia_Webhook_Sender: RESPONSE for {$current_action} - Code: {$response_code}, Body: " . $response_body);
    254253
    255254                // Log if the webhook did not return a successful 2xx status code
     
    630629                    $order = wc_get_order($order_id);
    631630                    if ($order) {
    632                         // ONLY proceed if the order status is 'completed'
    633                         if ($order->get_status() !== 'completed') {
    634                             error_log("Philia_Webhook_Sender: Order ID {$order_id} status is '{$order->get_status()}', not 'completed'. Skipping webhook send.");
     631                        // Get the configured order status for synchronization (default: 'completed')
     632                        $sync_status = Philia_API_Settings::get_order_sync_status();
     633                       
     634                        // ONLY proceed if the order status matches the configured sync status
     635                        if ($order->get_status() !== $sync_status) {
     636                            error_log("Philia_Webhook_Sender: Order ID {$order_id} status is '{$order->get_status()}', not '{$sync_status}'. Skipping webhook send.");
    635637                            return []; // Return empty data to prevent sending webhook
    636638                        }
     
    962964    }
    963965
    964 
    965966    /**
    966967     * Add Philia Product ID to product REST API responses
     
    11661167        return $coupon;
    11671168    }
     1169
     1170    /**
     1171     * Map coupon product IDs before inserting/updating via REST API.
     1172     * Supports mapping by product ID, SKU, or Philia Product ID (_philia_product_id).
     1173     *
     1174     * @param WC_Coupon $coupon The coupon object being created or updated.
     1175     * @param WP_REST_Request $request The REST API request object.
     1176     * @param bool $creating Whether this is a create operation (true) or update (false).
     1177     * @return WC_Coupon The modified coupon object.
     1178     */
     1179    public function map_coupons_product_ids_before_insert($coupon, $request, $creating)
     1180    {
     1181        // Check for custom REST parameter to enable product ID rewriting
     1182        $flag = $request->get_param( 'rewrite_product_ids' );
     1183       
     1184        if ( empty( $flag ) ) {
     1185            return $coupon;
     1186        }
     1187       
     1188        // Get existing product_ids from request
     1189        $product_ids = $request->get_param( 'product_ids' );
     1190       
     1191        if ( ! is_array( $product_ids ) ) {
     1192            return $coupon;
     1193        }
     1194       
     1195        /**
     1196         * Map incoming product IDs to actual WooCommerce product IDs
     1197         * Supports three methods:
     1198         * 1. Direct product ID (numeric)
     1199         * 2. SKU lookup
     1200         * 3. Philia Product ID (_philia_product_id meta field)
     1201         */
     1202        $mapped_ids = [];
     1203       
     1204        foreach ( $product_ids as $identifier )
     1205        {
     1206            $found_id = null;
     1207           
     1208            // Method 1: If input is numeric and a valid product, use it directly
     1209            if ( is_numeric( $identifier ) && get_post_type( $identifier ) === 'product' ) {
     1210                $found_id = (int) $identifier;
     1211            }
     1212
     1213            // Method 2: Try to find by SKU
     1214            elseif ( ! is_numeric( $identifier ) ) {
     1215                $product_id = wc_get_product_id_by_sku( $identifier );
     1216                if ( $product_id ) {
     1217                    $found_id = (int) $product_id;
     1218                }
     1219            }
     1220           
     1221            // Method 3: If not found yet, try to find by Philia Product ID
     1222            if ( ! $found_id ) {
     1223                $found_id = $this->get_product_id_by_philia_id( $identifier );
     1224            }
     1225           
     1226            // Add to mapped IDs if found
     1227            if ( $found_id ) {
     1228                $mapped_ids[] = $found_id;
     1229            }
     1230        }
     1231       
     1232        // Remove duplicates and invalid IDs
     1233        $mapped_ids = array_values( array_unique( array_filter( $mapped_ids ) ) );
     1234       
     1235        // Overwrite coupon product_ids with mapped values
     1236        $coupon->set_product_ids( $mapped_ids );
     1237       
     1238        return $coupon;
     1239    }
    11681240   
    11691241    /**
  • philia-integration/trunk/philia-integration.php

    r3426712 r3451370  
    33 * Plugin Name: Philia Integration with WooCommerce
    44 * Description: Sending Users, Products, Categories and invoices from WooCommerce to Philia and Receive Coupons and Cashback from Philia
    5  * Version: 1.12
     5 * Version: 1.13
    66 * Author: Philia
    77 * Author URI: https://fa.philia.vip
     
    1717
    1818// Define constants for plugin directory, URL, and table prefix.
    19 define('PHILIA_PLUGIN_VERSION', '1.12');
     19define('PHILIA_PLUGIN_VERSION', '1.13');
    2020define('PHILIA_PLUGIN_DIR', plugin_dir_path(__FILE__));
    2121define('PHILIA_PLUGIN_URL', plugin_dir_url(__FILE__));
  • philia-integration/trunk/readme.txt

    r3426712 r3451370  
    44Requires at least: 6.0
    55Tested up to: 6.9
    6 Stable tag: 1.12
     6Stable tag: 1.13
    77License: GPL-2.0+
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4545
    4646== Changelog ==
     47
     48= 1.13 =
     49* Added Order Status for Synchronization
     50
     51= 1.12.1 =
     52* Bugs fixes and improvements
    4753
    4854= 1.12 =
Note: See TracChangeset for help on using the changeset viewer.