add-attribute-terms.php
9 months ago
add-customer.php
10 months ago
add-emails-to-coupon.php
9 months ago
add-order-note.php
11 months ago
add-product-attributes.php
9 months ago
add-product-to-cart.php
9 months ago
add-update-order-custom-fields.php
11 months ago
apply-coupon.php
9 months ago
check-cart-status.php
9 months ago
check-if-order-is-renewal.php
5 months ago
create-attribute.php
9 months ago
create-bundle-product-order.php
7 months ago
create-coupon-code.php
11 months ago
create-multi-product-order.php
8 months ago
create-new-order.php
11 months ago
create-product-variation.php
1 year ago
create-product.php
1 year ago
create-variable-product.php
9 months ago
delete-coupon.php
1 year ago
delete-customer.php
9 months ago
fetch-coupon-details.php
11 months ago
find-orders-by-user-id.php
11 months ago
get-all-customers.php
9 months ago
get-all-products.php
9 months ago
get-customer-by-email.php
9 months ago
get-customer-by-id.php
9 months ago
get-order-details-by-order-id.php
11 months ago
get-order-type-by-order-id.php
5 months ago
get-orders-by-email.php
9 months ago
get-product-by-id.php
1 year ago
list-all-order-notes.php
9 months ago
list-all-orders.php
9 months ago
remove-product-from-cart.php
9 months ago
retrieve-coupons-totals.php
11 months ago
retrieve-customers-totals.php
11 months ago
retrieve-orders-totals.php
11 months ago
retrieve-products-totals.php
11 months ago
retrieve-refunds-list.php
11 months ago
retrieve-reviews-totals.php
11 months ago
retrieve-sales-report.php
11 months ago
retrieve-top-sellers-report.php
11 months ago
update-product-price.php
9 months ago
update-product-stock-quantity.php
9 months ago
update-status-of-order.php
11 months ago
find-orders-by-user-id.php
135 lines
| 1 | <?php |
| 2 | /** |
| 3 | * FindOrdersByUserID. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category FindOrdersByUserID |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\WooCommerce\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Integrations\WooCommerce\WooCommerce; |
| 19 | use SureTriggers\Traits\SingletonLoader; |
| 20 | |
| 21 | /** |
| 22 | * FindOrdersByUserID |
| 23 | * |
| 24 | * @category FindOrdersByUserID |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class FindOrdersByUserID extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'WooCommerce'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'wc_find_orders_by_user_id'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Find Orders by User ID', 'suretriggers' ), |
| 58 | 'action' => 'wc_find_orders_by_user_id', |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * |
| 72 | * @return object|array|void |
| 73 | * @throws Exception Exception. |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | $user_id = $selected_options['user_id']; |
| 77 | |
| 78 | $order_arg = [ |
| 79 | 'customer_id' => $user_id, |
| 80 | 'limit' => -1, |
| 81 | ]; |
| 82 | $customer_orders = wc_get_orders( $order_arg ); |
| 83 | if ( empty( $customer_orders ) ) { |
| 84 | return [ |
| 85 | 'status' => 'error', |
| 86 | 'message' => 'There are no orders for this user.', |
| 87 | ]; |
| 88 | } |
| 89 | $ids = []; |
| 90 | $status = []; |
| 91 | $product_ids = []; |
| 92 | $product_names = []; |
| 93 | /** |
| 94 | * |
| 95 | * Ignore line |
| 96 | * |
| 97 | * @phpstan-ignore-next-line |
| 98 | */ |
| 99 | foreach ( $customer_orders as $customer_order ) { |
| 100 | $order = wc_get_order( $customer_order ); |
| 101 | if ( $order && $order instanceof \WC_Order ) { |
| 102 | if ( $order->has_status( [ 'completed' ] ) ) { |
| 103 | $ids[] = $order->get_id(); |
| 104 | $status[] = $order->get_status(); |
| 105 | $items = $order->get_items(); |
| 106 | foreach ( $items as $item ) { |
| 107 | if ( $item instanceof \WC_Order_Item_Product ) { |
| 108 | $product = $item->get_product(); |
| 109 | if ( $product instanceof \WC_Product ) { |
| 110 | $product_ids[] = $product->get_id(); |
| 111 | $product_names[] = $product->get_name(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | if ( empty( $ids ) && empty( $product_ids ) ) { |
| 119 | return [ |
| 120 | 'status' => 'error', |
| 121 | 'message' => 'There are no completed orders for this user.', |
| 122 | ]; |
| 123 | } |
| 124 | $context = [ |
| 125 | 'ids' => implode( ', ', $ids ), |
| 126 | 'status' => implode( ', ', $status ), |
| 127 | 'product_ids' => implode( ', ', $product_ids ), |
| 128 | 'product_names' => implode( ', ', $product_names ), |
| 129 | ]; |
| 130 | return $context; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | FindOrdersByUserID::get_instance(); |
| 135 |