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
get-all-products.php
271 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GetAllProducts. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category GetAllProducts |
| 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 SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Integrations\WooCommerce\WooCommerce; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * GetAllProducts |
| 22 | * |
| 23 | * @category GetAllProducts |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class GetAllProducts extends AutomateAction { |
| 31 | |
| 32 | use SingletonLoader; |
| 33 | |
| 34 | /** |
| 35 | * Integration type. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $integration = 'WooCommerce'; |
| 40 | |
| 41 | /** |
| 42 | * Action name. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $action = 'wc_get_all_products'; |
| 47 | |
| 48 | /** |
| 49 | * Register the action. |
| 50 | * |
| 51 | * @param array $actions Actions array. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'Get All Products', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 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 Selected options. |
| 71 | * @return array |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | if ( ! function_exists( 'wc_get_products' ) ) { |
| 75 | return [ |
| 76 | 'success' => false, |
| 77 | 'message' => __( 'WooCommerce functions not available.', 'suretriggers' ), |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | // Get parameters with defaults. |
| 82 | $limit = isset( $selected_options['limit'] ) ? intval( $selected_options['limit'] ) : 10; |
| 83 | $status = isset( $selected_options['status'] ) ? $selected_options['status'] : 'publish'; |
| 84 | |
| 85 | // Validate limit. |
| 86 | if ( $limit <= 0 || $limit > 100 ) { |
| 87 | $limit = 10; |
| 88 | } |
| 89 | |
| 90 | // Prepare query arguments. |
| 91 | $args = [ |
| 92 | 'limit' => $limit, |
| 93 | 'status' => $status, |
| 94 | 'orderby' => 'date', |
| 95 | 'order' => 'DESC', |
| 96 | 'return' => 'objects', |
| 97 | ]; |
| 98 | |
| 99 | // Get products - try alternative approach if wc_get_products fails. |
| 100 | $products = wc_get_products( $args ); |
| 101 | |
| 102 | // If we didn't get the expected count, try with WP_Query. |
| 103 | if ( is_array( $products ) && count( $products ) < $limit && $limit > 1 ) { |
| 104 | $wp_args = [ |
| 105 | 'post_type' => 'product', |
| 106 | 'post_status' => $status, |
| 107 | 'posts_per_page' => $limit, |
| 108 | 'orderby' => 'date', |
| 109 | 'order' => 'DESC', |
| 110 | ]; |
| 111 | |
| 112 | $query = new \WP_Query( $wp_args ); |
| 113 | $products = []; |
| 114 | |
| 115 | if ( $query->have_posts() ) { |
| 116 | while ( $query->have_posts() ) { |
| 117 | $query->the_post(); |
| 118 | $product = wc_get_product( get_the_ID() ); |
| 119 | if ( $product ) { |
| 120 | $products[] = $product; |
| 121 | } |
| 122 | } |
| 123 | wp_reset_postdata(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if ( empty( $products ) ) { |
| 128 | return [ |
| 129 | 'success' => true, |
| 130 | 'products' => [], |
| 131 | 'products_count' => 0, |
| 132 | 'message' => __( 'No products found.', 'suretriggers' ), |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | // Format products data. |
| 137 | $formatted_products = []; |
| 138 | if ( is_array( $products ) ) { |
| 139 | foreach ( $products as $product ) { |
| 140 | $formatted_products[] = $this->format_product_data( $product ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | $response_data = [ |
| 145 | 'success' => true, |
| 146 | 'products' => $formatted_products, |
| 147 | 'products_count' => count( $formatted_products ), |
| 148 | 'filters_applied' => [ |
| 149 | 'limit' => $limit, |
| 150 | 'status' => $status, |
| 151 | ], |
| 152 | 'message' => sprintf( __( 'Retrieved %d products.', 'suretriggers' ), count( $formatted_products ) ), |
| 153 | ]; |
| 154 | |
| 155 | return $response_data; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Format product data. |
| 160 | * |
| 161 | * @param \WC_Product $product WooCommerce product object. |
| 162 | * @return array |
| 163 | */ |
| 164 | private function format_product_data( $product ) { |
| 165 | // Type check to ensure we have a WC_Product object. |
| 166 | if ( ! is_a( $product, 'WC_Product' ) ) { |
| 167 | return []; |
| 168 | } |
| 169 | $product_data = [ |
| 170 | 'id' => $product->get_id(), |
| 171 | 'name' => $product->get_name(), |
| 172 | 'slug' => $product->get_slug(), |
| 173 | 'sku' => $product->get_sku(), |
| 174 | 'type' => $product->get_type(), |
| 175 | 'status' => $product->get_status(), |
| 176 | 'description' => $product->get_description(), |
| 177 | 'short_description' => $product->get_short_description(), |
| 178 | 'regular_price' => $product->get_regular_price(), |
| 179 | 'sale_price' => $product->get_sale_price(), |
| 180 | 'price' => $product->get_price(), |
| 181 | 'stock_status' => $product->get_stock_status(), |
| 182 | 'stock_quantity' => $product->get_stock_quantity(), |
| 183 | 'manage_stock' => $product->get_manage_stock(), |
| 184 | 'weight' => $product->get_weight(), |
| 185 | 'length' => $product->get_length(), |
| 186 | 'width' => $product->get_width(), |
| 187 | 'height' => $product->get_height(), |
| 188 | 'date_created' => $product->get_date_created() ? $product->get_date_created()->date( 'Y-m-d H:i:s' ) : '', |
| 189 | 'date_modified' => $product->get_date_modified() ? $product->get_date_modified()->date( 'Y-m-d H:i:s' ) : '', |
| 190 | 'permalink' => $product->get_permalink(), |
| 191 | ]; |
| 192 | |
| 193 | // Get product images. |
| 194 | $image_id = $product->get_image_id(); |
| 195 | $gallery_ids = $product->get_gallery_image_ids(); |
| 196 | |
| 197 | $product_data['images'] = [ |
| 198 | 'featured_image' => $image_id ? wp_get_attachment_image_url( (int) $image_id, 'full' ) : '', |
| 199 | 'gallery_images' => [], |
| 200 | ]; |
| 201 | |
| 202 | foreach ( $gallery_ids as $gallery_id ) { |
| 203 | $product_data['images']['gallery_images'][] = wp_get_attachment_image_url( (int) $gallery_id, 'full' ); |
| 204 | } |
| 205 | |
| 206 | // Get product categories. |
| 207 | $categories = get_the_terms( $product->get_id(), 'product_cat' ); |
| 208 | $product_data['categories'] = []; |
| 209 | if ( $categories && ! is_wp_error( $categories ) ) { |
| 210 | foreach ( $categories as $category ) { |
| 211 | $product_data['categories'][] = [ |
| 212 | 'id' => $category->term_id, |
| 213 | 'name' => $category->name, |
| 214 | 'slug' => $category->slug, |
| 215 | ]; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Get product tags. |
| 220 | $tags = get_the_terms( $product->get_id(), 'product_tag' ); |
| 221 | $product_data['tags'] = []; |
| 222 | if ( $tags && ! is_wp_error( $tags ) ) { |
| 223 | foreach ( $tags as $tag ) { |
| 224 | $product_data['tags'][] = [ |
| 225 | 'id' => $tag->term_id, |
| 226 | 'name' => $tag->name, |
| 227 | 'slug' => $tag->slug, |
| 228 | ]; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Get product attributes. |
| 233 | $attributes = $product->get_attributes(); |
| 234 | $product_data['attributes'] = []; |
| 235 | foreach ( $attributes as $attribute ) { |
| 236 | $product_data['attributes'][] = [ |
| 237 | 'name' => $attribute->get_name(), |
| 238 | 'options' => $attribute->get_options(), |
| 239 | 'visible' => $attribute->get_visible(), |
| 240 | ]; |
| 241 | } |
| 242 | |
| 243 | // Add variation info for variable products. |
| 244 | if ( $product->is_type( 'variable' ) ) { |
| 245 | $variations = $product->get_children(); |
| 246 | $product_data['variations'] = []; |
| 247 | foreach ( $variations as $variation_id ) { |
| 248 | $variation = wc_get_product( $variation_id ); |
| 249 | if ( $variation && is_a( $variation, 'WC_Product' ) ) { |
| 250 | $variation_attributes = method_exists( $variation, 'get_variation_attributes' ) ? $variation->get_variation_attributes() : []; |
| 251 | $product_data['variations'][] = [ |
| 252 | 'id' => $variation->get_id(), |
| 253 | 'sku' => $variation->get_sku(), |
| 254 | 'price' => $variation->get_price(), |
| 255 | 'regular_price' => $variation->get_regular_price(), |
| 256 | 'sale_price' => $variation->get_sale_price(), |
| 257 | 'stock_status' => $variation->get_stock_status(), |
| 258 | 'stock_quantity' => $variation->get_stock_quantity(), |
| 259 | 'attributes' => $variation_attributes, |
| 260 | ]; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return $product_data; |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | GetAllProducts::get_instance(); |
| 271 |