Plugin Directory

Changeset 2840280


Ignore:
Timestamp:
12/28/2022 07:19:48 AM (3 years ago)
Author:
aliparsa
Message:

fix problem of update quantity for variations products

Location:
pasazh/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pasazh/trunk/pasazh.php

    r2835362 r2840280  
    66Plugin URI: https://epasazh.com
    77Description: با استفاده از این افزونه میتوانید فروشگاه خود را به پاساژ متصل نمایید. کافیست آن را فعال نمایید پس از آن به پنل مدیریت خودت در وب سایت پاساژ مراجعه نموده و ادامه مراحل جهت اتصال را تکمیل نمایید.
    8 Version: 1.5
     8Version: 1.6
    99Author: aliparsa
    1010Author URI: https://profiles.wordpress.org/aliparsa/
     
    1515*/
    1616
    17 const PASAZHAPI_papi_version = "1.5";
     17const PASAZHAPI_papi_version = "1.0";
    1818
    1919add_action('rest_api_init', function () {
     
    3434
    3535add_action('woocommerce_update_product', 'PASAZHAPI_sync_product', 10, 1);
     36
     37//region add two webhook for detect delete product
     38add_action( 'before_delete_post', 'PASAZHAPI_sync_product' );
     39add_action( 'save_post', 'PASAZHAPI_sync_product' );
     40//endregion
     41
     42
     43//function wpse_110037_new_posts($post_id){
     44//    $WC_Product = wc_get_product( $post_id);
     45//
     46//    if($WC_Product->status == "trash"){
     47//
     48//    }
     49//}
    3650
    3751//region api functions
     
    5266{
    5367
     68
    5469    $base64 = $data->get_params()["query"];
    5570    $json = base64_decode($base64);
     
    5974    $items = wc_get_products($query);
    6075
     76
     77
    6178    foreach ($items as $product) {
    6279        $pasazh_product = PASAZHAPI_get_pasazh_product_from_wc_product($product);
    6380        $pasazh_products[] = $pasazh_product;
    6481    }
     82
    6583
    6684    $response = [];
     
    7088    $response["products"] = $pasazh_products;
    7189
     90
    7291    return new WP_REST_Response($response, 200);
    7392}
     
    7594function PASAZHAPI_sync_product($product_id)
    7695{
    77 
    7896    try {
    7997        if (get_transient("papi_product_id") == $product_id) return;
    8098        set_transient('papi_product_id', $product_id, 1);
     99
    81100        wp_remote_get("https://epasazh.com/api/webhooks/woocommerce/addon-product-updated/$product_id", [
    82101            "headers" => [
     
    84103            ]
    85104        ]);
     105
     106
    86107    } catch (Exception $e) {
    87108
     
    96117function PASAZHAPI_get_pasazh_product_from_wc_product($product)
    97118{
    98 
    99119
    100120    $pasazh_product = new PASAZHAPIPasazhProduct();
     
    103123    $pasazh_product->setUrl(get_permalink($product->get_id()));
    104124
    105 
    106125    $description = $product->get_description();
    107 
    108     //get description from variations
    109126    if (strlen($description) == 0) {
    110 
    111             foreach ($product->get_attributes() as $attribute) {
    112                 $attribute_data = json_decode(json_encode($attribute['data'], JSON_PRETTY_PRINT));
    113                 $is_visible = $attribute_data->is_visible == 1;
    114                 if ($is_visible) {
    115                     $description .= $attribute_data->name . " : \r\n";
    116                     $description .= implode(',', $attribute_data->options);
    117                     $description .= "\r\n\r\n";
    118                 }
     127        foreach ($product->get_attributes() as $attribute) {
     128            $attribute_data = json_decode(json_encode($attribute['data'], JSON_PRETTY_PRINT));
     129            $is_visible = $attribute_data->is_visible == 1;
     130            if ($is_visible) {
     131                $description .= $attribute_data->name . " : \r\n";
     132                $description .= implode(' , ', $attribute_data->options);
     133                $description .= "\r\n\r\n";
    119134            }
    120 
    121     }
     135        }
     136    }
     137
    122138    $pasazh_product->setDescription($description);
     139
    123140
    124141
     
    126143    $technical_description = "";
    127144    $attrs = $product->get_attributes();
    128     foreach ($attrs as $attr) {
     145
     146    $test = "";
     147    foreach ($attrs as $attr){
    129148
    130149        if ($attr["visible"] != true) continue;
    131150        if ($attr["variation"] == true) continue;
    132 
    133         if (strlen($technical_description) > 0)
     151        if (strlen($technical_description)>0)
    134152            $technical_description .= "\n\n";
    135153        $data = $attr->get_data();
    136154        $technical_description .= $data["name"] . " : \n";
    137155        $options_text = "";
    138         foreach ($data["options"] as $option) {
     156        foreach ($data["options"] as $option){
     157
    139158            if (strlen($options_text) > 0)
    140                 $options_text .= " | ";
    141             $options_text .= $option;
    142 
    143         }
    144         $technical_description .= $options_text;
     159                $options_text .=  " | " ;
     160            $options_text .=  $option ;
     161
     162            $test .= $options_text;
     163
     164        }
     165
     166        $technical_description .= $options_text ;
    145167    }
    146168
     
    155177        $specification_arr = [];
    156178
     179
    157180        foreach ($product->get_available_variations() as $variation) {
     181
     182            $is_in_stock = isset($variation['is_in_stock']) && $variation['is_in_stock'] == 1;
    158183
    159184            $specification_object = new \stdClass();
     
    164189            $specification_object->name = trim($specification_object->name);
    165190            $specification_object->price = $variation["display_price"];
     191            $specification_object->is_in_stock = $is_in_stock;
    166192            $specification_arr [] = $specification_object;
    167193
    168194        }
     195
    169196
    170197        if (sizeof($specification_arr) > 0) {
     
    179206        $quantity = $product->get_stock_quantity();
    180207    } else {
    181         if ($product->is_in_stock()) {
     208        if($product->is_in_stock()){
    182209            $quantity = 1;
    183210        }
  • pasazh/trunk/readme.txt

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