neimarperez/laravel-woocommerce

WooCommerce Rest API 与 Laravel

v2.0.0 2021-09-23 02:31 UTC

This package is auto-updated.

Last update: 2024-09-10 07:46:14 UTC


README

为 Laravel 提供的 WooCommerce Rest API。您可以轻松地使用此包获取、创建、更新和删除 WooCommerce 产品。

#安装

composer require codexshaper/laravel-woocommerce

#发布配置文件

php artisan vendor:publish --tag=woocommerce

在您的 .env 文件中添加 API 凭证

WOOCOMMERCE_STORE_URL=YOUR_WEBSITE_URL
WOOCOMMERCE_CONSUMER_KEY=API_CONSUMER_KEY
WOOCOMMERCE_CONSUMER_SECRET=API_CONSUMER_SECRET

需要帮助创建自己的 API 凭证吗?请参阅官方文档 https://docs.woocommerce.com/document/woocommerce-rest-api/

产品示例

#检索产品(们)

use Codexshaper\WooCommerce\Facades\Product;

public function products()
{
  return Product::all();
}

public function product( Request $request )
{
  $product = Product::find($request->id);
}

#创建新产品

// For Simple Product
$data = [
    'name'              => 'Simple Product', // Product Name or Title
    'type'              => 'simple', // Product type simple|variable
    'regular_price'     => '100', // Regular Price
    'sale_price'        => '', // Price after offer
    'description'       => 'Product Description', // Product Long Description
    'short_description' => 'Product Short Description', // Product Short Description
    // Set Categories as an array
    'categories'        => [
        [
            'id' => 1,
        ],
        [
            'id' => 3,
        ],
    ],
    // Set thumnail images as an array
    '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',
        ],
    ],
];

// For Variable Product
$data = [
            'name'               => 'Variable Product', // Product Name pr Title
            'type'               => 'variable', // Product Type simple|variable
            'description'        => 'Product Description', // Product Long Description
            'short_description'  => 'Product Summery', // Product Short Description
            // Product Categories
            'categories'         => [
                [
                    'id' => 9,
                ],
                [
                    'id' => 14,
                ],
            ],
            // Product images
            'images'             => [
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg',
                ],
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg',
                ],
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg',
                ],
                [
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg',
                ],
            ],
            // Product Attributes
            'attributes'         => [
                [
                    'id'        => 6,
                    'position'  => 0,
                    'visible'   => false,
                    'variation' => true,
                    'options'   => [
                        'Black',
                        'Green',
                    ],
                ],
                [
                    'name'      => 'Size',
                    'position'  => 0,
                    'visible'   => true,
                    'variation' => true,
                    'options'   => [
                        'S',
                        'M',
                    ],
                ],
            ],
            // Set Default attributes
            'default_attributes' => [
                [
                    'id'     => 6,
                    'option' => 'Black',
                ],
                [
                    'name'   => 'Size',
                    'option' => 'S',
                ],
            ],
        ];

// Create a product using create() method
$product = Product::create($data);

// Create a product using save() method
$categories = [
    [
        'id' => 1,
    ],
    [
        'id' => 3,
    ],
];

$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',
    ],
];

$product                    = new Product;
$product->name              = 'Product Eloquent 2';
$product->type              = 'simple';
$product->regular_price     = '100';
$product->sale_price        = '50';
$product->description       = 'Product Description';
$product->short_description = 'Product Short Description';
$product->categories        = $categories;
$product->images            = $images;
$product->save();

#更新现有产品

$product_id = 40;
$data       = [
    'regular_price' => '50',
    'sale_price'    => '25', // 50% off
];

$product = Product::update($product_id, $data);

#删除产品

$product_id = 40;

$options = ['force' => true]; // Set force option true for delete permanently. Default value false

$product = Product::delete($product_id, $options);

订单示例

#创建订单

$data = [
    'payment_method'       => 'bacs',
    'payment_method_title' => 'Direct Bank Transfer',
    'set_paid'             => true,
    'billing'              => [
        'first_name' => 'John',
        'last_name'  => 'Doe',
        'address_1'  => '969 Market',
        'address_2'  => '',
        'city'       => 'San Francisco',
        'state'      => 'CA',
        'postcode'   => '94103',
        'country'    => 'US',
        'email'      => 'john.doe@example.com',
        'phone'      => '(555) 555-5555',
    ],
    'shipping'             => [
        'first_name' => 'John',
        'last_name'  => 'Doe',
        'address_1'  => '969 Market',
        'address_2'  => '',
        'city'       => 'San Francisco',
        'state'      => 'CA',
        'postcode'   => '94103',
        'country'    => 'US',
    ],
    'line_items'           => [
        [
            'product_id' => 40,
            'quantity'   => 2,
        ],
        [
            'product_id'   => 127,
            'variation_id' => 23,
            'quantity'     => 1,
        ],
    ],
];

