Plugin Directory

Changeset 1868048


Ignore:
Timestamp:
05/03/2018 11:00:52 AM (8 years ago)
Author:
chatx
Message:

Fix bugs and clean the code

Location:
chatx-ai/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • chatx-ai/trunk/chatx-ai.php

    r1863592 r1868048  
    44 * Plugin URI:        https://chatx.ai
    55 * Description:       Advanced Instant Search
    6  * Version:           0.1.9
     6 * Version:           0.2.0
    77 * Author:            chatx.ai
    88 * Author URI:        https://chatx.ai
  • chatx-ai/trunk/includes/class-chatx-ai-rest-api.php

    r1863159 r1868048  
    179179    }
    180180
     181    static function getAttributeValuesArray($isTax, $values){
     182        if($isTax){
     183            return explode(',', str_replace(', ', ',', $values));
     184        }
     185        else{
     186            return explode('|', str_replace(' | ', '|', str_replace('| ', '|', str_replace(' |', '|', $values))));
     187        }
     188    }
     189
     190    static function getProductAttributes($wooProduct){
     191
     192        $attributes = [];
     193        $attributeId = 0;
     194
     195        foreach ( $wooProduct->get_attributes() as $attributeKey => $attribute ) {
     196
     197            $attributeKeyFormatted = str_replace('pa_', '', $attributeKey);
     198
     199            if(is_array($attribute)){
     200
     201                $attrTerms = get_terms($attribute['name']);
     202
     203                $attributeOptions = [];
     204
     205                $attributeTerms = is_array($attrTerms) ? $attrTerms : [];
     206                foreach ($attributeTerms as $attributeTerm) {
     207                    $attributeOptions[] = $attributeTerm->slug;
     208                }
     209
     210                $attributes[] = [
     211                    'id' => $wooProduct->get_id() . '-' . $attributeId,
     212                    'name' => ucfirst(str_replace('pa_', '', $attribute['name'])),
     213                    'key' => $attributeKeyFormatted,
     214                    'options' => $attributeOptions,
     215                    'is_taxonomy' => $attribute['is_taxonomy'],
     216                    'position' => $attribute['position'],
     217                    'visible' => $attribute['is_visible'],
     218                    'variation' => $attribute['is_variation'],
     219                    'values' => Chatx_Ai_Rest_API::getAttributeValuesArray($attribute['is_taxonomy'], $wooProduct->get_attribute($attribute['name']))
     220                ];
     221
     222            }
     223            else{
     224
     225                $attributes[] = [
     226                    'id' => $wooProduct->get_id() . '-' . $attribute->get_id(),
     227                    'name' => ucfirst(str_replace('pa_', '', $attribute->get_name())),
     228                    'key' => $attributeKeyFormatted,
     229                    'options' => $attribute->get_slugs( ),
     230                    'is_taxonomy' => $attribute->is_taxonomy(),
     231                    'position' => $attribute->get_position(),
     232                    'visible' => $attribute->get_visible(),
     233                    'variation' => $attribute->get_variation(),
     234                    'values' => Chatx_Ai_Rest_API::getAttributeValuesArray($attribute['is_taxonomy'], $wooProduct->get_attribute($attribute->get_name()))
     235                ];
     236
     237            }
     238
     239            $attributeId++;
     240        }
     241
     242        return $attributes;
     243    }
     244
    181245    static function getProductInFormat($product){
    182246
     
    197261
    198262        $variations = [];
    199         $attributes = [];
    200263
    201264        if($wooProduct->is_type( 'variable' )){
     
    215278                    'inventory_quantity' => $variation['is_in_stock'] ? 1 : 0,
    216279                    'vendor' => null,
    217                     'title' => null
     280                    'title' => null,
     281                    'options' => []
    218282                ];
    219283
    220                 $optionIndex = 0;
    221284                foreach ($variation['attributes'] as $attributeKey => $attribute) {
    222                     $optionIndex++;
    223                     $variationFormated['option_' . $optionIndex] = $attribute;
     285
     286                    $attributeKeyFormatted = str_replace('attribute_', '', str_replace('attribute_pa_', '', $attributeKey));
     287                    $variationFormated['options'][$attributeKeyFormatted] = $attribute;
    224288                }
    225289
    226290                $variations[] = $variationFormated;
    227             }
    228 
    229             $attributeId = 0;
    230             foreach ( $wooProduct->get_attributes() as $attributeKey => $attribute ) {
    231 
    232                 if(is_array($attribute)){
    233 
    234                     $attrTerms = get_terms($attribute['name']);
    235 
    236                     $attributeOptions = [];
    237 
    238                     $attributeTerms = is_array($attrTerms) ? $attrTerms : [];
    239                     foreach ($attributeTerms as $attributeTerm) {
    240                         $attributeOptions[] = $attributeTerm->slug;
    241                     }
    242 
    243                     $attributes[] = [
    244                         'id' => $product->ID . '-' . $attributeId,
    245                         'name' => ucfirst(str_replace('pa_', '', $attribute['name'])),
    246                         'options' => $attributeOptions,
    247                         'is_taxonomy' => $attribute['is_taxonomy'],
    248                         'position' => $attribute['position'],
    249                         'visible' => $attribute['is_visible'],
    250                         'variation' => $attribute['is_variation']
    251                     ];
    252 
    253                 }
    254                 else{
    255 
    256                     $attributes[] = [
    257                         'id' => $product->ID . '-' . $attribute->get_id(),
    258                         'name' => ucfirst(str_replace('pa_', '', $attribute->get_name())),
    259                         'options' => $attribute->get_slugs( ),
    260                         'is_taxonomy' => $attribute->is_taxonomy(),
    261                         'position' => $attribute->get_position(),
    262                         'visible' => $attribute->get_visible(),
    263                         'variation' => $attribute->get_variation()
    264                     ];
    265 
    266                 }
    267 
    268                 $attributeId++;
    269291            }
    270292        }
     
    293315            'sku' => $wooProduct->get_sku(),
    294316            'variations' => $variations,
    295             'attributes' => $attributes,
     317            'attributes' => Chatx_Ai_Rest_API::getProductAttributes($wooProduct),
    296318            'price' => $wooProduct->get_price(),
    297319            'compare_at_price' => $wooProduct->get_regular_price(),
  • chatx-ai/trunk/readme.txt

    r1863592 r1868048  
    33Tags: instant search, advanced search, woocommerce, image search, voice search
    44Tested up to: 4.9.5
    5 Stable tag: 0.1.9
     5Stable tag: 0.2.0
    66Requires at least: 4.0
    77
     
    110110
    111111== Installation ==
    112 Install like any other plugin, directly from your plugins page. After you activate the plugin, you will be prompted to “Connect with ChatX.ai”. This will bring the chatbot alive and it’s a very important step for everything to work well.
     112Install like any other plugin, directly from your plugins page.
    113113
    114114== How to uninstall ChatX.ai ==
Note: See TracChangeset for help on using the changeset viewer.