mysql 如何使用Python使用WooCommerce API添加到购物车?

oxf4rvwz  于 11个月前  发布在  Mysql
关注(0)|答案(1)|浏览(84)
def reserve_image(image_id, user_id):
try:
    connection_images = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=database
    )
    connection_users = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=user_database
    )

    cursor_images = connection_images.cursor()
    cursor_users = connection_users.cursor()

    # Kullanıcının seçili imageini kontrol et
    cursor_users.execute("SELECT selected_image FROM users WHERE id = %s", (user_id,))
    selected_image = cursor_users.fetchone()[0]
    if selected_image:
        # Kullanıcının zaten seçili bir imagei varsa işlemi sonlandır
        flash("You have already selected a image.", "error")
        return

    # Imagein rezerve durumunu kontrol et
    cursor_images.execute("SELECT Reserved FROM images WHERE Image_ID = %s", (image_id,))
    reserved = cursor_images.fetchone()[0]
    if reserved:
        # Image zaten rezerve edilmişse işlemi sonlandır
        flash("The image is already reserved.", "error")
        return

    # WooCommerce API ile ödeme işlemi gerçekleştirme
    payment_data = {
        "payment_method": "stripe",  # Ödeme yöntemi
        "payment_method_title": "Credit Card",  # Ödeme yöntemi başlığı
        "set_paid": True  # Ödemeyi tamamlandı olarak işaretleme
    }
    response = wcapi.post("orders", payment_data)
    if response.status_code != 201 or response.json().get("payment_status") != "paid":
        # Ödeme başarısız ise hata mesajı göster
        flash("An error occurred while processing the payment.", "error")
        return

    # Ödeme başarılıysa veritabanında ödeme durumunu güncelleme
    payment_status = "Paid"  # Ödeme durumu
    cursor_users.execute("UPDATE users SET payment_status = %s WHERE id = %s", (payment_status, user_id))
    connection_users.commit()

    # Sepete ürün ekleme işlemi
    product_id = 17  # Eklenecek ürünün ID'si
    quantity = 1  # Eklenecek ürünün miktarı
    data = {
        "product_id": product_id,
        "quantity": quantity
    }
    response = wcapi.post("cart/items", data)
    if response.status_code != 201:
        # Sepete ekleme başarısız ise hata mesajı göster
        flash("An error occurred while adding the item to the cart.", "error")
        return

    # Image veritabanında rezervasyon işlemi
    cursor_images.execute("UPDATE images SET Reserved = TRUE WHERE Image_ID = %s", (image_id,))
    connection_images.commit()

    cursor_images.close()
    cursor_users.close()
    connection_images.close()
    connection_users.close()
except mysql.connector.Error as error:
    print("Hata oluştu:", error)`

字符串
所以,我的功能是这样的.我有一个网页,包括您可以点击和保留它们的图像.我想使用的方式,当用户点击它的保留按钮.它会添加到购物车到用户的购物车,它会工作,如果它的支付.然后用户将上传图像.
我做了一个函数来测试API,并尝试手动添加到购物车。

wcapi = API(
    url=store_url,
    consumer_key=consumer_key,
    consumer_secret=consumer_secret,
    version="wc/v3"
)

# Eklenecek ürünün verileri
product_id = 17
quantity = 1

# Sepete ürün ekleme işlemi
def add_to_cart(product_id):
    data = {
        'product_id': product_id,
        'quantity': quantity,
    }
    print(data)#17
    response = wcapi.post("cart/items", data)
    print(response.json())
    # İstek sonucunu kontrol etme
    if response.status_code == 200:
        print('Ürün başarıyla sepete eklendi.')
    else:
        print('Ürün sepete eklenirken bir hata oluştu.')
        print(data)

# Sepete ürün ekleme işlemini çağırma
add_to_cart(product_id)


它给出了“'code':'rest_no_route','message':'您的当前状态已被禁用,' data ':' status ':404}}"。
我得到了产品。'product_id':17,'quantity':1}
我是新来的。我知道我可能有批判性思维或实践错误。试图解决这个问题,以便我可以继续其他问题。基本上,我的商店里只有一个产品。我希望能够将该产品添加到购物车。如果我能设法添加到购物车并支付,然后相应地刷新相册页面。然后,它将是成功的。如果你来到这里并阅读所有,先谢谢你了。保重!

5ssjco0h

5ssjco0h1#

在WooCommerce中,您似乎使用了错误的URL来将商品添加到购物车。要将商品添加到购物车,您应该使用正确的端点,其格式如下:

BaseURL + wp-json/wc/store/v1/cart/add-item

字符串
下面是一个如何在Pyhton代码中构造请求的示例:

import requests

# Define your WooCommerce store URL, consumer key, and consumer secret
base_url = 'https://your-woocommerce-store.com'
consumer_key = 'your-consumer-key'
consumer_secret = 'your-consumer-secret'

# Define the endpoint to add items to the cart
add_item_endpoint = '/wp-json/wc/store/v1/cart/add-item'

# Construct the full API URL
api_url = base_url + add_item_endpoint

# Define the product data you want to add to the cart
product_data = {
    'product_id': 17,
    'quantity': 1
 }

# Send the POST request to add the product to the cart
response = requests.post(api_url, json=product_data, auth=(consumer_key, 
consumer_secret))

if response.status_code == 200:
    print("Product added to the cart successfully.")
    # You can handle the response data here if needed.
else:
    print(f"Error: {response.status_code} - {response.text}")


请确保将“your-wooCommerce-store.com”、“your-consumer-key”和“your-consumer-secret”替换为您实际的WooCommerce商店的详细信息。这将允许您使用REST API正确地将商品添加到WooCommerce商店的购物车中。
如果您遇到任何其他问题,请仔细检查您的身份验证详细信息,并确保您的WooCommerce商店已正确配置为接受API请求。

相关问题