Changeset 2894846
- Timestamp:
- 04/06/2023 08:32:57 AM (3 years ago)
- Location:
- sitepack-connect/trunk
- Files:
-
- 6 edited
-
class.sitepack-connect.php (modified) (2 diffs)
-
models/class.sitepack-category.php (modified) (1 diff)
-
models/class.sitepack-stock.php (modified) (3 diffs)
-
readme.txt (modified) (3 diffs)
-
services/class.sitepack-woocommerce.php (modified) (3 diffs)
-
sitepack-connect.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sitepack-connect/trunk/class.sitepack-connect.php
r2891776 r2894846 1 1 <?php 2 3 include SITEPACK_CONNECT_PLUGIN_DIR . '/models/class.sitepack-stock.php'; 4 include SITEPACK_CONNECT_PLUGIN_DIR . '/models/class.sitepack-stock-location.php'; 2 5 3 6 class SitePackConnect 4 7 { 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 } 5 21 6 22 public static function init() … … 8 24 } 9 25 10 public function fetchLiveStock(string $ importSource, string $ean)26 public function fetchLiveStock(string $siteUuid, string $importSource, string $ean): SitePackStock 11 27 { 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 ); 12 87 } 13 88 -
sitepack-connect/trunk/models/class.sitepack-category.php
r2891776 r2894846 76 76 } 77 77 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 78 102 public function jsonSerialize(): array 79 103 { -
sitepack-connect/trunk/models/class.sitepack-stock.php
r2891775 r2894846 22 22 * @param string|null $errorReason 23 23 */ 24 p rivatefunction __construct(24 public function __construct( 25 25 bool $hasStock, 26 26 int $stockQuantity, … … 29 29 bool $allowBackorder, 30 30 array $stockLocations, 31 ?DateTimeImmutable $deliveryDate ,32 ?string $errorReason 31 ?DateTimeImmutable $deliveryDate = null, 32 ?string $errorReason = null 33 33 ) { 34 34 $this->hasStock = $hasStock; … … 65 65 } 66 66 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 */ 67 134 public function jsonSerialize(): array 68 135 { 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); 79 137 } 80 138 } -
sitepack-connect/trunk/readme.txt
r2891863 r2894846 3 3 Tags: connect, woocommerce, cyclesoftware, wilmar, sitepack 4 4 Requires at least: 5.0 5 Tested up to: 6. 1.16 Stable tag: 1. 0.25 Tested up to: 6.2 6 Stable tag: 1.1.0 7 7 License: GPLv2 or later 8 8 … … 21 21 * Categorie indeling aanpassen 22 22 * Meerdere updates per dag 23 * Bijna live voorraad informatie (maximaal 15 minuten oud)24 23 * Productafbeeldingen uitsluiten 25 24 * Geen zware processen op je WordPress website 26 25 * Premium ondersteuning via onze helpdesk 27 26 * Vaste lage maandprijs, zonder verplichte opstartkosten 27 * Voorraad per locatie (Vanaf het Grow abonnement) 28 28 29 29 #### Categorie indeling aanpassen … … 109 109 == Changelog == 110 110 111 = 1.1.0 = 112 113 Release 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 111 125 = 1.0.2 = 112 126 -
sitepack-connect/trunk/services/class.sitepack-woocommerce.php
r2891776 r2894846 72 72 $product->set_stock_quantity(0); 73 73 $product->set_stock_status('outofstock'); 74 if ( $data['hasStock'] === true) {74 if ((bool)$data['hasStock'] === true || (int)$data['inStock'] >= 1 || (int)$data['stockSupplier'] >= 1) { 75 75 $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']); 82 77 83 78 if ($product->get_stock_quantity() < 1) { … … 92 87 if (is_array($json)) { 93 88 $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); 96 100 97 101 if ($product->get_date_created() === null) { 98 102 $product->set_date_created((new DateTimeImmutable())->format('Y-m-d H:i:s')); 99 103 } 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);109 104 110 105 return $product; … … 154 149 public function findProduct($productId): WC_Product 155 150 { 156 return new WC_Product_Simple($productId);151 return wc_get_product($productId); 157 152 } 158 153 -
sitepack-connect/trunk/sitepack-connect.php
r2891776 r2894846 8 8 Plugin URI: https://sitepack.nl/integraties 9 9 Description: 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.210 Version: 1.1.0 11 11 Author: SitePack B.V. 12 12 Author URI: https://sitepack.nl … … 49 49 } 50 50 } 51 52 if (!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 83 add_action('init', 'spFetchstock'); 84 85 function 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.