create-coupon.php
7 months ago
create-customer.php
7 months ago
delete-coupon.php
7 months ago
delete-customer.php
7 months ago
get-customer.php
7 months ago
get-order.php
7 months ago
get-product.php
7 months ago
list-customers.php
7 months ago
list-products.php
7 months ago
update-order-status.php
7 months ago
get-product.php
257 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GetProduct. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category GetProduct |
| 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\FluentCart\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use FluentCart\App\Models\Product; |
| 19 | use FluentCart\App\Models\ProductVariation; |
| 20 | |
| 21 | /** |
| 22 | * GetProduct |
| 23 | * |
| 24 | * @category GetProduct |
| 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 GetProduct extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'FluentCart'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'fluentcart_get_product'; |
| 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' => __( 'Get Product', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 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 array|void |
| 73 | * |
| 74 | * @throws \Exception Exception. |
| 75 | */ |
| 76 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 77 | if ( ! class_exists( '\FluentCart\App\Models\Product' ) ) { |
| 78 | return [ |
| 79 | 'status' => 'error', |
| 80 | 'message' => __( 'FluentCart is not installed or activated.', 'suretriggers' ), |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | $product_id = isset( $selected_options['product_id'] ) ? $selected_options['product_id'] : ''; |
| 85 | $product_slug = isset( $selected_options['product_slug'] ) ? $selected_options['product_slug'] : ''; |
| 86 | $product_sku = isset( $selected_options['product_sku'] ) ? $selected_options['product_sku'] : ''; |
| 87 | |
| 88 | if ( empty( $product_id ) && empty( $product_slug ) && empty( $product_sku ) ) { |
| 89 | return [ |
| 90 | 'status' => 'error', |
| 91 | 'message' => __( 'Product ID, slug, or SKU is required.', 'suretriggers' ), |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | $product = null; |
| 97 | |
| 98 | // Find product by ID first (most specific). |
| 99 | if ( ! empty( $product_id ) ) { |
| 100 | $product = Product::find( $product_id ); |
| 101 | } elseif ( ! empty( $product_slug ) ) { |
| 102 | // Then by slug. |
| 103 | $product = Product::where( 'post_name', $product_slug )->first(); |
| 104 | } elseif ( ! empty( $product_sku ) ) { |
| 105 | // Finally by SKU. |
| 106 | $product = Product::whereHas( |
| 107 | 'meta', |
| 108 | function( $q ) use ( $product_sku ) { |
| 109 | $q->where( 'meta_key', '_fct_sku' )->where( 'meta_value', $product_sku ); |
| 110 | } |
| 111 | )->first(); |
| 112 | } |
| 113 | |
| 114 | if ( ! $product ) { |
| 115 | return [ |
| 116 | 'status' => 'error', |
| 117 | 'message' => __( 'Product not found.', 'suretriggers' ), |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | $product_meta = get_post_meta( $product->ID ); |
| 122 | if ( ! is_array( $product_meta ) ) { |
| 123 | $product_meta = []; |
| 124 | } |
| 125 | |
| 126 | $context = [ |
| 127 | 'product_id' => $product->ID, |
| 128 | 'title' => $product->post_title, |
| 129 | 'content' => $product->post_content, |
| 130 | 'excerpt' => $product->post_excerpt, |
| 131 | 'status' => $product->post_status, |
| 132 | 'slug' => $product->post_name, |
| 133 | 'created_at' => $product->post_date, |
| 134 | 'updated_at' => $product->post_modified, |
| 135 | 'permalink' => get_permalink( $product->ID ), |
| 136 | 'edit_link' => admin_url( 'post.php?post=' . $product->ID . '&action=edit' ), |
| 137 | ]; |
| 138 | |
| 139 | // Add product meta data with proper type checking. |
| 140 | $context['price'] = ( isset( $product_meta['_fct_price'] ) && is_array( $product_meta['_fct_price'] ) && isset( $product_meta['_fct_price'][0] ) ) ? $product_meta['_fct_price'][0] : ''; |
| 141 | $context['sale_price'] = ( isset( $product_meta['_fct_sale_price'] ) && is_array( $product_meta['_fct_sale_price'] ) && isset( $product_meta['_fct_sale_price'][0] ) ) ? $product_meta['_fct_sale_price'][0] : ''; |
| 142 | $context['regular_price'] = ( isset( $product_meta['_fct_regular_price'] ) && is_array( $product_meta['_fct_regular_price'] ) && isset( $product_meta['_fct_regular_price'][0] ) ) ? $product_meta['_fct_regular_price'][0] : $context['price']; |
| 143 | $context['product_type'] = ( isset( $product_meta['_fct_product_type'] ) && is_array( $product_meta['_fct_product_type'] ) && isset( $product_meta['_fct_product_type'][0] ) ) ? $product_meta['_fct_product_type'][0] : 'simple'; |
| 144 | $context['sku'] = ( isset( $product_meta['_fct_sku'] ) && is_array( $product_meta['_fct_sku'] ) && isset( $product_meta['_fct_sku'][0] ) ) ? $product_meta['_fct_sku'][0] : ''; |
| 145 | $context['stock_quantity'] = ( isset( $product_meta['_fct_stock_quantity'] ) && is_array( $product_meta['_fct_stock_quantity'] ) && isset( $product_meta['_fct_stock_quantity'][0] ) ) ? $product_meta['_fct_stock_quantity'][0] : ''; |
| 146 | $context['manage_stock'] = ( isset( $product_meta['_fct_manage_stock'] ) && is_array( $product_meta['_fct_manage_stock'] ) && isset( $product_meta['_fct_manage_stock'][0] ) ) ? $product_meta['_fct_manage_stock'][0] : 'no'; |
| 147 | $context['stock_status'] = ( isset( $product_meta['_fct_stock_status'] ) && is_array( $product_meta['_fct_stock_status'] ) && isset( $product_meta['_fct_stock_status'][0] ) ) ? $product_meta['_fct_stock_status'][0] : 'instock'; |
| 148 | $context['backorders'] = ( isset( $product_meta['_fct_backorders'] ) && is_array( $product_meta['_fct_backorders'] ) && isset( $product_meta['_fct_backorders'][0] ) ) ? $product_meta['_fct_backorders'][0] : 'no'; |
| 149 | $context['featured'] = ( isset( $product_meta['_fct_featured'] ) && is_array( $product_meta['_fct_featured'] ) && isset( $product_meta['_fct_featured'][0] ) ) ? $product_meta['_fct_featured'][0] : 'no'; |
| 150 | $context['virtual'] = ( isset( $product_meta['_fct_virtual'] ) && is_array( $product_meta['_fct_virtual'] ) && isset( $product_meta['_fct_virtual'][0] ) ) ? $product_meta['_fct_virtual'][0] : 'no'; |
| 151 | $context['downloadable'] = ( isset( $product_meta['_fct_downloadable'] ) && is_array( $product_meta['_fct_downloadable'] ) && isset( $product_meta['_fct_downloadable'][0] ) ) ? $product_meta['_fct_downloadable'][0] : 'no'; |
| 152 | $context['weight'] = ( isset( $product_meta['_fct_weight'] ) && is_array( $product_meta['_fct_weight'] ) && isset( $product_meta['_fct_weight'][0] ) ) ? $product_meta['_fct_weight'][0] : ''; |
| 153 | $context['length'] = ( isset( $product_meta['_fct_length'] ) && is_array( $product_meta['_fct_length'] ) && isset( $product_meta['_fct_length'][0] ) ) ? $product_meta['_fct_length'][0] : ''; |
| 154 | $context['width'] = ( isset( $product_meta['_fct_width'] ) && is_array( $product_meta['_fct_width'] ) && isset( $product_meta['_fct_width'][0] ) ) ? $product_meta['_fct_width'][0] : ''; |
| 155 | $context['height'] = ( isset( $product_meta['_fct_height'] ) && is_array( $product_meta['_fct_height'] ) && isset( $product_meta['_fct_height'][0] ) ) ? $product_meta['_fct_height'][0] : ''; |
| 156 | |
| 157 | // Pricing calculations. |
| 158 | $regular_price_value = ! empty( $context['regular_price'] ) ? $context['regular_price'] : $context['price']; |
| 159 | $regular_price = is_numeric( $regular_price_value ) ? floatval( $regular_price_value ) : 0.0; |
| 160 | $sale_price = is_numeric( $context['sale_price'] ) ? floatval( $context['sale_price'] ) : 0.0; |
| 161 | |
| 162 | if ( $sale_price > 0 && $sale_price < $regular_price ) { |
| 163 | $context['on_sale'] = true; |
| 164 | $context['discount_amount'] = $regular_price - $sale_price; |
| 165 | $context['discount_percentage'] = $regular_price > 0 ? round( ( $context['discount_amount'] / $regular_price ) * 100, 2 ) : 0; |
| 166 | $context['effective_price'] = $sale_price; |
| 167 | } else { |
| 168 | $context['on_sale'] = false; |
| 169 | $context['discount_amount'] = 0; |
| 170 | $context['discount_percentage'] = 0; |
| 171 | $context['effective_price'] = $regular_price; |
| 172 | } |
| 173 | |
| 174 | // Get featured image. |
| 175 | $thumbnail_id = get_post_thumbnail_id( $product->ID ); |
| 176 | if ( $thumbnail_id ) { |
| 177 | $context['featured_image'] = [ |
| 178 | 'id' => $thumbnail_id, |
| 179 | 'url' => wp_get_attachment_url( $thumbnail_id ), |
| 180 | 'alt' => get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ), |
| 181 | 'thumbnail' => wp_get_attachment_image_url( $thumbnail_id, 'thumbnail' ), |
| 182 | 'medium' => wp_get_attachment_image_url( $thumbnail_id, 'medium' ), |
| 183 | 'large' => wp_get_attachment_image_url( $thumbnail_id, 'large' ), |
| 184 | 'full' => wp_get_attachment_image_url( $thumbnail_id, 'full' ), |
| 185 | ]; |
| 186 | } else { |
| 187 | $context['featured_image'] = null; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | // Get categories and tags. |
| 193 | $categories = wp_get_post_terms( $product->ID, 'fct_product_category' ); |
| 194 | $tags = wp_get_post_terms( $product->ID, 'fct_product_tag' ); |
| 195 | |
| 196 | $context['categories'] = []; |
| 197 | $context['category_names'] = []; |
| 198 | if ( ! is_wp_error( $categories ) && is_array( $categories ) ) { |
| 199 | foreach ( $categories as $category ) { |
| 200 | $context['categories'][] = [ |
| 201 | 'id' => $category->term_id, |
| 202 | 'name' => $category->name, |
| 203 | 'slug' => $category->slug, |
| 204 | ]; |
| 205 | $context['category_names'][] = $category->name; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | $context['tags'] = []; |
| 210 | $context['tag_names'] = []; |
| 211 | if ( ! is_wp_error( $tags ) && is_array( $tags ) ) { |
| 212 | foreach ( $tags as $tag ) { |
| 213 | $context['tags'][] = [ |
| 214 | 'id' => $tag->term_id, |
| 215 | 'name' => $tag->name, |
| 216 | 'slug' => $tag->slug, |
| 217 | ]; |
| 218 | $context['tag_names'][] = $tag->name; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | // Add product attributes for variable products. |
| 224 | if ( 'variable' === $context['product_type'] ) { |
| 225 | $product_attributes_raw = ( isset( $product_meta['_fct_product_attributes'] ) && is_array( $product_meta['_fct_product_attributes'] ) && isset( $product_meta['_fct_product_attributes'][0] ) ) ? $product_meta['_fct_product_attributes'][0] : ''; |
| 226 | $attributes_data = []; |
| 227 | |
| 228 | if ( ! empty( $product_attributes_raw ) && is_string( $product_attributes_raw ) ) { |
| 229 | $product_attributes = maybe_unserialize( $product_attributes_raw ); |
| 230 | if ( is_array( $product_attributes ) ) { |
| 231 | foreach ( $product_attributes as $attr_name => $attr_data ) { |
| 232 | $attributes_data[] = [ |
| 233 | 'name' => $attr_name, |
| 234 | 'label' => isset( $attr_data['name'] ) ? $attr_data['name'] : $attr_name, |
| 235 | 'values' => isset( $attr_data['value'] ) ? $attr_data['value'] : [], |
| 236 | 'visible' => isset( $attr_data['is_visible'] ) ? $attr_data['is_visible'] : false, |
| 237 | 'variation' => isset( $attr_data['is_variation'] ) ? $attr_data['is_variation'] : false, |
| 238 | ]; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | $context['product_attributes'] = $attributes_data; |
| 243 | } |
| 244 | |
| 245 | return $context; |
| 246 | |
| 247 | } catch ( \Exception $e ) { |
| 248 | return [ |
| 249 | 'status' => 'error', |
| 250 | 'message' => $e->getMessage(), |
| 251 | ]; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | GetProduct::get_instance(); |
| 257 |