$order = Order::create($data);

#检索订单(们)

use Codexshaper\WooCommerce\Facades\Order;

public function orders()
{
  $orders = Order::all();
}

public function order( Request $request )
{
  $order = Order::find($request->id);
}

#更新订单

$order_id = 173;
$data     = [
    'status' => 'completed',
];

$order = Order::update($order_id, $data);

#删除订单

$order_id = 173;
$options = ['force' => true]; // Set force option true for delete permanently. Default value false

$order = Order::delete($order_id, $options);

为特定订单创建备注

$order_id = 172;
$data = [
    'note' => 'Add your note',
];
$createNote = Order::createNote($order_id, $data);

获取特定订单的备注

$order_id = 172;
$note_id  = 67;

$note = Order::note($order_id, $note_id);

获取特定订单的所有备注

$order_id = 172;
$options = [];
$notes = Order::notes($order_id, $options);

删除特定订单的备注

$order_id = 172;
$note_id  = 67;
$options = [];

$note = Order::deleteNote($order_id, $note_id, $options);

为特定订单创建退款

$order_id = 172;
$data = [
    'amount' => '10'
];
$createNote = Order::createRefund($order_id, $data);

获取特定订单的退款

$order_id = 172;
$refund_id  = 117;

$refund = Order::refund($order_id, $refund_id);

获取特定订单的所有退款

$order_id = 172;
$options = [];
$refunds = Order::refunds($order_id, $options);

删除特定订单的退款

$order_id = 172;
$refund_id  = 67;
$options = [];

$refund = Order::deleteRefund($order_id, $refund_id, $options);

客户示例

#创建客户

$data = [
    'email'      => 'john.doe@example.com',
    'first_name' => 'John',
    'last_name'  => 'Doe',
    'username'   => 'john.doe',
    'billing'    => [
        'first_name' => 'John',
        'last_name'  => 'Doe',
        'company'    => '',
        'address_1'  => '969 Market',
        'address_2'  => '',
        'city'       => 'San Francisco',
        'state'      => 'CA',
        'postcode'   => '94103',
        'country'    => 'US',
        'email'      => 'john.doe@example.com',
        'phone'      => '(555) 555-5555',
    ],
    'shipping'   => [
        'first_name' => 'John',
        'last_name'  => 'Doe',
        'company'    => '',
        'address_1'  => '969 Market',
        'address_2'  => '',
        'city'       => 'San Francisco',
        'state'      => 'CA',
        'postcode'   => '94103',
        'country'    => 'US',
    ],
];
$customer = Customer::create($data);

#检索客户(们)

use Codexshaper\WooCommerce\Facades\Customer;

public function customers()
{
  return Customer::all();
}

public function customer( Request $request )
{
  $customer = Customer::find($request->id);
}

#更新客户

$customer_id = 2;
$data        = [
    'first_name' => 'James',
    'billing'    => [
        'first_name' => 'James',
    ],
    'shipping'   => [
        'first_name' => 'James',
    ],
];

$customer = Customer::update($customer_id, $data);

#删除客户

$customer_id = 2;
$options     = ['force' => true]; // Set force option true for delete permanently. Default value false

$customer = Customer::delete($customer_id, $options);

产品、客户和订单的 Eloquent 风格

// Where passing multiple parameters
$orders = Order::where('status', 'publishing')->get();
$orders = Order::where('total', '>=', 10)->get();

// Where passing an array
$orders = Order::where(['status' => 'processing']);
$orders = Order::where(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get();

// Order with where
$orders = Order::where('total', '>=', 10)->orderBy('id', 'asc')->get();

// Set Options
$orders = Order::options(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get();

// You can set options by passing an array when call `all` method
$orders = Order::all(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc']);

#产品选项: https://woocommerce.github.io/woocommerce-rest-api-docs/#products

#客户选项: https://woocommerce.github.io/woocommerce-rest-api-docs/#customers

#订单选项: https://woocommerce.github.io/woocommerce-rest-api-docs/#orders

您也可以使用 WooCommerce Facade

use Codexshaper\WooCommerce\Facades\WooCommerce;

public function products()
{
  return WooCommerce::all('products');
}

public function product( Request $request )
{
  $product = WooCommerce::find('products/'.$request->id);
}

public function orders()
{
  return WooCommerce::all('orders');
}

public function order( Request $request )
{
  $order = WooCommerce::all('orders/'.$request->id);
}

public function customers()
{
  return WooCommerce::all('customers');
}

public function customer( Request $request )
{
  $customer = WooCommerce::all('customers/'.$request->id);
}

使用 Facade 别名

use WooCommerce // Same as use Codexshaper\WooCommerce\Facades\WooCommerce;
use Customer // Same as use Codexshaper\WooCommerce\Models\Customer;
use Order // Same as use Codexshaper\WooCommerce\Models\Order;
use Product // Same as Codexshaper\WooCommerce\Models\Product;