Forum in maintenance, we will back soon 🙂
Woocommerce Product Updater
Hi Hasan,
I have a challenge for you which i am sure you would be able to solve for me and i know it would be so useful for a lot of other people who use Woocommerce or are learning Python.
So i run a Woocommerce store and i had a brick wall i had to over come. So i though i'd ask ChatGPT if there was a way to solve my issue.
Here is my question and the answer i got back from ChatGPT:-
i have a problem i need to solve. I have a woocommerce website. I have a supplier who can supply me with 1000's of products. They have given me a product file which i have uploaded to my site. They also have a file that lives on their ftp, password protected site that is updated 2 times a day with stock quantities and prices. The file is in csv format I need to setup an automated way of making sure the sock levels and the pricing on my site are updated regularly to reflect the suppliers file. The solution must be totally hands off, once setup, and reliable. It should also look for products whos price or stock has changed, since the last update and only update those products on my site. What solution do you propose?Â
ChatGPT
To solve this problem, you need a solution that automates the process of downloading the CSV file from your supplier's FTP server, comparing it with your current WooCommerce product data, and updating only the products that have had changes in stock levels or prices. Here is a proposed solution:
Tools Needed:
- WooCommerce REST API: To interact with your WooCommerce store programmatically.
- Python: For scripting the automation process.
- Scheduled Task/Job Scheduler: To run the script at regular intervals (e.g., cron job for Unix/Linux, Task Scheduler for Windows).
Steps to Implement the Solution:
Set Up Access to FTP Server:
- Use Python's
ftplib
library to connect to the supplier's FTP server and download the CSV file.Process the CSV File:
- Use
pandas
orcsv
library in Python to read the CSV file and store it in a DataFrame or dictionary for easy comparison.Compare with Existing Product Data:
- Use WooCommerce REST API to fetch current product data from your site.
- Compare the stock levels and prices from the downloaded CSV with the current data on your WooCommerce store.
Update Changed Products:
- Use WooCommerce REST API to update only the products that have changes in stock levels or prices.
Schedule the Script:
- Use a cron job (Linux) or Task Scheduler (Windows) to run the script at the desired intervals (e.g., twice a day).
Example Python Script:
Here is a simplified example of how the script might look:
pythonimport ftplib
import pandas as pd
import requests
import json
from woocommerce import API# WooCommerce API credentials
wcapi = API(
url="https://your-woocommerce-site.com",
consumer_key="ck_your_consumer_key",
consumer_secret="cs_your_consumer_secret",
version="wc/v3"
)# FTP server credentials
ftp_server = "ftp.supplier.com"
ftp_user = "username"
ftp_password = "password"
remote_file = "supplier_data.csv"
local_file = "local_supplier_data.csv"# Download file from FTP
def download_csv():
with ftplib.FTP(ftp_server) as ftp:
ftp.login(user=ftp_user, passwd=ftp_password)
with open(local_file, 'wb') as f:
ftp.retrbinary(f'RETR {remotes_file}', f.write)# Update WooCommerce products
def update_products():
# Read the downloaded CSV file
supplier_data = pd.read_csv(local_file)for index, row in supplier_data.iterrows():
product_id = row['product_id']
new_stock = row['stock']
new_price = row['price']# Fetch current product data from WooCommerce
product = wcapi.get(f"products/{product_id}").json()
current_stock = product['stock_quantity']
current_price = product['price']# Check for changes
if new_stock != current_stock or new_price != current_price:
data = {
"stock_quantity": new_stock,
"regular_price": str(new_price)
}
wcapi.put(f"products/{product_id}", data)# Main function
if __name__ == "__main__":
download_csv()
update_products()
Scheduling the Script:
For Linux (using cron):
- Open the crontab file:
shcrontab -e
- Add the following line to run the script twice a day:
sh0 0,12 * * * /usr/bin/python3 /path/to/your_script.py
For Windows (using Task Scheduler):
- Open Task Scheduler and create a new task.
- Set the trigger to run twice a day.
- Set the action to run the Python script with the Python interpreter.
Conclusion:
This solution leverages Python and the WooCommerce REST API to automate the update process. Once set up, it will run at the specified intervals, download the latest CSV file, compare it with the current data, and update only the products that have changed, ensuring your WooCommerce site always reflects the latest stock levels and prices from your supplier.
Would you be able to create a video that would take this problem and use the instructions provided by ChatGPT and show us how we can make this solution work?
I think that would be a great project for us to learn from.
Thanks in advance Hasan @adminÂ
@rajsidhu you need a dedicated server to process the data like this. The amount of data can quickly overwhelm any VPS. There are also a lot of DevOps that you also need to consider.Â
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
so you need to fetch the data from the target server and then update woocommerce; this definitely needs a custom solution and custom coding, I don't think there is an automated no code approach
you will need to read the CSV, and using the woocommerce hooks and functions, you will update the products.Â
can you send like 5-10 records of the CSV to see how it is structured?Â
And it's very much dependent on the data service provider; so you need custom code for each different service provider.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@admin Here is an Excel spreadsheet that contains the structure of the products to be uploaded.
Once they are uploaded, the products will be available to buy on our Woocommerce website.
But every day this file will be update with stock and any price updates so the automation will be just for the updated info. coming through every day.
Hope that makes sense.
Â
@rajsidhu is it always going to be this one sheet? If so, then this would be a good fit for automation tools like Make.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@rajsidhu yeah as @ssadvisor mentioned, you can use no code automation tools like Make to read and update the woocomerce products.Â
I don’t know if woocommerce have all the endpoints to do so. But anyway, it is not so complicated.
@ssadvisor So the csv that contains the updated information is always going to be the same file name and location. It just get updated with new stock levels and prices (if they change that is).
What do you think?
@rajsidhu yes, definitely doable with Make. Try it and if you need help add SSAdvisor as a team member for me to take a look.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@ssadvisor @admin How would i start to create this automation, Can you advise please?
@rajsidhu start here https://learnwithhasan.com/no-code-ai-system-topic-research/
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@ssadvisor @admin not sure how this will work for me.
I want to create an automation that will update prices and stock for products in my Woocommerce store.
Friend, sorry, but how can we help here?
you can use Make to do this with no code, try it, and we will help.Â
@rajsidhu Try building the foundation yourself and if you faced problems we can help you in this thread.