├── .gitattributes ├── .gitignore ├── .scrutinizer.yml ├── .travis.yml ├── LICENSE.md ├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml ├── src ├── Facades │ ├── Attribute.php │ ├── Category.php │ ├── Coupon.php │ ├── Customer.php │ ├── Note.php │ ├── Order.php │ ├── PaymentGateway.php │ ├── Product.php │ ├── Query.php │ ├── Refund.php │ ├── Report.php │ ├── Review.php │ ├── Setting.php │ ├── ShippingMethod.php │ ├── ShippingZone.php │ ├── ShippingZoneMethod.php │ ├── Subscription.php │ ├── System.php │ ├── Tag.php │ ├── Tax.php │ ├── TaxClass.php │ ├── Term.php │ ├── Variation.php │ ├── Webhook.php │ ├── WooAnalytics.php │ ├── WooCommerce.php │ └── WoocommerceFacade.php ├── Models │ ├── Attribute.php │ ├── BaseModel.php │ ├── Category.php │ ├── Coupon.php │ ├── Customer.php │ ├── Note.php │ ├── Order.php │ ├── PaymentGateway.php │ ├── Product.php │ ├── Refund.php │ ├── Report.php │ ├── Review.php │ ├── Setting.php │ ├── ShippingMethod.php │ ├── ShippingZone.php │ ├── Subscription.php │ ├── System.php │ ├── Tag.php │ ├── Tax.php │ ├── TaxClass.php │ ├── Term.php │ ├── Variation.php │ └── Webhook.php ├── Query.php ├── Traits │ ├── QueryBuilderTrait.php │ └── WooCommerceTrait.php ├── WooCommerceAnalyticsApi.php ├── WooCommerceApi.php ├── WooCommerceServiceProvider.php └── config │ └── woocommerce.php └── tests └── Product.php /.gitattributes: -------------------------------------------------------------------------------- 1 | *.php linguist-language=PHP 2 | *.js linguist-language=PHP 3 | *.vue linguist-language=PHP -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /public/hot 3 | /public/storage 4 | /storage/*.key 5 | /vendor 6 | .env 7 | .phpunit.result.cache 8 | Homestead.json 9 | Homestead.yaml 10 | npm-debug.log 11 | yarn-error.log 12 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | checks: 4 | php: 5 | code_rating: true 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | tools: 20 | external_code_coverage: 21 | timeout: 600 22 | runs: 2 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - '7.2' 5 | - '7.3' 6 | 7 | before_script: 8 | - travis_retry composer self-update 9 | - travis_retry composer update --prefer-lowest --prefer-source --no-interaction 10 | 11 | script: 12 | - phpunit --coverage-text --coverage-clover=coverage.clover 13 | 14 | after_script: 15 | - wget https://scrutinizer-ci.com/ocular.phar 16 | - php ocular.phar code-coverage:upload --format=php-clover coverage.clover 17 | 18 | notifications: 19 | on_success: never 20 | on_failure: always -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Md. Abu Ahsan Basir 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org) 2 | [![Build Status](https://travis-ci.org/Codexshaper/laravel-woocommerce.svg?branch=master)](https://travis-ci.org/Codexshaper/laravel-woocommerce) 3 | [![StyleCI](https://github.styleci.io/repos/180436811/shield?branch=master)](https://github.styleci.io/repos/180436811) 4 | [![Quality Score](https://img.shields.io/scrutinizer/g/Codexshaper/laravel-woocommerce.svg?style=flat-square)](https://scrutinizer-ci.com/g/Codexshaper/laravel-woocommerce) 5 | [![Downloads](https://poser.pugx.org/Codexshaper/laravel-woocommerce/d/total.svg)](https://packagist.org/packages/Codexshaper/laravel-woocommerce) 6 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/Codexshaper/laravel-woocommerce.svg?style=flat-square)](https://packagist.org/packages/Codexshaper/laravel-woocommerce) 7 | 8 | # Description 9 | WooCommerce Rest API for Laravel. You can Get, Create, Update and Delete your woocommerce product using this package easily. 10 | 11 | [Documentation](https://codexshaper.github.io/docs/laravel-woocommerce/) 12 | 13 | ## Authors 14 | 15 | * **Md Abu Ahsan Basir** - [github](https://github.com/maab16) 16 | 17 | ## License 18 | 19 | - **[MIT license](http://opensource.org/licenses/mit-license.php)** 20 | - Copyright 2020 © CodexShaper. 21 | 22 | # Eloquent Style for Product, Customer and Order 23 | 24 | ``` 25 | // Where passing multiple parameters 26 | $products = Product::where('title','hello')->get(); 27 | OR 28 | // You can call field with where clause 29 | $products = Product::whereTitle('hello')->get(); 30 | // Fields name are more than one words or seperate by underscore (_). For example field name is `min_price` 31 | $products = Product::whereMinPrice(5)->get(); 32 | 33 | // Where passing an array 34 | $orders = Order::where(['status' => 'processing']); 35 | $orders = Order::where(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get(); 36 | 37 | // Set Options 38 | $orders = Order::options(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc'])->get(); 39 | 40 | // You can set options by passing an array when call `all` method 41 | $orders = Order::all(['status' => 'processing', 'orderby' => 'id', 'order' => 'asc']); 42 | ``` 43 | #Product Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#products 44 | 45 | #Customer Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#customers 46 | 47 | #Order Options: https://woocommerce.github.io/woocommerce-rest-api-docs/#orders 48 | 49 | # You can also use ```WooCommerce``` Facade 50 | 51 | ``` 52 | use Codexshaper\WooCommerce\Facades\WooCommerce; 53 | 54 | public function products() 55 | { 56 | return WooCommerce::all('products'); 57 | } 58 | 59 | public function product( Request $request ) 60 | { 61 | $product = WooCommerce::find('products/'.$request->id); 62 | } 63 | 64 | public function orders() 65 | { 66 | return WooCommerce::all('orders'); 67 | } 68 | 69 | public function order( Request $request ) 70 | { 71 | $order = WooCommerce::all('orders/'.$request->id); 72 | } 73 | 74 | public function customers() 75 | { 76 | return WooCommerce::all('customers'); 77 | } 78 | 79 | public function customer( Request $request ) 80 | { 81 | $customer = WooCommerce::all('customers/'.$request->id); 82 | } 83 | ``` 84 | 85 | # Use Facade Alias 86 | 87 | ``` 88 | use WooCommerce // Same as use Codexshaper\WooCommerce\Facades\WooCommerce; 89 | use Customer // Same as use Codexshaper\WooCommerce\Models\Customer; 90 | use Order // Same as use Codexshaper\WooCommerce\Models\Order; 91 | use Product // Same as Codexshaper\WooCommerce\Models\Product; 92 | ``` 93 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codexshaper/laravel-woocommerce", 3 | "description": "WooCommerce Rest API for Laravel", 4 | "keywords": [ 5 | "woocommerce", 6 | "laravel", 7 | "laravel-woocommerce", 8 | "woocommerce-api", 9 | "woocommerce-rest-api", 10 | "laravel-woocommerce-api", 11 | "laravel-woocommerce-rest-api" 12 | ], 13 | "type": "library", 14 | "require": { 15 | "automattic/woocommerce": "^3.0" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "^7.0|^8.0|^9.3", 19 | "illuminate/support": "~5.5.40|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0" 20 | }, 21 | "license": "MIT", 22 | "authors": [ 23 | { 24 | "name": "Md Abu Ahsan Basir", 25 | "email": "maab.career@gmail.com" 26 | } 27 | ], 28 | "minimum-stability": "dev", 29 | "autoload": { 30 | "psr-4": { 31 | "Codexshaper\\WooCommerce\\": "src/" 32 | } 33 | }, 34 | "extra": { 35 | "laravel": { 36 | "providers": [ 37 | "Codexshaper\\WooCommerce\\WooCommerceServiceProvider" 38 | ], 39 | "aliases": { 40 | "Attribute": "Codexshaper\\WooCommerce\\Models\\Attribute", 41 | "Category": "Codexshaper\\WooCommerce\\Models\\Category", 42 | "Coupon": "Codexshaper\\WooCommerce\\Models\\Coupon", 43 | "Customer": "Codexshaper\\WooCommerce\\Models\\Customer", 44 | "Note": "Codexshaper\\WooCommerce\\Models\\Note", 45 | "Order": "Codexshaper\\WooCommerce\\Models\\Order", 46 | "PaymentGateway": "Codexshaper\\WooCommerce\\Facades\\PaymentGateway", 47 | "Product": "Codexshaper\\WooCommerce\\Models\\Product", 48 | "Refund": "Codexshaper\\WooCommerce\\Models\\Refund", 49 | "Report": "Codexshaper\\WooCommerce\\Models\\Report", 50 | "Review": "Codexshaper\\WooCommerce\\Models\\Review", 51 | "Setting": "Codexshaper\\WooCommerce\\Models\\Setting", 52 | "ShippingMethod": "Codexshaper\\WooCommerce\\Models\\ShippingMethod", 53 | "ShippingZone": "Codexshaper\\WooCommerce\\Models\\ShippingZone", 54 | "ShippingZoneMethod": "Codexshaper\\WooCommerce\\Models\\ShippingZoneMethod", 55 | "System": "Codexshaper\\WooCommerce\\Models\\System", 56 | "Tag": "Codexshaper\\WooCommerce\\Models\\Tag", 57 | "Tax": "Codexshaper\\WooCommerce\\Models\\Tax", 58 | "TaxClass": "Codexshaper\\WooCommerce\\Models\\TaxClass", 59 | "Term": "Codexshaper\\WooCommerce\\Models\\Term", 60 | "Variation": "Codexshaper\\WooCommerce\\Models\\Variation", 61 | "Webhook": "Codexshaper\\WooCommerce\\Facades\\Webhook", 62 | "WooCommerce": "Codexshaper\\WooCommerce\\Facades\\WooCommerce", 63 | "WooAnalytics": "Codexshaper\\WooCommerce\\Facades\\WooAnalytics", 64 | "Query": "Codexshaper\\WooCommerce\\Facades\\Query" 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "5673c61b72915f0a71a70d93634cb73d", 8 | "packages": [ 9 | { 10 | "name": "automattic/woocommerce", 11 | "version": "3.0.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/woocommerce/wc-api-php.git", 15 | "reference": "a71aa95cc3de3f1d68c6303525d03c0557a96137" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/woocommerce/wc-api-php/zipball/a71aa95cc3de3f1d68c6303525d03c0557a96137", 20 | "reference": "a71aa95cc3de3f1d68c6303525d03c0557a96137", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "ext-curl": "*", 25 | "ext-json": "*", 26 | "php": ">= 5.4.0" 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit": "*", 30 | "squizlabs/php_codesniffer": "3.*" 31 | }, 32 | "type": "library", 33 | "autoload": { 34 | "psr-4": { 35 | "Automattic\\WooCommerce\\": [ 36 | "src/WooCommerce" 37 | ] 38 | } 39 | }, 40 | "notification-url": "https://packagist.org/downloads/", 41 | "license": [ 42 | "MIT" 43 | ], 44 | "authors": [ 45 | { 46 | "name": "Claudio Sanches", 47 | "email": "claudio.sanches@automattic.com" 48 | } 49 | ], 50 | "description": "A PHP wrapper for the WooCommerce REST API", 51 | "keywords": [ 52 | "api", 53 | "woocommerce" 54 | ], 55 | "time": "2019-01-16T20:28:40+00:00" 56 | } 57 | ], 58 | "packages-dev": [], 59 | "aliases": [], 60 | "minimum-stability": "stable", 61 | "stability-flags": [], 62 | "prefer-stable": false, 63 | "prefer-lowest": false, 64 | "platform": [], 65 | "platform-dev": [] 66 | } 67 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/Facades/Attribute.php: -------------------------------------------------------------------------------- 1 | setEndpoint("products/attributes/{$attribute_id}/terms") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retrieve single Item. 31 | * 32 | * @param int $attribute_id 33 | * @param int $term_id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function getTerm($attribute_id, $term_id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 42 | ->find($term_id, $options); 43 | } 44 | 45 | /** 46 | * Create new Item. 47 | * 48 | * @param int $attribute_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function addTerm($attribute_id, $data) 54 | { 55 | return Query::init() 56 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Update Existing Item. 62 | * 63 | * @param int $attribute_id 64 | * @param int $term_id 65 | * @param array $data 66 | * 67 | * @return object 68 | */ 69 | protected function updateTerm($attribute_id, $term_id, $data) 70 | { 71 | return Query::init() 72 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 73 | ->update($term_id, $data); 74 | } 75 | 76 | /** 77 | * Destroy Item. 78 | * 79 | * @param int $attribute_id 80 | * @param int $term_id 81 | * @param array $options 82 | * 83 | * @return object 84 | */ 85 | protected function deleteTerm($attribute_id, $term_id, $options = []) 86 | { 87 | return Query::init() 88 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 89 | ->delete($term_id, $options); 90 | } 91 | 92 | /** 93 | * Batch Update. 94 | * 95 | * @param int $attribute_id 96 | * @param array $data 97 | * 98 | * @return object 99 | */ 100 | protected function batchTerm($attribute_id, $data) 101 | { 102 | return Query::init() 103 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 104 | ->batch($data); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/Models/BaseModel.php: -------------------------------------------------------------------------------- 1 | $name; 19 | } 20 | 21 | /** 22 | * Set Option. 23 | * 24 | * @param string $name 25 | * @param string $value 26 | * 27 | * @return void 28 | */ 29 | public function __set($name, $value) 30 | { 31 | $this->properties[$name] = $value; 32 | } 33 | 34 | public function __call($method, $parameters) 35 | { 36 | if (!method_exists($this, $method)) { 37 | preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $partials); 38 | $method = array_shift($partials[0]); 39 | if (!method_exists($this, $method)) { 40 | throw new \Exception('Sorry! you are calling wrong method'); 41 | } 42 | array_unshift($parameters, strtolower(implode('_', $partials[0]))); 43 | } 44 | 45 | return $this->$method(...$parameters); 46 | } 47 | 48 | public static function __callStatic($method, $parameters) 49 | { 50 | return (new static())->$method(...$parameters); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Models/Category.php: -------------------------------------------------------------------------------- 1 | setEndpoint("customers/{$id}/downloads") 25 | ->all($options); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Models/Note.php: -------------------------------------------------------------------------------- 1 | setEndpoint("orders/{$order_id}/notes") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retrieve single Item. 31 | * 32 | * @param int $order_id 33 | * @param int $note_id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function find($order_id, $note_id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("orders/{$order_id}/notes") 42 | ->find($note_id, $options); 43 | } 44 | 45 | /** 46 | * Create new Item. 47 | * 48 | * @param int $order_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function create($order_id, $data) 54 | { 55 | return Query::init() 56 | ->setEndpoint("orders/{$order_id}/notes") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Destroy Item. 62 | * 63 | * @param int $order_id 64 | * @param int $note_id 65 | * @param array $options 66 | * 67 | * @return object 68 | */ 69 | protected function delete($order_id, $note_id, $options = []) 70 | { 71 | return Query::init() 72 | ->setEndpoint("orders/{$order_id}/notes") 73 | ->delete($note_id, $options); 74 | } 75 | 76 | /** 77 | * Paginate results. 78 | * 79 | * 80 | * @param int $order_id 81 | * @param int $per_page 82 | * @param int $current_page 83 | * @param array $options 84 | * 85 | * @return array 86 | */ 87 | protected function paginate( 88 | $order_id, 89 | $per_page = 10, 90 | $current_page = 1, 91 | $options = [] 92 | ) { 93 | return Query::init() 94 | ->setEndpoint("orders/{$order_id}/notes") 95 | ->paginate($per_page, $current_page, $options); 96 | } 97 | 98 | /** 99 | * Count all results. 100 | * 101 | * @param int $order_id 102 | * 103 | * @return int 104 | */ 105 | protected function count($order_id) 106 | { 107 | return Query::init() 108 | ->setEndpoint("orders/{$order_id}/notes") 109 | ->count(); 110 | } 111 | 112 | /** 113 | * Store data. 114 | * 115 | * @param int $order_id 116 | * 117 | * @return array 118 | */ 119 | public function save($order_id) 120 | { 121 | return Query::init() 122 | ->setEndpoint("orders/{$order_id}/notes") 123 | ->save(); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Models/Order.php: -------------------------------------------------------------------------------- 1 | setEndpoint("orders/{$order_id}/notes") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retreive a note. 31 | * 32 | * @param int $order_id 33 | * @param int $note_id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function note($order_id, $note_id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("orders/{$order_id}/notes") 42 | ->find($note_id, $options); 43 | } 44 | 45 | /** 46 | * Create a note. 47 | * 48 | * @param int $order_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function createNote($order_id, $data = []) 54 | { 55 | return Query::init() 56 | ->setEndpoint("orders/{$order_id}/notes") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Delete a note. 62 | * 63 | * @param int $order_id 64 | * @param int $note_id 65 | * @param array $options 66 | * 67 | * @return object 68 | */ 69 | protected function deleteNote($order_id, $note_id, $options = []) 70 | { 71 | return Query::init() 72 | ->setEndpoint("orders/{$order_id}/notes") 73 | ->delete($note_id, $options); 74 | } 75 | 76 | /** 77 | * Retrieve all refunds. 78 | * 79 | * @param int $order_id 80 | * @param array $options 81 | * 82 | * @return array 83 | */ 84 | protected function refunds($order_id, $options = []) 85 | { 86 | return Query::init() 87 | ->setEndpoint("orders/{$order_id}/refunds") 88 | ->all($options); 89 | } 90 | 91 | /** 92 | * Retrieve a refund. 93 | * 94 | * @param int $order_id 95 | * @param int $refund_id 96 | * @param array $options 97 | * 98 | * @return object 99 | */ 100 | protected function refund($order_id, $refund_id, $options = []) 101 | { 102 | return Query::init() 103 | ->setEndpoint("orders/{$order_id}/refunds") 104 | ->find($refund_id, $options); 105 | } 106 | 107 | /** 108 | * Create refund. 109 | * 110 | * @param int $order_id 111 | * @param array $data 112 | * 113 | * @return object 114 | */ 115 | protected function createRefund($order_id, $data = []) 116 | { 117 | return Query::init() 118 | ->setEndpoint("orders/{$order_id}/refunds") 119 | ->create($data); 120 | } 121 | 122 | /** 123 | * Delete refund. 124 | * 125 | * @param int $order_id 126 | * @param int $refund_id 127 | * @param array $options 128 | * 129 | * @return object 130 | */ 131 | protected function deleteRefund($order_id, $refund_id, $options = []) 132 | { 133 | return Query::init() 134 | ->setEndpoint("orders/{$order_id}/refunds") 135 | ->delete($refund_id, $options); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/Models/PaymentGateway.php: -------------------------------------------------------------------------------- 1 | setEndpoint("orders/{$order_id}/refunds") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retrieve single Item. 31 | * 32 | * @param int $order_id 33 | * @param int $refund_id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function find($order_id, $refund_id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("orders/{$order_id}/refunds") 42 | ->find($refund_id, $options); 43 | } 44 | 45 | /** 46 | * Create new Item. 47 | * 48 | * @param int $order_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function create($order_id, $data) 54 | { 55 | return Query::init() 56 | ->setEndpoint("orders/{$order_id}/refunds") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Destroy Item. 62 | * 63 | * @param int $order_id 64 | * @param int $refund_id 65 | * @param array $options 66 | * 67 | * @return object 68 | */ 69 | protected function delete($order_id, $refund_id, $options = []) 70 | { 71 | return Query::init() 72 | ->setEndpoint("orders/{$order_id}/refunds") 73 | ->delete($refund_id, $options); 74 | } 75 | 76 | /** 77 | * Paginate results. 78 | * 79 | * @param int $order_id 80 | * @param int $per_page 81 | * @param int $current_page 82 | * @param array $options 83 | * 84 | * @return array 85 | */ 86 | protected function paginate( 87 | $order_id, 88 | $per_page = 10, 89 | $current_page = 1, 90 | $options = [] 91 | ) { 92 | return Query::init() 93 | ->setEndpoint("orders/{$order_id}/refunds") 94 | ->paginate($per_page, $current_page, $options); 95 | } 96 | 97 | /** 98 | * Count all results. 99 | * 100 | * @param int $order_id 101 | * 102 | * @return int 103 | */ 104 | protected function count($order_id) 105 | { 106 | return Query::init() 107 | ->setEndpoint("orders/{$order_id}/refunds") 108 | ->count(); 109 | } 110 | 111 | /** 112 | * Store data. 113 | * 114 | * @param int $order_id 115 | * 116 | * @return array 117 | */ 118 | public function save($order_id) 119 | { 120 | return Query::init() 121 | ->setEndpoint("orders/{$order_id}/refunds") 122 | ->save(); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Models/Report.php: -------------------------------------------------------------------------------- 1 | setEndpoint('reports/sales') 25 | ->all($options); 26 | } 27 | 28 | /** 29 | * Retrieve all top sellers. 30 | * 31 | * @param array $options 32 | * 33 | * @return array 34 | */ 35 | protected function topSellers($options = []) 36 | { 37 | return Query::init() 38 | ->setEndpoint('reports/top_sellers') 39 | ->all($options); 40 | } 41 | 42 | /** 43 | * Retrieve all coupons. 44 | * 45 | * @param array $options 46 | * 47 | * @return array 48 | */ 49 | protected function coupons($options = []) 50 | { 51 | return Query::init() 52 | ->setEndpoint('reports/coupons/totals') 53 | ->all($options); 54 | } 55 | 56 | /** 57 | * Retrieve all customers. 58 | * 59 | * @param array $options 60 | * 61 | * @return array 62 | */ 63 | protected function customers($options = []) 64 | { 65 | return Query::init() 66 | ->setEndpoint('reports/customers/totals') 67 | ->all($options); 68 | } 69 | 70 | /** 71 | * Retrieve all orders. 72 | * 73 | * @param array $options 74 | * 75 | * @return array 76 | */ 77 | protected function orders($options = []) 78 | { 79 | return Query::init() 80 | ->setEndpoint('reports/orders/totals') 81 | ->all($options); 82 | } 83 | 84 | /** 85 | * Retrieve all products. 86 | * 87 | * @param array $options 88 | * 89 | * @return array 90 | */ 91 | protected function products($options = []) 92 | { 93 | return Query::init() 94 | ->setEndpoint('reports/products/totals') 95 | ->all($options); 96 | } 97 | 98 | /** 99 | * Retrieve all reviews. 100 | * 101 | * @param array $options 102 | * 103 | * @return array 104 | */ 105 | protected function reviews($options = []) 106 | { 107 | return Query::init() 108 | ->setEndpoint('reports/reviews/totals') 109 | ->all($options); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Models/Review.php: -------------------------------------------------------------------------------- 1 | setEndpoint("settings/{$group_id}") 27 | ->find($id, $options); 28 | } 29 | 30 | /** 31 | * Retrieve options. 32 | * 33 | * @param int $id 34 | * @param array $options 35 | * 36 | * @return array 37 | */ 38 | protected function options($id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint('settings') 42 | ->find($id, $options); 43 | } 44 | 45 | /** 46 | * Update Existing Item. 47 | * 48 | * @param int $group_id 49 | * @param int $id 50 | * @param array $data 51 | * 52 | * @return object 53 | */ 54 | protected function update($group_id, $id, $data) 55 | { 56 | return Query::init() 57 | ->setEndpoint("settings/{$group_id}") 58 | ->update($id, $data); 59 | } 60 | 61 | /** 62 | * Batch Update. 63 | * 64 | * @param array $data 65 | * 66 | * @return object 67 | */ 68 | protected function batch($id, $data) 69 | { 70 | return Query::init() 71 | ->setEndpoint("settings/{$id}") 72 | ->batch($data); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Models/ShippingMethod.php: -------------------------------------------------------------------------------- 1 | setEndpoint("shipping/zones/{$id}/locations") 27 | ->all($options); 28 | } 29 | 30 | /** 31 | * Update Existing Item. 32 | * 33 | * @param int $id 34 | * @param array $data 35 | * 36 | * @return object 37 | */ 38 | protected function updateLocations($id, $data = []) 39 | { 40 | return WooCommerce::update("shipping/zones/{$id}/locations", $data); 41 | } 42 | 43 | /** 44 | * Create new Item. 45 | * 46 | * @param int $id 47 | * @param array $data 48 | * 49 | * @return object 50 | */ 51 | protected function addShippingZoneMethod($id, $data) 52 | { 53 | return Query::init() 54 | ->setEndpoint("shipping/zones/{$id}/methods") 55 | ->create($data); 56 | } 57 | 58 | /** 59 | * Retrieve single Item. 60 | * 61 | * @param int $zone_id 62 | * @param int $id 63 | * @param array $options 64 | * 65 | * @return object 66 | */ 67 | protected function getShippingZoneMethod($zone_id, $id, $options = []) 68 | { 69 | return Query::init() 70 | ->setEndpoint("shipping/zones/{$zone_id}/methods") 71 | ->find($id, $options); 72 | } 73 | 74 | /** 75 | * Retrieve all Items. 76 | * 77 | * @param int $id 78 | * @param array $options 79 | * 80 | * @return array 81 | */ 82 | protected function getShippingZoneMethods($id, $options = []) 83 | { 84 | return Query::init() 85 | ->setEndpoint("shipping/zones/{$id}/methods") 86 | ->all($options); 87 | } 88 | 89 | /** 90 | * Update Existing Item. 91 | * 92 | * @param int $zone_id 93 | * @param int $id 94 | * @param array $data 95 | * 96 | * @return object 97 | */ 98 | protected function updateShippingZoneMethod($zone_id, $id, $data = []) 99 | { 100 | return Query::init() 101 | ->setEndpoint("shipping/zones/{$zone_id}/methods") 102 | ->update($id, $data); 103 | } 104 | 105 | /** 106 | * Destroy Item. 107 | * 108 | * @param int $zone_id 109 | * @param int $id 110 | * @param array $options 111 | * 112 | * @return object 113 | */ 114 | protected function deleteShippingZoneMethod($zone_id, $id, $options = []) 115 | { 116 | return Query::init() 117 | ->setEndpoint("shipping/zones/{$zone_id}/methods") 118 | ->delete($id, $options); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/Models/Subscription.php: -------------------------------------------------------------------------------- 1 | setEndpoint("subscriptions/{$subscription_id}/notes") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retreive a note. 31 | * 32 | * @param int $subscription_id 33 | * @param int $note_id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function note($subscription_id, $note_id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("subscriptions/{$subscription_id}/notes") 42 | ->find($note_id, $options); 43 | } 44 | 45 | /** 46 | * Create a note. 47 | * 48 | * @param int $subscription_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function createNote($subscription_id, $data = []) 54 | { 55 | return Query::init() 56 | ->setEndpoint("subscriptions/{$subscription_id}/notes") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Delete a note. 62 | * 63 | * @param int $subscription_id 64 | * @param int $note_id 65 | * @param array $options 66 | * 67 | * @return object 68 | */ 69 | protected function deleteNote($subscription_id, $note_id, $options = []) 70 | { 71 | return Query::init() 72 | ->setEndpoint("subscriptions/{$subscription_id}/notes") 73 | ->delete($note_id, $options); 74 | } 75 | 76 | /** 77 | * Retrieve all orders for the subscription. 78 | * 79 | * @param int $subscription_id 80 | * @param array $options 81 | * 82 | * @return array 83 | */ 84 | protected function orders($subscription_id, $options = []) 85 | { 86 | return Query::init() 87 | ->setEndpoint("subscriptions/{$subscription_id}/orders") 88 | ->all($options); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/Models/System.php: -------------------------------------------------------------------------------- 1 | setEndpoint('system_status') 25 | ->all($options); 26 | } 27 | 28 | /** 29 | * Retrieve single tool. 30 | * 31 | * @param int $id 32 | * @param array $options 33 | * 34 | * @return object 35 | */ 36 | protected function tool($id, $options = []) 37 | { 38 | return Query::init() 39 | ->setEndpoint('system_status/tools') 40 | ->find($id, $options); 41 | } 42 | 43 | /** 44 | * Retrieve all tools. 45 | * 46 | * @param array $options 47 | * 48 | * @return array 49 | */ 50 | protected function tools($options = []) 51 | { 52 | return Query::init() 53 | ->setEndpoint('system_status/tools') 54 | ->all($options); 55 | } 56 | 57 | /** 58 | * Run tool. 59 | * 60 | * @param int $id 61 | * @param array $data 62 | * 63 | * @return object 64 | */ 65 | protected function run($id, $data) 66 | { 67 | return Query::init() 68 | ->setEndpoint('system_status/tools') 69 | ->update($id, $data); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Models/Tag.php: -------------------------------------------------------------------------------- 1 | setEndpoint("products/attributes/{$attribute_id}/terms") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retrieve single Item. 31 | * 32 | * @param int $attribute_id 33 | * @param int $id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function find($attribute_id, $id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 42 | ->find($id, $options); 43 | } 44 | 45 | /** 46 | * Create new Item. 47 | * 48 | * @param int $attribute_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function create($attribute_id, $data) 54 | { 55 | return Query::init() 56 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Update Existing Item. 62 | * 63 | * @param int $attribute_id 64 | * @param int $id 65 | * @param array $data 66 | * 67 | * @return object 68 | */ 69 | protected function update($attribute_id, $id, $data) 70 | { 71 | return Query::init() 72 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 73 | ->update($id, $data); 74 | } 75 | 76 | /** 77 | * Destroy Item. 78 | * 79 | * @param int $attribute_id 80 | * @param int $id 81 | * @param array $options 82 | * 83 | * @return object 84 | */ 85 | protected function delete($attribute_id, $id, $options = []) 86 | { 87 | return Query::init() 88 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 89 | ->delete($id, $options); 90 | } 91 | 92 | /** 93 | * Batch Update. 94 | * 95 | * @param int $attribute_id 96 | * @param array $data 97 | * 98 | * @return object 99 | */ 100 | protected function batch($attribute_id, $data) 101 | { 102 | return Query::init() 103 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 104 | ->batch($data); 105 | } 106 | 107 | /** 108 | * Paginate results. 109 | * 110 | * @param int $attribute_id 111 | * @param int $per_page 112 | * @param int $current_page 113 | * @param array $options 114 | * 115 | * @return array 116 | */ 117 | protected function paginate( 118 | $attribute_id, 119 | $per_page = 10, 120 | $current_page = 1, 121 | $options = [] 122 | ) { 123 | return Query::init() 124 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 125 | ->paginate($per_page, $current_page, $options); 126 | } 127 | 128 | /** 129 | * Count all results. 130 | * 131 | * @param int $attribute_id 132 | * 133 | * @return int 134 | */ 135 | protected function count($attribute_id) 136 | { 137 | return Query::init() 138 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 139 | ->count(); 140 | } 141 | 142 | /** 143 | * Store data. 144 | * 145 | * @param int $attribute_id 146 | * 147 | * @return array 148 | */ 149 | public function save($attribute_id) 150 | { 151 | return Query::init() 152 | ->setEndpoint("products/attributes/{$attribute_id}/terms") 153 | ->save(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Models/Variation.php: -------------------------------------------------------------------------------- 1 | setEndpoint("products/{$product_id}/variations") 26 | ->all($options); 27 | } 28 | 29 | /** 30 | * Retrieve single Item. 31 | * 32 | * @param int $product_id 33 | * @param int $id 34 | * @param array $options 35 | * 36 | * @return object 37 | */ 38 | protected function find($product_id, $id, $options = []) 39 | { 40 | return Query::init() 41 | ->setEndpoint("products/{$product_id}/variations") 42 | ->find($id, $options); 43 | } 44 | 45 | /** 46 | * Create new Item. 47 | * 48 | * @param int $product_id 49 | * @param array $data 50 | * 51 | * @return object 52 | */ 53 | protected function create($product_id, $data) 54 | { 55 | return Query::init() 56 | ->setEndpoint("products/{$product_id}/variations") 57 | ->create($data); 58 | } 59 | 60 | /** 61 | * Update Existing Item. 62 | * 63 | * @param int $product_id 64 | * @param int $id 65 | * @param array $data 66 | * 67 | * @return object 68 | */ 69 | protected function update($product_id, $id, $data) 70 | { 71 | return Query::init() 72 | ->setEndpoint("products/{$product_id}/variations") 73 | ->update($id, $data); 74 | } 75 | 76 | /** 77 | * Destroy Item. 78 | * 79 | * @param int $product_id 80 | * @param int $id 81 | * @param array $options 82 | * 83 | * @return object 84 | */ 85 | protected function delete($product_id, $id, $options = []) 86 | { 87 | return Query::init() 88 | ->setEndpoint("products/{$product_id}/variations") 89 | ->delete($id, $options); 90 | } 91 | 92 | /** 93 | * Batch Update. 94 | * 95 | * @param int $product_id 96 | * @param array $data 97 | * 98 | * @return object 99 | */ 100 | protected function batch($product_id, $data) 101 | { 102 | return Query::init() 103 | ->setEndpoint("products/{$product_id}/variations") 104 | ->batch($data); 105 | } 106 | 107 | /** 108 | * Paginate results. 109 | * 110 | * @param int $product_id 111 | * @param int $per_page 112 | * @param int $current_page 113 | * @param array $options 114 | * 115 | * @return array 116 | */ 117 | protected function paginate( 118 | $product_id, 119 | $per_page = 10, 120 | $current_page = 1, 121 | $options = [] 122 | ) { 123 | return Query::init() 124 | ->setEndpoint("products/{$product_id}/variations") 125 | ->paginate($per_page, $current_page, $options); 126 | } 127 | 128 | /** 129 | * Count all results. 130 | * 131 | * @param int $product_id 132 | * 133 | * @return int 134 | */ 135 | protected function count($product_id) 136 | { 137 | return Query::init() 138 | ->setEndpoint("products/{$product_id}/variations") 139 | ->count(); 140 | } 141 | 142 | /** 143 | * Store data. 144 | * 145 | * @param int $product_id 146 | * 147 | * @return array 148 | */ 149 | public function save($product_id) 150 | { 151 | return Query::init() 152 | ->setEndpoint("products/{$product_id}/variations") 153 | ->save(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Models/Webhook.php: -------------------------------------------------------------------------------- 1 | endpoint = $endpoint; 18 | } 19 | 20 | public function setEndpoint($endpoint) 21 | { 22 | $this->endpoint = $endpoint; 23 | 24 | return $this; 25 | } 26 | 27 | public function init() 28 | { 29 | if (!static::$instance) { 30 | static::$instance = new static(); 31 | } 32 | 33 | return static::$instance; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Traits/QueryBuilderTrait.php: -------------------------------------------------------------------------------- 1 | isLazyCollection) { 50 | return LazyCollection::make(WooCommerce::all($this->endpoint, $options)); 51 | } 52 | 53 | if ($this->isCollection) { 54 | return collect(WooCommerce::all($this->endpoint, $options)); 55 | } 56 | 57 | return WooCommerce::all($this->endpoint, $options); 58 | } 59 | 60 | /** 61 | * Retrieve single Item. 62 | * 63 | * @param int $id 64 | * @param array $options 65 | * 66 | * @return object 67 | */ 68 | protected function find($id, $options = []) 69 | { 70 | if ($this->isLazyCollection) { 71 | return LazyCollection::make(WooCommerce::find("{$this->endpoint}/{$id}", $options)); 72 | } 73 | 74 | if ($this->isCollection) { 75 | return collect(WooCommerce::find("{$this->endpoint}/{$id}", $options)); 76 | } 77 | 78 | return WooCommerce::find("{$this->endpoint}/{$id}", $options); 79 | } 80 | 81 | /** 82 | * Create new Item. 83 | * 84 | * @param array $data 85 | * 86 | * @return object 87 | */ 88 | protected function create($data) 89 | { 90 | if ($this->isLazyCollection) { 91 | return LazyCollection::make(WooCommerce::create($this->endpoint, $data)); 92 | } 93 | 94 | if ($this->isCollection) { 95 | return collect(WooCommerce::create($this->endpoint, $data)); 96 | } 97 | 98 | return WooCommerce::create($this->endpoint, $data); 99 | } 100 | 101 | /** 102 | * Update Existing Item. 103 | * 104 | * @param int $id 105 | * @param array $data 106 | * 107 | * @return object 108 | */ 109 | protected function update($id, $data) 110 | { 111 | if ($this->isLazyCollection) { 112 | return LazyCollection::make(WooCommerce::update("{$this->endpoint}/{$id}", $data)); 113 | } 114 | 115 | if ($this->isCollection) { 116 | return collect(WooCommerce::update("{$this->endpoint}/{$id}", $data)); 117 | } 118 | 119 | return WooCommerce::update("{$this->endpoint}/{$id}", $data); 120 | } 121 | 122 | /** 123 | * Destroy Item. 124 | * 125 | * @param int $id 126 | * @param array $options 127 | * 128 | * @return object 129 | */ 130 | protected function delete($id, $options = []) 131 | { 132 | if ($this->isLazyCollection) { 133 | return LazyCollection::make(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); 134 | } 135 | 136 | if ($this->isCollection) { 137 | return collect(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); 138 | } 139 | 140 | return WooCommerce::delete("{$this->endpoint}/{$id}", $options); 141 | } 142 | 143 | /** 144 | * Batch Update. 145 | * 146 | * @param array $data 147 | * 148 | * @return object 149 | */ 150 | protected function batch($data) 151 | { 152 | if ($this->isLazyCollection) { 153 | return LazyCollection::make(WooCommerce::create("{$this->endpoint}/batch", $data)); 154 | } 155 | 156 | if ($this->isCollection) { 157 | return collect(WooCommerce::create("{$this->endpoint}/batch", $data)); 158 | } 159 | 160 | return WooCommerce::create("{$this->endpoint}/batch", $data); 161 | } 162 | 163 | /** 164 | * Retrieve data. 165 | * 166 | * @return array 167 | */ 168 | protected function get() 169 | { 170 | if ($this->isLazyCollection) { 171 | return LazyCollection::make(WooCommerce::all($this->endpoint, $this->options)); 172 | } 173 | 174 | if ($this->isCollection) { 175 | return collect(WooCommerce::all($this->endpoint, $this->options)); 176 | } 177 | 178 | return WooCommerce::all($this->endpoint, $this->options); 179 | } 180 | 181 | /** 182 | * Retrieve data. 183 | * 184 | * @return object 185 | */ 186 | protected function first() 187 | { 188 | if ($this->isLazyCollection) { 189 | return LazyCollection::make($this->get()[0] ?? new \stdClass()); 190 | } 191 | 192 | if ($this->isCollection) { 193 | return collect($this->get()[0] ?? new \stdClass()); 194 | } 195 | 196 | return collect($this->get()[0] ?? new \stdClass()); 197 | } 198 | 199 | /** 200 | * Set original. 201 | * 202 | * @return object $this 203 | */ 204 | protected function withOriginal() 205 | { 206 | $this->isOriginal = true; 207 | $this->isCollection = false; 208 | $this->isLazyCollection = false; 209 | 210 | return $this; 211 | } 212 | 213 | /** 214 | * Set collection. 215 | * 216 | * @return object $this 217 | */ 218 | protected function withCollection() 219 | { 220 | $this->isOriginal = false; 221 | $this->isCollection = true; 222 | $this->isLazyCollection = false; 223 | 224 | return $this; 225 | } 226 | 227 | /** 228 | * Set lazy collection. 229 | * 230 | * @return object $this 231 | */ 232 | protected function withLazyCollection() 233 | { 234 | $this->isOriginal = false; 235 | $this->isCollection = false; 236 | $this->isLazyCollection = true; 237 | 238 | return $this; 239 | } 240 | 241 | /** 242 | * Set options for woocommerce request. 243 | * 244 | * @param array $parameters 245 | * 246 | * @return object $this 247 | */ 248 | protected function options($parameters) 249 | { 250 | if (!is_array($parameters)) { 251 | throw new \Exception('Options must be an array', 1); 252 | } 253 | 254 | if (empty($parameters)) { 255 | throw new \Exception('Options must be pass at least one element', 1); 256 | } 257 | 258 | foreach ($parameters as $key => $value) { 259 | $this->options[$key] = $value; 260 | } 261 | 262 | return $this; 263 | } 264 | 265 | /** 266 | * Join options for woocommerce request. 267 | * 268 | * @param array $parameters 269 | * 270 | * @return object $this 271 | */ 272 | protected function where(...$parameters) 273 | { 274 | if (count($parameters) < 2 || count($parameters) > 3) { 275 | throw new \Exception('You can pass minimum 2 and maximum 3 paramneters'); 276 | } 277 | $field = strtolower($parameters[0]); 278 | $value = count($parameters) == 3 ? $parameters[2] : $parameters[1]; 279 | 280 | switch ($field) { 281 | case 'name': case 'title': case 'description': 282 | $this->options['search'] = $value; 283 | break; 284 | default: 285 | $this->options[$field] = $value; 286 | break; 287 | } 288 | 289 | return $this; 290 | } 291 | 292 | /** 293 | * Set order direction. 294 | * 295 | * @param string $name 296 | * @param string $direction 297 | * 298 | * @return object $this 299 | */ 300 | protected function orderBy($name, $direction = 'desc') 301 | { 302 | $this->options['orderby'] = $name; 303 | $this->options['order'] = $direction; 304 | 305 | return $this; 306 | } 307 | 308 | /** 309 | * Paginate results. 310 | * 311 | * @param int $per_page 312 | * @param int $current_page 313 | * @param array $options 314 | * 315 | * @return array 316 | */ 317 | protected function paginate($per_page = 10, $current_page = 1, $options = []) 318 | { 319 | try { 320 | $this->options['per_page'] = (int) $per_page; 321 | 322 | if ($current_page > 0) { 323 | $this->options['page'] = (int) $current_page; 324 | } 325 | 326 | foreach ($options as $option => $value) { 327 | $this->options[$option] = $value; 328 | } 329 | 330 | $data = $this->get(); 331 | $totalResults = WooCommerce::countResults(); 332 | $totalPages = WooCommerce::countPages(); 333 | $currentPage = WooCommerce::current(); 334 | $previousPage = WooCommerce::previous(); 335 | $nextPage = WooCommerce::next(); 336 | 337 | $pagination = [ 338 | 'total_results' => $totalResults, 339 | 'total_pages' => $totalPages, 340 | 'current_page' => $currentPage, 341 | 'previous_page' => $previousPage, 342 | 'next_page' => $nextPage, 343 | 'first_page' => 1, 344 | 'last_page' => $totalResults, 345 | ]; 346 | 347 | $results = [ 348 | 'meta' => $pagination, 349 | 'data' => $data, 350 | ]; 351 | 352 | if ($this->isLazyCollection) { 353 | return LazyCollection::make($results); 354 | } 355 | 356 | if ($this->isCollection) { 357 | return collect($results); 358 | } 359 | 360 | return $results; 361 | } catch (\Exception $ex) { 362 | throw new \Exception($ex->getMessage(), 1); 363 | } 364 | } 365 | 366 | /** 367 | * Count all results. 368 | * 369 | * @return int 370 | */ 371 | protected function count() 372 | { 373 | try { 374 | $results = WooCommerce::all($this->endpoint, $this->options); 375 | $totalResults = WooCommerce::countResults(); 376 | 377 | return $totalResults; 378 | } catch (\Exception $ex) { 379 | throw new \Exception($ex->getMessage(), 1); 380 | } 381 | } 382 | 383 | /** 384 | * Store data. 385 | * 386 | * @return array 387 | */ 388 | public function save() 389 | { 390 | $this->results = WooCommerce::create($this->endpoint, $this->properties); 391 | 392 | if ($this->isLazyCollection) { 393 | return LazyCollection::make($this->results); 394 | } 395 | 396 | if ($this->isCollection) { 397 | return collect($this->results); 398 | } 399 | 400 | return $this->results; 401 | } 402 | } 403 | -------------------------------------------------------------------------------- /src/Traits/WooCommerceTrait.php: -------------------------------------------------------------------------------- 1 | client->get($endpoint, $options); 22 | } catch (\Exception $e) { 23 | throw new \Exception($e->getMessage(), 1); 24 | } 25 | } 26 | 27 | /** 28 | * GET method. 29 | * Retrieve Single data. 30 | * 31 | * @param string $endpoint API endpoint. 32 | * @param array $options 33 | * 34 | * @return array 35 | */ 36 | public function find($endpoint = '', $options = []) 37 | { 38 | try { 39 | self::__construct(); 40 | 41 | return $this->client->get($endpoint, $options); 42 | } catch (\Exception $e) { 43 | throw new \Exception($e->getMessage(), 1); 44 | } 45 | } 46 | 47 | /** 48 | * POST method. 49 | * Insert data. 50 | * 51 | * @param string $endpoint API endpoint. 52 | * @param array $data 53 | * 54 | * @return array 55 | */ 56 | public function create($endpoint, $data) 57 | { 58 | try { 59 | self::__construct(); 60 | 61 | return $this->client->post($endpoint, $data); 62 | } catch (\Exception $e) { 63 | throw new \Exception($e->getMessage(), 1); 64 | } 65 | } 66 | 67 | /** 68 | * PUT method. 69 | * Update data. 70 | * 71 | * @param string $endpoint API endpoint. 72 | * @param array $data 73 | * 74 | * @return array 75 | */ 76 | public function update($endpoint, $data) 77 | { 78 | try { 79 | self::__construct(); 80 | 81 | return $this->client->put($endpoint, $data); 82 | } catch (\Exception $e) { 83 | throw new \Exception($e->getMessage(), 1); 84 | } 85 | } 86 | 87 | /** 88 | * DELETE method. 89 | * Remove data. 90 | * 91 | * @param string $endpoint API endpoint. 92 | * @param array $options 93 | * 94 | * @return array 95 | */ 96 | public function delete($endpoint, $options = []) 97 | { 98 | try { 99 | self::__construct(); 100 | 101 | return $this->client->delete($endpoint, $options); 102 | } catch (\Exception $e) { 103 | throw new \Exception($e->getMessage(), 1); 104 | } 105 | } 106 | 107 | /** 108 | * Return the last request header. 109 | * 110 | * @return \Automattic\WooCommerce\HttpClient\Request 111 | */ 112 | public function getRequest() 113 | { 114 | try { 115 | return $this->client->http->getRequest(); 116 | } catch (\Exception $e) { 117 | throw new \Exception($e->getMessage(), 1); 118 | } 119 | } 120 | 121 | /** 122 | * Return the http response headers from last request. 123 | * 124 | * @return \Automattic\WooCommerce\HttpClient\Response 125 | */ 126 | public function getResponse() 127 | { 128 | try { 129 | return $this->client->http->getResponse(); 130 | } catch (\Exception $e) { 131 | throw new \Exception($e->getMessage(), 1); 132 | } 133 | } 134 | 135 | /** 136 | * Count the total results and return it. 137 | * 138 | * @return int 139 | */ 140 | public function countResults() 141 | { 142 | return (int) $this->getResponse()->getHeaders()[$this->headers['header_total']]; 143 | } 144 | 145 | /** 146 | * Count the total pages and return. 147 | * 148 | * @return mixed 149 | */ 150 | public function countPages() 151 | { 152 | return (int) $this->getResponse()->getHeaders()[$this->headers['header_total_pages']]; 153 | } 154 | 155 | /** 156 | * Return the current page number. 157 | * 158 | * @return int 159 | */ 160 | public function current() 161 | { 162 | return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1; 163 | } 164 | 165 | /** 166 | * Return the previous page number. 167 | * 168 | * @return int|null 169 | */ 170 | public function previous() 171 | { 172 | $currentPage = $this->current(); 173 | 174 | return ($currentPage-- > 1) ? $currentPage : null; 175 | } 176 | 177 | /** 178 | * Return the next page number. 179 | * 180 | * @return int|null 181 | */ 182 | public function next() 183 | { 184 | $currentPage = $this->current(); 185 | 186 | return ($currentPage++ < $this->countPages()) ? $currentPage : null; 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /src/WooCommerceAnalyticsApi.php: -------------------------------------------------------------------------------- 1 | headers = [ 31 | 'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total', 32 | 'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages', 33 | ]; 34 | 35 | $this->client = new Client( 36 | config('woocommerce.store_url'), 37 | config('woocommerce.consumer_key'), 38 | config('woocommerce.consumer_secret'), 39 | [ 40 | 'version' => 'wc-analytics', 41 | 'wp_api' => config('woocommerce.wp_api_integration'), 42 | 'verify_ssl' => config('woocommerce.verify_ssl'), 43 | 'query_string_auth' => config('woocommerce.query_string_auth'), 44 | 'timeout' => config('woocommerce.timeout'), 45 | ] 46 | ); 47 | } catch (\Exception $e) { 48 | throw new \Exception($e->getMessage(), 1); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/WooCommerceApi.php: -------------------------------------------------------------------------------- 1 | headers = [ 31 | 'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total', 32 | 'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages', 33 | ]; 34 | 35 | $this->client = new Client( 36 | config('woocommerce.store_url'), 37 | config('woocommerce.consumer_key'), 38 | config('woocommerce.consumer_secret'), 39 | [ 40 | 'version' => 'wc/'.config('woocommerce.api_version'), 41 | 'wp_api' => config('woocommerce.wp_api_integration'), 42 | 'verify_ssl' => config('woocommerce.verify_ssl'), 43 | 'query_string_auth' => config('woocommerce.query_string_auth'), 44 | 'timeout' => config('woocommerce.timeout'), 45 | ] 46 | ); 47 | } catch (\Exception $e) { 48 | throw new \Exception($e->getMessage(), 1); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/WooCommerceServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/config/woocommerce.php' => config_path('woocommerce.php'), 18 | ], 'woocommerce'); 19 | } 20 | 21 | /** 22 | * Register the service provider. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | $this->mergeConfigFrom( 29 | __DIR__.'/config/woocommerce.php', 30 | 'woocommerce' 31 | ); 32 | 33 | $this->app->singleton('WooCommerceApi', function () { 34 | return new WooCommerceApi(); 35 | }); 36 | $this->app->alias('Codexshaper\Woocommerce\WooCommerceApi', 'WooCommerceApi'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/config/woocommerce.php: -------------------------------------------------------------------------------- 1 | env('WOOCOMMERCE_STORE_URL', 'YOUR_STORE_URL'), 10 | 11 | /** 12 | *================================================================================ 13 | * Consumer Key 14 | *================================================================================. 15 | */ 16 | 'consumer_key' => env('WOOCOMMERCE_CONSUMER_KEY', 'YOUR_CONSUMER_KEY'), 17 | 18 | /** 19 | * Consumer Secret. 20 | */ 21 | 'consumer_secret' => env('WOOCOMMERCE_CONSUMER_SECRET', 'YOUR_CONSUMER_SECRET'), 22 | 23 | /** 24 | *================================================================================ 25 | * SSL support 26 | *================================================================================. 27 | */ 28 | 'verify_ssl' => env('WOOCOMMERCE_VERIFY_SSL', false), 29 | 30 | /** 31 | *================================================================================ 32 | * Woocommerce API version 33 | *================================================================================. 34 | */ 35 | 'api_version' => env('WOOCOMMERCE_API_VERSION', 'v3'), 36 | 37 | /** 38 | *================================================================================ 39 | * Enable WP API Integration 40 | *================================================================================. 41 | */ 42 | 'wp_api' => env('WP_API_INTEGRATION', true), 43 | 44 | /** 45 | *================================================================================ 46 | * Force Basic Authentication as query string 47 | *================================================================================. 48 | */ 49 | 'query_string_auth' => env('WOOCOMMERCE_WP_QUERY_STRING_AUTH', false), 50 | 51 | /** 52 | *================================================================================ 53 | * Default WP timeout 54 | *================================================================================. 55 | */ 56 | 'timeout' => env('WOOCOMMERCE_WP_TIMEOUT', 15), 57 | 58 | /** 59 | *================================================================================ 60 | * Total results header 61 | * Default value X-WP-Total 62 | *================================================================================. 63 | */ 64 | 'header_total' => env('WOOCOMMERCE_WP_HEADER_TOTAL', 'X-WP-Total'), 65 | 66 | /** 67 | *================================================================================ 68 | * Total pages header 69 | * Default value X-WP-TotalPages 70 | *================================================================================. 71 | */ 72 | 'header_total_pages' => env('WOOCOMMERCE_WP_HEADER_TOTAL_PAGES', 'X-WP-TotalPages'), 73 | ]; 74 | -------------------------------------------------------------------------------- /tests/Product.php: -------------------------------------------------------------------------------- 1 | assertSame(true, true); 13 | } 14 | } 15 | --------------------------------------------------------------------------------