c14r/woocommerce-api

WooCommerce REST-API 的包装器

v1.0.0 2023-09-21 07:05 UTC

This package is auto-updated.

Last update: 2024-09-21 10:07:15 UTC


README

packagist version

本包允许用户在Laravel应用程序中轻松消费由automattic/woocommerce提供的REST API。

安装

安装 WooCommerce-API 的推荐方式是通过 Composer

composer require c14r/woocommerce-api

.env 文件

WOOCOMMERCE_URL=https://www.your-shop.com/
WOOCOMMERCE_KEY=ck_???
WOOCOMMERCE_SECRET=cs_???
WOOCOMMERCE_VERSION=wc/v3

配置

如果您想更改默认配置或想使用多个连接,可以通过 ``php artisan vendor:publish --tag=woocommerce-config` 发布 config/woocommerce.php

目录

待办事项

README.md

文档尚不完整。

德国化

https://vendidero.de/dokument/shipments-rest-api

  • 配送
  • 取消
  • 发票

使用方法

获取实例

use C14r\Woocommerce\V3\API;

// via helper function
$api = woo();

// via dependency injection
public function example(API $api)

// via singleton
$api = API::getInstance();

过滤

缓存

每个 get()all() 请求都可以缓存,您只需调用 cache()cacheAll() 方法。

$order = $api->order($order_id)->cache(); // instead of ...->get()
$orders = $api->orders()->cacheAll(); // instead of ...->all()

配置

分页

与调用 ->per_page(25)->page($page)->get()->all() 相比,您可以使用默认的 Laravel 分页。有关更多信息,请参阅 Laravel 文档

$orders = $api->paginate();

服务类

服务类通过将 API 调用封装到专用方法中来简化 API 使用,从而提高代码的可读性、可维护性,并减少 API 交互复杂性。

ProductService

$service->increseStockQuantity(int $product_id, int $amount = 1);
$service->decreseStockQuantity(int $product_id, int $amount = 1);
$service->setStockQuantity(int $product_id, int $stock_quantity);
$service->getStockQuantity(int $product_id);
$service->setStatus(int $product_id, ProductStatus $status);
$service->rename(int $product_id, string $name);
$service->get(int $product_id);
$service->update(int $product_id, array $data);

CustomerService


OrderService

$service->get(int $order_id);
$service->getWithCustomer(int $order_id);

API

优惠券

// List Coupons
$api->coupons()->get();

// Retrieve an Coupon
$api->coupon($coupon_id)->get();

// Create an Coupon
$api->coupons()->create([
    'code' => '10off',
    'discount_type' => CouponsDiscountType::percent,
    'amount' => 10,
    'individual_use' => true,
    'exclude_sale_items' => true,
    'minimum_amount' => 100.00
]);

// Update an Coupon
$api->coupon($coupon_id)->update([
    'amount' => 5
]);

// Delete an Coupon
$api->coupon($coupon_id)->delete();

// Batch
$api->couponBatch()->post([
    'create' => [
        [
            'code' => '20off',
            'discount_type' => CouponsDiscountType::percent,
            'amount' => 20,
            'individual_use' => true,
            'exclude_sale_items' => true,
            'minimum_amount' => 100.00
        ],
        [
            'code' => '30off',
            'discount_type' => CouponsDiscountType::percent,
            'amount' => 30,
            'individual_use' => true,
            'exclude_sale_items' => true,
            'minimum_amount' => 100.00
        ]
    ],
    'update' => [
        [
            'id' => 719,
            'minimum_amount' => 50.00
        ]
    ],
    'delete' => [
        720
    ]
]);

客户

// List Customers
$api->customers()->get();

// Retrieve a Customer
$api->customer($customer_id)->get();

// Create a Customer
$api->customers()->create([

]);

// Update a Customer
$api->customer($customer_id)->update([

]);

// Batch
$api->customerBatch()->post([

]);

// Retrieve Customer Downloads
$api->customerDownloads($customer_id)->get();

订单

// List Orders (paginated)
$api->orders()->get();

// List all Orders
$api->orders()->all(); // or cacheAll($seconds)

// Retrieve an Order
$api->order($order_id)->get();

// Create an Order
$api->orders()->create([
    'title' => 'The Title!',
    'status' => OrderStatus::on_hold
]);

// Update an Order
$api->order($id)->update([
    'status' => OrderStatus::completed
]);

// Delete an order
$api->order($id)->delete();

订单备注

// List Order Notes per Order
$api->orderNotes($order_id)->get();

// Retrieve an Order Note
$api->orderNote($order_id, $note_id)->get();

// Create an Order Note
$api->orderNotes($order_id)->create([
    'note' => 'Order ok!'
]);

// Delete an Order Note
$api->orderNote($order_id, $note_id)->delete();

订单退款

// List Order Refunds per Order
$api->orderRefunds($order_id)->get();

// Retrieve an Order Refund
$api->orderRefund($order_id, $refund_id)->get();

// Create an Order Refund
$api->orderRefunds($order_id)->create([
     'amount' => 30,
     'line_items' => [
       [
           'id' => 111,
           'refund_total' => 10,
           'refund_tax' => [
              [
                 'id' => 222,
                 'amount' => 20
              ]
           ]
       ]
     ]
]);

// Delete an Order Refund
$api->orderRefund($order_id, $refund_id)->delete();

产品

// List Product
$api->products()->get();

// Retrieve a Product
$api->product($product_id)->get();

// Create a Product
$api->product($product_id)->create([
    'name' => 'Premium Quality',
    'type' => 'simple',
    'regular_price' => 21.99,
    'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
    'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    'categories' => [
        [
            'id' => 9
        ],
        [
            'id' => 14
        ]
    ],
    'images' => [
        [
            'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
        ],
        [
            'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
        ]
    ]
]);

// Update a Product
$api->product($product_id)->update([
    'regular_price' => 24.54
]);

// Batch
$api->productBatch()->post([
    'create' => [
        [
            'name' => 'Woo Single #1',
            'type' => ProductType::simple,
            'regular_price' => 21.99,
            'virtual' => true,
            'downloadable' => true,
            'downloads' => [
                [
                    'name' => 'Woo Single',
                    'file' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg'
                ]
            ],
            'categories' => [
                [
                    'id' => 11
                ],
                [
                    'id' => 13
                ]
            ],
            'images' => [
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg'
                ]
            ]
        ],
        [
            'name' => 'New Premium Quality',
            'type' => 'simple',
            'regular_price' => 21.99,
            'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
            'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
            'categories' => [
                [
                    'id' => 9
                ],
                [
                    'id' => 14
                ]
            ],
            'images' => [
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
                ],
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
                ]
            ]
        ]
    ],
    'update' => [
        [
            'id' => 799,
            'default_attributes' => [
                [
                    'id' => 6,
                    'name' => 'Color',
                    'option' => 'Green'
                ],
                [
                    'id' => 0,
                    'name' => 'Size',
                    'option' => 'M'
                ]
            ]
        ]
    ],
    'delete' => [
        794
    ]
]);

产品属性

// List Product Attributes
$api->productAttributes()->get();

// Retrieve a Product Attributes
$api->productAttribute($attribute_id)->get();

// Create a Product Attributes
$api->productAttributes()->create([
    'name' => 'Color',
    'slug' => 'pa_color',
    'type' => AttributesType::select,
    'order_by' => OrderByAttributes::menu_order,
    'has_archives' => true
]);

// Update a Product Attributes
$api->productAttribute($attribute_id)->update([
    'order_by' => OrderByAttributes::name,
]);

// Batch
$api->productAttributeBatch()->post([
    'create' => [
        [
            'name' => 'Brand'
        ],
        [
            'name' => 'Publisher'
        ]
    ],
    'update' => [
        [
            'id' => 2,
            'order_by' => 'name'
        ]
    ],
    'delete' => [
        1
    ]
]);

产品属性术语

// List Product Attribute Terms
$api->productAttributeTerms($attribute_id)->get();

// Retrieve a Product Attribute Terms
$api->productAttributeTerm($attfibute_id, $term_id)->get();

// Create a Product Attribute Terms
$api->productAttributeTerms($attfibute_id)->create([
    'name' => 'XXS'
]);

// Update a Product Attribute Terms
$api->productAttributeTerm($attfibute_id, $term_id)->update([
    'name' => 'XXS'
]);

// Batch
$api->productAttributeTermBatch()->post([
    'create' => [
        [
            'name' => 'XXS'
        ],
        [
            'name' => 'S'
        ]
    ],
    'update' => [
        [
            'id' => 19,
            'menu_order' => 6
        ]
    ],
    'delete' => [
        21,
        20
    ]
]);

产品分类

// List Product Categories
$api->productCategories()->get();

// Retrieve a Product Categories
$api->productCategory($category_id)->get();

// Create a Categories
$api->productCategories()->create([
    'name' => 'Clothing',
    'image' => [
        'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
    ]
]);

// Update a Categories
$api->productCategory($category_id)->update([
    'description' => 'All kinds of clothes.'
]);

// Batch
$api->productCategoryBatch()->post([
    'create' => [
        [
            'name' => 'Albums'
        ],
        [
            'name' => 'Clothing'
        ]
    ],
    'update' => [
        [
            'id' => 10,
            'description' => 'Nice hoodies'
        ]
    ],
    'delete' => [
        11,
        12
    ]
]);

产品评论

// List Product Reviews
$api->productReviews($product_id)->get();

// Retrieve a Product Reviews
$api->productReview($product_id, $review_id)->get();

// Create a Product Reviews
$api->productReviews($product_id)->create([
    'product_id' => 22,
    'review' => 'Nice album!',
    'reviewer' => 'John Doe',
    'reviewer_email' => 'john.doe@example.com',
    'rating' => 5
]);

// Update a Product Reviews
$api->productReview($product_id, $review_id)->update([
    'rating': 5
]);

// Batch
$api->productReviewBatch($product_id)->post([
    'create' => [
        [
            'product_id' => 22,
            'review' => 'Looks fine',
            'reviewer' => 'John Doe',
            'reviewer_email' => 'john.doe@example.com',
            'rating' => 4
        ],
        [
            'product_id' => 22,
            'review' => 'I love this album',
            'reviewer' => 'John Doe',
            'reviewer_email' => 'john.doe@example.com',
            'rating' => 5
        ]
    ],
    'update' => [
        [
            'id' => 7,
            'reviewer' => 'John Doe',
            'reviewer_email' => 'john.doe@example.com',
        ]
    ],
    'delete' => [
        22
    ]
]);

产品标签

// List Product Tags
$api->productTags()->get();

// Retrieve a Product Tags
$api->productTag($tag_id)->get();

// Create a Product Tags
$api->productTags()->create([
    'name' => 'Leather Shoes'
]);

// Update a Product Tags
$api->productTag($tag_id)->update([
    'description': 'Genuine leather.'
]);

// Batch
$api->productTagBatch()->post([
    'create' => [
        [
            'name' => 'Round toe'
        ],
        [
            'name' => 'Flat'
        ]
    ],
    'update' => [
        [
            'id' => 34,
            'description' => 'Genuine leather.'
        ]
    ],
    'delete' => [
        35
    ]
]);

产品变体

// List Product Variations
$api->productVariations($product_id)->get();

// Retrieve a Product Variations
$api->productVariation($product_id, $variation_id)->get();

// Create a Product Variations
$api->productVariation($product_id)->create([
    'regular_price' => 9.00,
    'image' => [
        'id' => 423
    ],
    'attributes' => [
        [
            'id' => 9,
            'option' => 'Black'
        ]
    ]
]);

// Update a Product Variations
$api->productVariation($product_id, $variation_id)->update([
    'regular_price' => 10.00
]);

// Batch
$api->productVariationsBatch($product_id)->post([
    'create' => [
        [
            'regular_price' => 10.00,
            'attributes' => [
                [
                    'id' => 6,
                    'option' => 'Blue'
                ]
            ]
        ],
        [
            'regular_price' => 10.00,
            'attributes' => [
                [
                    'id' => 6,
                    'option' => 'White'
                ]
            ]
        ]
    ],
    'update' => [
        [
            'id' => 733,
            'regular_price' => 10.00
        ]
    ],
    'delete' => [
        732
    ]
]);

配送

配送方式

// List Shipping Methods
$api->shippingMethods()->get();

// Retrieve a Shipping Methods
$api->shippingMethod($method_id)->get();

// Create a Shipping Methods
$api->shippingMethods()->create([

]);

// Update a Shipping Methods
$api->shippingMethod($method_id)->update([

]);

配送区域

// List Shipping Zones
$api->shippingZones()->get();

// Retrieve a Shipping Zones
$api->shippingZone($zone_id)->get();

// Create a Shipping Zones
$api->shippingZones()->create([

]);

// Update a Shipping Zones
$api->shippingZone($zone_id)->update([

]);

配送区域位置

// TODO

配送区域方法

// List Shipping Zones Method
$api->shippingZoneMethods($zone_id)->get();

// Retrieve a Shipping Zones Method
$api->shippingZoneMethod($zone_id, $instance_id)->get();

// Create a Shipping Zones Method
$api->shippingZoneMethods($zone_id)->create([

]);

// Update a Shipping Zones Method
$api->shippingZoneMethod($zone_id, $instance_id)->update([

]);

报告

// List Reports
$api->reports()->get();

销售报告

// List Report Sales
$api->xyz()->get();

// Retrieve a Report Sales
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

顶级卖家报告

// List Report Top Seller
$api->xyz()->get();

// Retrieve a Report Top Seller
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

设置

// List ettings
$api->xyz()->get();

// Retrieve a ettings
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

税收

// List axes
$api->xyz()->get();

// Retrieve a axes
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

税收类别

// List Tax Classes
$api->xyz()->get();

// Retrieve a Tax Classes
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

Webhooks

// List ebhooks
$api->xyz()->get();

// Retrieve a ebhooks
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

支付网关

// List ayment Gateways
$api->xyz()->get();

// Retrieve a ayment Gateways
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

系统

// List ystem
$api->xyz()->get();

// Retrieve a ystem
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

系统状态

// List System Status
$api->xyz()->get();

// Retrieve a System Status
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);

系统状态工具

// List System Status Tools
$api->xyz()->get();

// Retrieve a System Status Tools
$api->xyz()->get();

// Create a xyz
$api->xyz()->create([

]);

// Update a xyz
$api->xyz()->update([

]);

// Batch
$api->xyz()->post([

]);