enthusiast-source-woocommerce 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- enthusiast_source_woocommerce-0.1.1/PKG-INFO +96 -0
- enthusiast_source_woocommerce-0.1.1/README.md +80 -0
- enthusiast_source_woocommerce-0.1.1/enthusiast_source_woocommerce/__init__.py +1 -0
- enthusiast_source_woocommerce-0.1.1/enthusiast_source_woocommerce/source.py +103 -0
- enthusiast_source_woocommerce-0.1.1/pyproject.toml +16 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: enthusiast-source-woocommerce
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary:
|
|
5
|
+
Author: jakubl2290
|
|
6
|
+
Requires-Python: >=3.9,<4.0
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Requires-Dist: enthusiast-common (>=0.4.0,<0.5.0)
|
|
14
|
+
Requires-Dist: woocommerce (>=3.0.0,<4.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# WooCommerce Integration
|
|
18
|
+
|
|
19
|
+
## This Enthusiast plugin enables the import of product data directly from WooCommerce.
|
|
20
|
+
|
|
21
|
+
### First, install the `enthusiast-source-woocommerce` package using pip:
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
pip install enthusiast-source-woocommerce
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Then, enable the plugin by adding it to the `settings.py`:
|
|
29
|
+
|
|
30
|
+
```python title="server/pecl/settings.py"
|
|
31
|
+
CATALOG_PRODUCT_SOURCE_PLUGINS = {
|
|
32
|
+
...
|
|
33
|
+
"WooCommerce": "enthusiast_source_woocommerce.WoocommerceProductSource"
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Set WooCommerce consumer key and secret by setting enviroment variables
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
WOO_CONSUMER_KEY=consumerkey
|
|
41
|
+
WOO_CONSUMER_SECRET=consumersecret
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### OR continue reading to implement Consumer Key and Secret instead in sources configuration.
|
|
45
|
+
|
|
46
|
+
Save the changes and restart the web server and the worker.
|
|
47
|
+
|
|
48
|
+
## Syncing WooCommerce Products to a Data Set
|
|
49
|
+
|
|
50
|
+
Log in as an admin user and go to Manage → Data Sets from the left-hand menu.
|
|
51
|
+
|
|
52
|
+
Then. click on "Sources" next to the desired data set.
|
|
53
|
+
|
|
54
|
+
Add a source using “WooCommerce” as the plugin and provide a JSON configuration with the following attributes:
|
|
55
|
+
|
|
56
|
+
### Provide base url to your shop.
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"base_url": "BASE_URL",
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### If you decide to add Consumer Key and Secrect directly to sources
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
...
|
|
69
|
+
"consumer_key": "consumerkey",
|
|
70
|
+
"consumer_secret": "consumersecret"
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### By default synchronization will obtain 20 products per page.
|
|
75
|
+
|
|
76
|
+
You can change this by setting up "per_page" in your config.
|
|
77
|
+
|
|
78
|
+
`Min. 10, max 100`
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
...
|
|
83
|
+
"per_page": 10
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Example configuration for source
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
{
|
|
91
|
+
"base_url": "http://localhost:8080",
|
|
92
|
+
"consumer_key": "mykey",
|
|
93
|
+
"consumer_secret": "mysecret",
|
|
94
|
+
"per_page": 10
|
|
95
|
+
}
|
|
96
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# WooCommerce Integration
|
|
2
|
+
|
|
3
|
+
## This Enthusiast plugin enables the import of product data directly from WooCommerce.
|
|
4
|
+
|
|
5
|
+
### First, install the `enthusiast-source-woocommerce` package using pip:
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
pip install enthusiast-source-woocommerce
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Then, enable the plugin by adding it to the `settings.py`:
|
|
13
|
+
|
|
14
|
+
```python title="server/pecl/settings.py"
|
|
15
|
+
CATALOG_PRODUCT_SOURCE_PLUGINS = {
|
|
16
|
+
...
|
|
17
|
+
"WooCommerce": "enthusiast_source_woocommerce.WoocommerceProductSource"
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Set WooCommerce consumer key and secret by setting enviroment variables
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
WOO_CONSUMER_KEY=consumerkey
|
|
25
|
+
WOO_CONSUMER_SECRET=consumersecret
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
#### OR continue reading to implement Consumer Key and Secret instead in sources configuration.
|
|
29
|
+
|
|
30
|
+
Save the changes and restart the web server and the worker.
|
|
31
|
+
|
|
32
|
+
## Syncing WooCommerce Products to a Data Set
|
|
33
|
+
|
|
34
|
+
Log in as an admin user and go to Manage → Data Sets from the left-hand menu.
|
|
35
|
+
|
|
36
|
+
Then. click on "Sources" next to the desired data set.
|
|
37
|
+
|
|
38
|
+
Add a source using “WooCommerce” as the plugin and provide a JSON configuration with the following attributes:
|
|
39
|
+
|
|
40
|
+
### Provide base url to your shop.
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"base_url": "BASE_URL",
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### If you decide to add Consumer Key and Secrect directly to sources
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
...
|
|
53
|
+
"consumer_key": "consumerkey",
|
|
54
|
+
"consumer_secret": "consumersecret"
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### By default synchronization will obtain 20 products per page.
|
|
59
|
+
|
|
60
|
+
You can change this by setting up "per_page" in your config.
|
|
61
|
+
|
|
62
|
+
`Min. 10, max 100`
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
...
|
|
67
|
+
"per_page": 10
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Example configuration for source
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"base_url": "http://localhost:8080",
|
|
76
|
+
"consumer_key": "mykey",
|
|
77
|
+
"consumer_secret": "mysecret",
|
|
78
|
+
"per_page": 10
|
|
79
|
+
}
|
|
80
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .source import WoocommerceProductSource as WoocommerceProductSource
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from enthusiast_common import ProductDetails, ProductSourcePlugin
|
|
2
|
+
from woocommerce import API
|
|
3
|
+
from urllib.parse import urlparse
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class WoocommerceProductSource(ProductSourcePlugin):
|
|
11
|
+
def __init__(self, data_set_id, config: dict):
|
|
12
|
+
super().__init__(data_set_id, config)
|
|
13
|
+
self._woocommerce_url = config.get("base_url")
|
|
14
|
+
self._per_page = self._validate_per_page(config.get("per_page", 20))
|
|
15
|
+
self._woocommerce_consumer_key, self._woocommerce_consumer_secret = self._get_credentials(config)
|
|
16
|
+
self.wcapi = self._initialize_api()
|
|
17
|
+
|
|
18
|
+
def _validate_per_page(self, per_page):
|
|
19
|
+
return max(10, min(int(per_page), 100))
|
|
20
|
+
|
|
21
|
+
def _get_credentials(self, config):
|
|
22
|
+
if "WOO_CONSUMER_KEY" in os.environ and "WOO_CONSUMER_SECRET" in os.environ:
|
|
23
|
+
return os.environ["WOO_CONSUMER_KEY"], os.environ["WOO_CONSUMER_SECRET"]
|
|
24
|
+
elif "consumer_key" in config and "consumer_secret" in config:
|
|
25
|
+
return config["consumer_key"], config["consumer_secret"]
|
|
26
|
+
else:
|
|
27
|
+
raise ValueError("WooCommerce consumer key and secret not provided")
|
|
28
|
+
|
|
29
|
+
def _check_url_security(self):
|
|
30
|
+
if not self._woocommerce_url:
|
|
31
|
+
raise ValueError("WooCommerce URL is not set")
|
|
32
|
+
|
|
33
|
+
parsed_url = urlparse(self._woocommerce_url)
|
|
34
|
+
if not parsed_url.scheme or not parsed_url.netloc:
|
|
35
|
+
raise ValueError(f"Invalid WooCommerce URL: {self._woocommerce_url}")
|
|
36
|
+
|
|
37
|
+
scheme = parsed_url.scheme.lower()
|
|
38
|
+
match scheme:
|
|
39
|
+
case "https":
|
|
40
|
+
return True
|
|
41
|
+
case "http":
|
|
42
|
+
logger.warning(
|
|
43
|
+
"HTTP protocol detected for WooCommerce URL. This is not recommended for production environments due to security risks. Please consider using HTTPS."
|
|
44
|
+
)
|
|
45
|
+
return False
|
|
46
|
+
case _:
|
|
47
|
+
raise ValueError(f"Unsupported protocol in WooCommerce URL: {scheme}")
|
|
48
|
+
|
|
49
|
+
def _initialize_api(self):
|
|
50
|
+
is_secure = self._check_url_security()
|
|
51
|
+
return API(
|
|
52
|
+
url=self._woocommerce_url,
|
|
53
|
+
consumer_key=self._woocommerce_consumer_key,
|
|
54
|
+
consumer_secret=self._woocommerce_consumer_secret,
|
|
55
|
+
version="wc/v3",
|
|
56
|
+
timeout=30,
|
|
57
|
+
verify_ssl=is_secure,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def fetch(self) -> list[ProductDetails]:
|
|
61
|
+
results = []
|
|
62
|
+
page = 1
|
|
63
|
+
while True:
|
|
64
|
+
try:
|
|
65
|
+
response = self.wcapi.get("products", params={"page": page, "per_page": self._per_page})
|
|
66
|
+
|
|
67
|
+
if response.status_code != 200:
|
|
68
|
+
logger.error(f"Failed to fetch products. Status code: {response.status_code}")
|
|
69
|
+
break
|
|
70
|
+
|
|
71
|
+
products = response.json()
|
|
72
|
+
|
|
73
|
+
if not products:
|
|
74
|
+
break
|
|
75
|
+
|
|
76
|
+
for product in products:
|
|
77
|
+
results.append(self._convert_to_product_details(product))
|
|
78
|
+
|
|
79
|
+
page += 1
|
|
80
|
+
except Exception as e:
|
|
81
|
+
logger.error(f"Error fetching products: {str(e)}")
|
|
82
|
+
break
|
|
83
|
+
|
|
84
|
+
return results
|
|
85
|
+
|
|
86
|
+
def _convert_to_product_details(self, woo_product: dict) -> ProductDetails:
|
|
87
|
+
return ProductDetails(
|
|
88
|
+
entry_id=str(woo_product["id"]),
|
|
89
|
+
name=woo_product["name"],
|
|
90
|
+
slug=woo_product["slug"],
|
|
91
|
+
description=woo_product["description"],
|
|
92
|
+
sku=woo_product["sku"],
|
|
93
|
+
properties=str(
|
|
94
|
+
{
|
|
95
|
+
"status": woo_product["status"],
|
|
96
|
+
"featured": woo_product["featured"],
|
|
97
|
+
"catalog_visibility": woo_product["catalog_visibility"],
|
|
98
|
+
"type": woo_product["type"],
|
|
99
|
+
}
|
|
100
|
+
),
|
|
101
|
+
categories=", ".join(category["name"] for category in woo_product["categories"]),
|
|
102
|
+
price=float(woo_product["price"]) if woo_product["price"] else 0.0,
|
|
103
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "enthusiast-source-woocommerce"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["jakubl2290", "Rafal Cymerys <rafal@upsidelab.io>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
|
|
8
|
+
[tool.poetry.dependencies]
|
|
9
|
+
python = "^3.9"
|
|
10
|
+
enthusiast-common = "^0.4.0"
|
|
11
|
+
woocommerce = "^3.0.0"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["poetry-core"]
|
|
16
|
+
build-backend = "poetry.core.masonry.api"
|