Plugin Directory

Changeset 3331483


Ignore:
Timestamp:
07/21/2025 12:30:05 PM (9 months ago)
Author:
aliparsa
Message:

feat: call webhook url when order status changed

Location:
pasazh/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pasazh/trunk/pasazh.php

    r3330622 r3331483  
    66Plugin URI: https://epasazh.com
    77Description: Using this plugin, you can connect your store to Pasazh. Just activate it, then go to your admin panel on the Pasazh website and complete the remaining steps to establish the connection.
    8 Version: 1.19
     8Version: 1.20
    99Author: aliparsa
    1010Author URI: https://profiles.wordpress.org/aliparsa/
     
    1212License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1313Domain Path: /languages
    14 Text Domain: my-toolset
     14Text Domain: Pasazh
    1515*/
    1616
    17 const PASAZHAPI_papi_version = "1.19";
     17const PASAZHAPI_papi_version = "1.20";
    1818add_action('rest_api_init', function () {
    1919    register_rest_route('papi', '/products', array(
     
    3939add_action('deleted_post', 'PASAZHAPI_delete_product');
    4040add_action('save_post', 'PASAZHAPI_sync_product');
     41add_action('woocommerce_payment_complete', 'handle_payment_complete');
     42add_action('woocommerce_order_status_changed', 'handle_order_status_changed', 10, 4);
     43const BASE_WEBHOOK_URL = "https://epasazh.com/api/webhooks/woocommerce";
    4144
    4245//region api functions
     46
     47function handle_order_status_changed($order_id, $old_status, $new_status, $order) {
     48    if ($new_status === 'cancelled') {
     49        foreach ($order->get_items() as $item) {
     50            $product_id = $item->get_product_id();
     51            if (!$product_id) continue;
     52            PASAZHAPI_sync_product($product_id);
     53        }
     54    }
     55}
     56
     57function handle_payment_complete($order_id) {
     58    $order = wc_get_order($order_id);
     59    if (!$order) return;
     60
     61    $order_status = $order->get_status();
     62
     63    if($order_status !== "processing")
     64        return;
     65
     66    foreach ($order->get_items() as $item) {
     67        $product_id = $item->get_product_id();
     68        if (!$product_id) continue;
     69        PASAZHAPI_sync_product($product_id);
     70    }
     71
     72}
    4373
    4474function PASAZHAPI_info($product_id)
     
    71101    }
    72102
     103    if(isset($query['field'])){
     104        $query['return'] = $query['field'];
     105        unset($query['field']);
     106    }
     107
    73108    $pasazh_products = [];
    74109    $items = wc_get_products($query);
    75 
    76     if(is_int($items[0])){
    77         foreach ($items as $productId) {
    78             $pasazh_products[] = $productId;
    79         }
    80     }else{
    81         foreach ($items as $product) {
    82             $product->description = apply_filters( 'the_content', $product->description );
    83             $pasazh_product = PASAZHAPI_get_pasazh_product_from_wc_product($product);
    84             $pasazh_products[] = $pasazh_product;
    85         }
    86     }
    87 
     110    if(count($items) > 0){
     111        if(is_int($items[0])){
     112            foreach ($items as $productId) {
     113                $pasazh_products[] = $productId;
     114            }
     115        }else{
     116            foreach ($items as $product) {
     117                $pasazh_product = PASAZHAPI_get_pasazh_product_from_wc_product($product);
     118                $pasazh_products[] = $pasazh_product;
     119            }
     120        }
     121    }
    88122    $response = [];
    89123    $response["meta"] = [
     
    91125    ];
    92126    $response["products"] = $pasazh_products;
    93 
    94127    return new WP_REST_Response($response, 200);
    95128}
     
    107140        if ($post_type != "product") return;
    108141
    109         wp_remote_get("https://epasazh.com/api/webhooks/woocommerce/addon-product-deleted/$product_id", [
     142        wp_remote_get(BASE_WEBHOOK_URL . "/addon-product-deleted/$product_id", [
    110143            "headers" => [
    111144                "papi-source-url" => get_site_url()
     
    120153        if (get_transient("papi_product_id") == $product_id) return;
    121154        set_transient('papi_product_id', $product_id, 1);
    122 
    123155        $post = get_post($product_id);
    124156        if (!$post) return;
     
    134166        }
    135167
    136         wp_remote_get("https://epasazh.com/api/webhooks/woocommerce/addon-product-updated/$product_id", [
     168        $res = wp_remote_get(BASE_WEBHOOK_URL . "/addon-product-updated/$product_id", [
    137169            "headers" => [
    138170                "papi-source-url" => get_site_url()
    139171            ]
    140172        ]);
    141     } catch (Exception $e) {}
     173    } catch (Exception $e) {
     174        error_log($e->getMessage());
     175    }
    142176}
    143177
     
    202236    $pasazh_product->attributes = $attributes;
    203237
    204     $description = $product->get_description();
     238    $description = apply_filters( 'the_content', $product->get_description() );
    205239
    206240    if (strlen($description) == 0) {
  • pasazh/trunk/readme.txt

    r3330622 r3331483  
    55Requires at least: 5.0.3
    66Tested up to: 5.9.3
    7 Stable tag: 1.19
     7Stable tag: 1.20
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2323== Screenshots ==
    2424== Changelog ==
    25 = 1.19 =
     25= 1.20 =
    2626* First release
    2727== Upgrade Notice ==
Note: See TracChangeset for help on using the changeset viewer.