Plugin Directory

Changeset 3330622


Ignore:
Timestamp:
07/19/2025 12:25:24 PM (9 months ago)
Author:
aliparsa
Message:

refactor products function for get only product ids

Location:
pasazh/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pasazh/trunk/pasazh.php

    r3212810 r3330622  
    55Plugin Name: Pasazh
    66Plugin URI: https://epasazh.com
    7 Description: با استفاده از این افزونه میتوانید فروشگاه خود را به پاساژ متصل نمایید. کافیست آن را فعال نمایید پس از آن به پنل مدیریت خودت در وب سایت پاساژ مراجعه نموده و ادامه مراحل جهت اتصال را تکمیل نمایید.
    8 Version: 1.18
     7Description: 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.
     8Version: 1.19
    99Author: aliparsa
    1010Author URI: https://profiles.wordpress.org/aliparsa/
     
    1515*/
    1616
    17 const PASAZHAPI_papi_version = "1.18";
    18 
     17const PASAZHAPI_papi_version = "1.19";
    1918add_action('rest_api_init', function () {
    2019    register_rest_route('papi', '/products', array(
     
    2423    ));
    2524});
    26 
    2725add_action('rest_api_init', function () {
    2826    register_rest_route('papi', '/products/count', array(
     
    3230    ));
    3331});
    34 
    3532add_action('rest_api_init', function () {
    3633    register_rest_route('papi', '/info', array(
     
    4037    ));
    4138});
    42 
    43 //add_action('woocommerce_new_product', 'PASAZHAPI_sync_product', 10, 1);
    44 //add_action('woocommerce_update_product', 'PASAZHAPI_sync_product', 10, 1);
    4539add_action('deleted_post', 'PASAZHAPI_delete_product');
    4640add_action('save_post', 'PASAZHAPI_sync_product');
    4741
    48 
    4942//region api functions
    5043
    5144function PASAZHAPI_info($product_id)
    5245{
    53 
    5446    $logo_url = null;
    5547    $attachment_image_src = wp_get_attachment_image_src(get_theme_mod('custom_logo'), 'full');
     
    6456    ];
    6557    return new WP_REST_Response($response, 200);
    66 
    6758}
    6859
     
    7364function PASAZHAPI_products($data)
    7465{
    75 
    7666    $base64 = $data->get_params()["query"];
    7767    $json = base64_decode($base64);
     
    8373    $pasazh_products = [];
    8474    $items = wc_get_products($query);
    85     foreach ($items as $product) {
    86         $product->description = apply_filters( 'the_content', $product->description );
    87         $pasazh_product = PASAZHAPI_get_pasazh_product_from_wc_product($product);
    88         $pasazh_products[] = $pasazh_product;
     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        }
    8986    }
    9087
     
    9592    $response["products"] = $pasazh_products;
    9693
    97 
    9894    return new WP_REST_Response($response, 200);
    9995}
     
    10197function PASAZHAPI_delete_product($product_id)
    10298{
    103 
    104 
    10599    try {
    106100        if (get_transient("papi_product_id") == $product_id) return;
     
    108102
    109103        $post = get_post($product_id);
    110 
    111 
    112         if (!$post)
    113             return;
     104        if (!$post) return;
    114105
    115106        $post_type = $post->post_type;
    116         if ($post_type != "product")
    117             return;
    118 
     107        if ($post_type != "product") return;
    119108
    120109        wp_remote_get("https://epasazh.com/api/webhooks/woocommerce/addon-product-deleted/$product_id", [
     
    123112            ]
    124113        ]);
    125 
    126 
    127     } catch (Exception $e) {
    128 
    129     }
    130 
     114    } catch (Exception $e) {}
    131115}
    132116
    133117function PASAZHAPI_sync_product($product_id)
    134118{
    135 
    136 
    137119    try {
    138120        if (get_transient("papi_product_id") == $product_id) return;
     
    140122
    141123        $post = get_post($product_id);
    142 
    143 
    144         if (!$post)
    145             return;
     124        if (!$post) return;
    146125
    147126        $post_type = $post->post_type;
    148         if ($post_type != "product")
    149             return;
    150 
     127        if ($post_type != "product") return;
     128
     129        $product = wc_get_product($product_id);
     130        $data = $product->get_data();
     131        $data['type'] = $product->get_type();
     132        if ($product->is_type('variable')) {
     133            $data['variations'] = $product->get_available_variations();
     134        }
    151135
    152136        wp_remote_get("https://epasazh.com/api/webhooks/woocommerce/addon-product-updated/$product_id", [
     
    155139            ]
    156140        ]);
    157 
    158 
    159     } catch (Exception $e) {
    160 
    161     }
    162 
     141    } catch (Exception $e) {}
    163142}
    164143
     
    168147
    169148function get_readable_product_attributes($product_id) {
    170     // دریافت شیء محصول
    171149    $product = wc_get_product($product_id);
    172 
    173150    if (!$product) {
    174151        return [];
     
    187164
    188165        if ($attribute->is_taxonomy()) {
    189             // اگر ویژگی نوع تاکسونومی باشد
    190             $taxonomy = $attribute->get_name(); // مانند pa_color
    191             $attribute_data['name'] = wc_attribute_label($taxonomy); // دریافت نام ویژگی
    192             $terms = wc_get_product_terms($product_id, $taxonomy, ['fields' => 'names']); // مقادیر ویژگی
     166            // If the attribute is a taxonomy type
     167            $taxonomy = $attribute->get_name(); // e.g., pa_color
     168            $attribute_data['name'] = wc_attribute_label($taxonomy); // Get attribute label
     169            $terms = wc_get_product_terms($product_id, $taxonomy, ['fields' => 'names']); // Get attribute values
    193170            $attribute_data['values'] = $terms;
    194171        } else {
    195             // اگر ویژگی نوع دلخواه باشد
    196             $attribute_data['name'] = $attribute->get_name(); // نام ویژگی
    197             $attribute_data['values'] = $attribute->get_options(); // مقادیر ویژگی
     172            // If the attribute is a custom (non-taxonomy) type
     173            $attribute_data['name'] = $attribute->get_name(); // Get attribute name
     174            $attribute_data['values'] = $attribute->get_options(); // Get attribute values
    198175        }
    199176
     
    215192function PASAZHAPI_get_pasazh_product_from_wc_product($product)
    216193{
    217 
    218194    $product_status = $product->get_status();
    219195
     
    222198    $pasazh_product->setName($product->get_name());
    223199    $pasazh_product->setUrl(get_permalink($product->get_id()));
    224 
    225200
    226201    $attributes = get_readable_product_attributes($product->get_id());
     
    246221        $pasazh_product->setDescription($description);
    247222
    248 
    249223    // region technical desc
    250224    $technical_description = "";
     
    252226
    253227    foreach ($attrs as $attr) {
    254 
    255228        if ($attr["visible"] != true) continue;
    256229        if ($attr["variation"] == true) continue;
     
    261234        $options_text = "";
    262235        foreach ($data["options"] as $option) {
    263 
    264236            if (strlen($options_text) > 0)
    265237                $options_text .= " | ";
    266238            $options_text .= $option;
    267239        }
    268 
    269240        $technical_description .= $options_text;
    270241    }
     
    277248
    278249    if ($product->is_type('variable')) {
    279 
    280250        $specification_arr = [];
    281 
    282251        foreach ($product->get_available_variations() as $variation) {
    283 
    284252            $is_in_stock = isset($variation['is_in_stock']) && $variation['is_in_stock'] == 1;
    285 
    286253            $specification_object = new \stdClass();
    287254            $specification_object->name = "";
     
    293260            $specification_object->is_in_stock = $is_in_stock;
    294261            $specification_arr [] = $specification_object;
    295 
    296         }
    297 
     262        }
    298263
    299264        if (sizeof($specification_arr) > 0) {
    300265            $pasazh_product->setSpecifications($specification_arr);
    301266        }
    302 
    303     }
    304 
     267    }
    305268
    306269    //region set quantity
     
    361324    } else {
    362325        $pasazh_product->setVisibilityStatus(0);
    363 
    364326    }
    365327
  • pasazh/trunk/readme.txt

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