Plugin Directory

Changeset 2062929


Ignore:
Timestamp:
04/04/2019 11:19:09 AM (7 years ago)
Author:
altoshift
Message:

update plugin 1.0.1

Location:
altoshift-woocommerce/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • altoshift-woocommerce/trunk/AltoshiftWoocommercePlugin.php

    r2053142 r2062929  
    44Plugin Name:  Altoshift Woocommerce Plugin
    55Description:  Plugin for Altoshift search integration into your Woocommerce shop
    6 Version:      1.0.0
     6Version:      1.0.1
    77Author:       Altoshift
    88License:      GPL2
  • altoshift-woocommerce/trunk/includes/classes/Feed/Feed.php

    r2053142 r2062929  
    1515    public $products = [];
    1616    public $categories = [];
     17
     18    public static $predefinedFields = array(
     19        "id"
     20    );
     21
     22    public $allFields = array();
    1723
    1824    public static function registerUrls()
     
    7884            $this->createElement($headerName, $headerValue);
    7985        }
     86
     87        echo '<allFields>';
     88        foreach($this->allFields as $field)
     89        {
     90            $this->createElement('field', $field);
     91        }
     92        echo '</allFields>';
    8093
    8194        foreach($this->categories as $category)
     
    93106            echo '<item>';
    94107            foreach ($product as $field => $value) {
    95                 if ($field == 'categories' || $field === 'categoriesTree') {
     108                if ($field == 'categoryIds' || $field === 'categoryTree') {
    96109                    $this->renderProductCategories($value, $field);
    97110                    continue;
     
    108121    }
    109122
    110     private function renderProductCategories($categories, $element = 'categories')
     123    private function renderProductCategories($categories, $element = 'categoryIds')
    111124    {
    112125        echo '<'.$element.'>';
     
    196209        }
    197210
     211        $moreFields = array();
     212        if (isset($_GET['more_fields']) && !empty($_GET['more_fields'])) {
     213            try {
     214                $moreFields = explode(",", $_GET['more_fields']);
     215            } catch(Exception $e) {
     216                $moreFields = array();
     217            }
     218        }
     219
    198220        $query = new \WP_Query($args);
    199221
    200222        foreach ($query->posts as $post) {
    201223            $product = WC()->product_factory->get_product($post->ID);
    202             $imageId = get_post_thumbnail_id($post->ID);
    203             $imageLinks = wp_get_attachment_image_src($imageId, 'full');
    204 
    205             $productCategoriesTree = $this->getProductCategoryTree($post->ID);
    206             $productCategories = $this->getProductCategories($post->ID);
    207 
    208             $this->products[] = array(
    209                 'id' => $post->ID,
    210                 'link' => get_permalink($post),
    211                 'title' => $product->name,
    212                 'description' => $product->description,
    213                 'image_link' => is_array($imageLinks) && count($imageLinks) ? $imageLinks[0] : '',
    214                 'availability' => $product->is_purchasable() && $product->is_in_stock() ? 'in stock' : 'out of stock',
    215                 'price' => $product->regular_price,
    216                 'sale_price' => $product->sale_price,
    217                 'categoriesTree' => $productCategoriesTree,
    218                 'categories' => $productCategories,
    219             );
     224
     225            $productData = $this->productToArray($post, $product);
     226            try {
     227                if (!count($this->allFields)) {
     228                    $this->allFields = $this->getAdditionalFields($productData);
     229                }
     230            } catch (Exception $e) {
     231
     232            }
     233
     234            $productItem = array(
     235                'id' => $productData['id'],
     236            );
     237
     238            foreach($moreFields as $field) {
     239                if (!isset($productData[$field])) {
     240                    continue;
     241                }
     242                $productItem[$field] = $productData[$field];
     243            }
     244
     245            $this->products[] = $productItem;
    220246        }
    221247    }
     
    260286        echo "<![CDATA[$value]]>";
    261287    }
     288
     289    private function productToArray($post, $product) {
     290        $imageId = get_post_thumbnail_id($post->ID);
     291        $imageLinks = wp_get_attachment_image_src($imageId, 'full');
     292        $productCategoriesTree = $this->getProductCategoryTree($post->ID);
     293        $productCategories = $this->getProductCategories($post->ID);
     294
     295        $data = $this->accessProtected($product, "data");
     296        $data = array_merge($data, array(
     297            'id' => $post->ID,
     298            'link' => get_permalink($post),
     299            'title' => $product->name,
     300            'description' => $product->description,
     301            'image_link' => is_array($imageLinks) && count($imageLinks) ? $imageLinks[0] : '',
     302            'availability' => $product->is_purchasable() && $product->is_in_stock() ? 'in stock' : 'out of stock',
     303            'price' => $product->regular_price,
     304            'sale_price' => $product->sale_price,
     305            'categoryTree' => $productCategoriesTree,
     306            'categoryIds' => $productCategories,
     307        ));
     308
     309        unset($data['name']);
     310        unset($data['regular_price']);
     311
     312        return $data;
     313    }
     314
     315    private function accessProtected($obj, $prop) {
     316        $reflection = new \ReflectionClass($obj);
     317        $property = $reflection->getProperty($prop);
     318        $property->setAccessible(true);
     319        return $property->getValue($obj);
     320    }
     321
     322    private function getAdditionalFields($productData) {
     323        $productFields = array_keys($productData);
     324        return array_diff($productFields, self::$predefinedFields);
     325    }
    262326}
  • altoshift-woocommerce/trunk/readme.txt

    r2053357 r2062929  
    8282
    8383== Changelog ==
     84= 1.0.1 =
     85* Add new Feed parser for new feature
    8486
    8587= 1.0 =
Note: See TracChangeset for help on using the changeset viewer.