use anyhow::Result; use tracing::info; use rust_woocommerce::Attribute; use rust_woocommerce::ProductVariation; use rust_woocommerce::{ApiClient, Config}; use rust_woocommerce::{Product, ProductType}; #[tokio::main] async fn main() -> Result<()> { tracing_subscriber::fmt::init(); let config = Config::new("woo.toml")?; let client = ApiClient::new(&config)?; let products = client.list_all::().await?; let random_variable_id = products .iter() .find(|p| !p.variations.is_empty()) .map(|p| p.id) .unwrap_or_default(); let variations = client .list_all_subentities::(random_variable_id) .await?; info!( "Got {} variations for product with id: {random_variable_id}", variations.len() ); let retrieved_variation: ProductVariation = client .retrieve_subentity( random_variable_id, variations.first().map(|v| v.id).unwrap_or_default(), ) .await?; info!("Retrieved variation has sku: {}", retrieved_variation.sku); let attribute = Attribute::builder() .name("Test Attribute") .option("Best") .option("Test") .variation() .visible() .build(); let new_variable_product = Product::builder() .name("Test Product For Example") .product_type(ProductType::Variable) .featured() .short_description("The most professional description") .sku("product for test 42") .regular_price("6969") .manage_stock() .stock_quantity(42) .weight("50") .dimensions("4", "3", "2") .shipping_class("large") .images("https://cs14.pikabu.ru/post_img/2021/06/27/7/1624794514137159585.jpg") .attribute(attribute) .build(); let created: Product = client.create(new_variable_product).await?; info!("Create product {} with id: {}", created.name, created.id); let variation = ProductVariation::builder() .sku(format!("{} Best", created.sku)) .regular_price("6969") .manage_stock() .stock_quantity(96) .weight("52") .dimensions("5", "4", "3") .attribute(None, "Test Attribute", "Best") .build(); let batch_create_variation = vec![variation.clone()]; let created_variation: ProductVariation = client.create_subentity(created.id, variation).await?; info!( "Variation {} created with price: {}", created_variation.sku, created_variation.price ); let update = ProductVariation::builder().regular_price("7000").build(); let updated_variation: ProductVariation = client .update_subentity(created.id, created_variation.id, update) .await?; info!( "Variation {} updated with price: {}", updated_variation.sku, updated_variation.price ); let deleted_variation: ProductVariation = client .delete_subentity(created.id, updated_variation.id) .await?; info!("Variation {} deleted", deleted_variation.sku); let batch_created_variation: Vec = client .batch_create_subentity(created.id, batch_create_variation) .await?; let bcv_id = batch_created_variation .first() .map(|v| v.id) .unwrap_or_default(); let batch_update_variation = vec![ProductVariation::builder() .id(bcv_id) .regular_price("777") .build()]; let _batch_updated_variation: Vec = client .batch_update_subentity(created.id, batch_update_variation) .await?; let _batch_deleted_variation: Vec = client .batch_delete_subentity(created.id, vec![bcv_id]) .await?; let deleted: Product = client.delete(created.id).await?; info!("Product {} deleted", deleted.name); Ok(()) }