Plugin Directory

Changeset 2894846


Ignore:
Timestamp:
04/06/2023 08:32:57 AM (3 years ago)
Author:
sitepack
Message:

Plugin update released

Location:
sitepack-connect/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • sitepack-connect/trunk/class.sitepack-connect.php

    r2891776 r2894846  
    11<?php
     2
     3include SITEPACK_CONNECT_PLUGIN_DIR . '/models/class.sitepack-stock.php';
     4include SITEPACK_CONNECT_PLUGIN_DIR . '/models/class.sitepack-stock-location.php';
    25
    36class SitePackConnect
    47{
     8
     9    private static ?SitePackConnect $instance = null;
     10
     11    public static function getInstance(): SitePackConnect
     12    {
     13        if (!isset(self::$instance)) {
     14            self::$instance = new SitePackConnect();
     15
     16            self::$instance->init();
     17        }
     18
     19        return self::$instance;
     20    }
    521
    622    public static function init()
     
    824    }
    925
    10     public function fetchLiveStock(string $importSource, string $ean)
     26    public function fetchLiveStock(string $siteUuid, string $importSource, string $ean): SitePackStock
    1127    {
     28        $sitePackApiHost = apply_filters('sitepack_api_hostname', 'https://api.sitepack.nl');
     29        $url = $sitePackApiHost . '/api/v2/products/%s/%s/%s/connect/stock';
     30        $url = sprintf(
     31            $url,
     32            $siteUuid,
     33            $importSource,
     34            $ean
     35        );
     36
     37        $response = wp_remote_post($url);
     38
     39        if (is_wp_error($response)) {
     40            $errorMessage = $response->get_error_message();
     41
     42            return new SitePackStock(
     43                false,
     44                0,
     45                0,
     46                0,
     47                false,
     48                [],
     49                null,
     50                $errorMessage
     51            );
     52        }
     53
     54
     55        if (empty($response['body'])) {
     56            return new SitePackStock(
     57                false,
     58                0,
     59                0,
     60                0,
     61                false,
     62                [],
     63                null,
     64                'Empty body response'
     65            );
     66        }
     67
     68        $data = json_decode($response['body'], true);
     69
     70        $locations = [];
     71        if (is_array($data['stock']['locations'])) {
     72            foreach ($data['stock']['locations'] as $dataLocation) {
     73                $locations[] = SitePackStockLocation::fromSitePackConnectData($dataLocation);
     74            }
     75        }
     76
     77        return new SitePackStock(
     78            $data['stock']['inStock'],
     79            $data['stock']['quantityAvailable'],
     80            $data['stock']['quantitySupplier'],
     81            0, // TODO in SP API
     82            $data['stock']['allowBackorder'],
     83            $locations,
     84            ($data['stock']['deliveryDate'] !== null) ? new DateTimeImmutable($data['stock']['deliveryDate']) : null,
     85            $data['stock']['errorReason'],
     86        );
    1287    }
    1388
  • sitepack-connect/trunk/models/class.sitepack-category.php

    r2891776 r2894846  
    7676    }
    7777
     78    /**
     79     * @return string
     80     */
     81    public function getCategoryMain(): string
     82    {
     83        return $this->categoryMain;
     84    }
     85
     86    /**
     87     * @return string
     88     */
     89    public function getCategorySub(): string
     90    {
     91        return $this->categorySub;
     92    }
     93
     94    /**
     95     * @return string
     96     */
     97    public function getCategorySubSub(): string
     98    {
     99        return $this->categorySubSub;
     100    }
     101
    78102    public function jsonSerialize(): array
    79103    {
  • sitepack-connect/trunk/models/class.sitepack-stock.php

    r2891775 r2894846  
    2222     * @param string|null $errorReason
    2323     */
    24     private function __construct(
     24    public function __construct(
    2525        bool $hasStock,
    2626        int $stockQuantity,
     
    2929        bool $allowBackorder,
    3030        array $stockLocations,
    31         ?DateTimeImmutable $deliveryDate,
    32         ?string $errorReason
     31        ?DateTimeImmutable $deliveryDate = null,
     32        ?string $errorReason = null
    3333    ) {
    3434        $this->hasStock = $hasStock;
     
    6565    }
    6666
     67    /**
     68     * @return bool
     69     */
     70    public function isHasStock(): bool
     71    {
     72        return $this->hasStock;
     73    }
     74
     75    /**
     76     * @return int
     77     */
     78    public function getStockQuantity(): int
     79    {
     80        return $this->stockQuantity;
     81    }
     82
     83    /**
     84     * @return int
     85     */
     86    public function getStockQuantitySupplier(): int
     87    {
     88        return $this->stockQuantitySupplier;
     89    }
     90
     91    /**
     92     * @return int
     93     */
     94    public function getStockQuantityReserved(): int
     95    {
     96        return $this->stockQuantityReserved;
     97    }
     98
     99    /**
     100     * @return bool
     101     */
     102    public function isAllowBackorder(): bool
     103    {
     104        return $this->allowBackorder;
     105    }
     106
     107    /**
     108     * @return array
     109     */
     110    public function getStockLocations(): array
     111    {
     112        return $this->stockLocations;
     113    }
     114
     115    /**
     116     * @return DateTimeImmutable|null
     117     */
     118    public function getDeliveryDate(): ?DateTimeImmutable
     119    {
     120        return $this->deliveryDate;
     121    }
     122
     123    /**
     124     * @return string|null
     125     */
     126    public function getErrorReason(): ?string
     127    {
     128        return $this->errorReason;
     129    }
     130
     131    /**
     132     * @return array
     133     */
    67134    public function jsonSerialize(): array
    68135    {
    69         return [
    70             'hasStock' => $this->hasStock,
    71             'stockQuantity' => $this->stockQuantity,
    72             'stockQuantitySupplier' => $this->stockQuantitySupplier,
    73             'stockQuantityReserved' => $this->stockQuantityReserved,
    74             'allowBackOrder' => $this->allowBackorder,
    75             'stockLocations' => $this->stockLocations,
    76             'deliveryDate' => $this->deliveryDate,
    77             'errorReason' => $this->errorReason,
    78         ];
     136        return get_object_vars($this);
    79137    }
    80138}
  • sitepack-connect/trunk/readme.txt

    r2891863 r2894846  
    33Tags: connect, woocommerce, cyclesoftware, wilmar, sitepack
    44Requires at least: 5.0
    5 Tested up to: 6.1.1
    6 Stable tag: 1.0.2
     5Tested up to: 6.2
     6Stable tag: 1.1.0
    77License: GPLv2 or later
    88
     
    2121* Categorie indeling aanpassen
    2222* Meerdere updates per dag
    23 * Bijna live voorraad informatie (maximaal 15 minuten oud)
    2423* Productafbeeldingen uitsluiten
    2524* Geen zware processen op je WordPress website
    2625* Premium ondersteuning via onze helpdesk
    2726* Vaste lage maandprijs, zonder verplichte opstartkosten
     27* Voorraad per locatie (Vanaf het Grow abonnement)
    2828
    2929#### Categorie indeling aanpassen
     
    109109== Changelog ==
    110110
     111= 1.1.0 =
     112
     113Release date: 2023-04-06
     114
     115#### Enhancements
     116
     117* You can use the spGetProductStockInformation(int $productId) method to get the live stock information of a product and their related stock locations if the subscription is sufficient
     118* Disable ajax stock information in the product page by using the filter "sitepack_fetch_live_stock" with value "false"
     119* Stock locations added
     120
     121#### Bugfixes
     122
     123* Stock implementation call bugfixes in REST API
     124
    111125= 1.0.2 =
    112126
  • sitepack-connect/trunk/services/class.sitepack-woocommerce.php

    r2891776 r2894846  
    7272        $product->set_stock_quantity(0);
    7373        $product->set_stock_status('outofstock');
    74         if ($data['hasStock'] === true) {
     74        if ((bool)$data['hasStock'] === true || (int)$data['inStock'] >= 1 || (int)$data['stockSupplier'] >= 1) {
    7575            $product->set_stock_status('instock');
    76 
    77             if ($data['inStock'] === 0 && $data['stockSupplier'] >= 1) {
    78                 $product->set_stock_status('onbackorder');
    79             }
    80 
    81             $product->set_stock_quantity($data['inStock'] + $data['stockSupplier']);
     76            $product->set_stock_quantity((int)$data['inStock'] + (int)$data['stockSupplier']);
    8277
    8378            if ($product->get_stock_quantity() < 1) {
     
    9287            if (is_array($json)) {
    9388                $metaData = $json;
    94             }
    95         }
     89
     90                foreach ($metaData as $key => $value) {
     91                    $product->add_meta_data($key, $value);
     92                }
     93            }
     94        }
     95
     96        $product->add_meta_data('import_provider', 'SITEPACK', true);
     97        $product->add_meta_data('import_source', $data['importSource'], true);
     98        $product->add_meta_data('site', $data['site'], true);
     99        $product->add_meta_data('ean', $data['ean'], true);
    96100
    97101        if ($product->get_date_created() === null) {
    98102            $product->set_date_created((new DateTimeImmutable())->format('Y-m-d H:i:s'));
    99103        }
    100 
    101         $metaData = $metaData + [
    102                 'import_provider' => 'SITEPACK',
    103                 'import_source' => $data['importSource'],
    104                 'site' => $data['site'],
    105                 'ean' => $data['ean'],
    106             ];
    107 
    108         $product->set_meta_data($metaData);
    109104
    110105        return $product;
     
    154149    public function findProduct($productId): WC_Product
    155150    {
    156         return new WC_Product_Simple($productId);
     151        return wc_get_product($productId);
    157152    }
    158153
  • sitepack-connect/trunk/sitepack-connect.php

    r2891776 r2894846  
    88Plugin URI: https://sitepack.nl/integraties
    99Description: Connect your eCommerce store with external APIs, using SitePack Connect. Import products with stock information and export orders to your favorite third party software.
    10 Version: 1.0.2
     10Version: 1.1.0
    1111Author: SitePack B.V.
    1212Author URI: https://sitepack.nl
     
    4949    }
    5050}
     51
     52if (!function_exists('spGetProductStockInformation')) {
     53    /**
     54     * Fetch the live stock information
     55     *
     56     * @param int $productId
     57     * @return ?SitePackStock
     58     */
     59    function spGetProductStockInformation(int $productId): ?SitePackStock
     60    {
     61        $connect = SitePackConnect::getInstance();
     62        $product = wc_get_product($productId);
     63
     64        if (!$product instanceof WC_Product) {
     65            return null;
     66        }
     67
     68        if (empty($product->get_meta('site'))
     69            || empty($product->get_meta('import_source'))
     70            || empty($product->get_meta('ean'))
     71        ) {
     72            return null;
     73        }
     74
     75        return $connect->fetchLiveStock(
     76            $product->get_meta('site'),
     77            $product->get_meta('import_source'),
     78            $product->get_meta('ean')
     79        );
     80    }
     81}
     82
     83add_action('init', 'spFetchstock');
     84
     85function spFetchstock()
     86{
     87    if (is_admin()) {
     88        return;
     89    }
     90
     91    $ajaxStock = (bool)apply_filters('sitepack_fetch_live_stock', true);
     92    // TODO: build in stock info
     93}
     94
     95
     96
     97
Note: See TracChangeset for help on using the changeset viewer.