| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WooCommerce_Actions |
| 16 |
*/ |
| 17 |
class WooCommerce_Actions extends Action { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'woocommerce'; |
| 24 |
$this->group = __( 'WooCommerce', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Execute the action |
| 29 |
* |
| 30 |
* @param array $action_data Configuration for this action from the workflow. |
| 31 |
* @param array $trigger_data Data passed from the trigger. |
| 32 |
* @return bool|array |
| 33 |
*/ |
| 34 |
public function execute( $action_data, $trigger_data ) { |
| 35 |
$action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; |
| 36 |
|
| 37 |
if ( empty( $action_id ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
switch ( $action_id ) { |
| 42 |
case 'wc_order_created': |
| 43 |
return $this->create_order( $action_data, $trigger_data ); |
| 44 |
default: |
| 45 |
return array( 'success' => true, 'message' => "Action {$action_id} executed (simulated)" ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Create a new WooCommerce order |
| 51 |
*/ |
| 52 |
private function create_order( $config, $trigger_data ) { |
| 53 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 54 |
return array( 'success' => false, 'message' => 'WooCommerce is not active.' ); |
| 55 |
} |
| 56 |
|
| 57 |
// Get User ID from trigger data or previous actions. |
| 58 |
$user_id = isset( $trigger_data['user_id'] ) ? $trigger_data['user_id'] : get_current_user_id(); |
| 59 |
|
| 60 |
if ( ! $user_id ) { |
| 61 |
//error_log( 'WPbot Automator - Create Order: No user ID found.' ); |
| 62 |
return array( 'success' => false, 'message' => 'No user ID found for order.' ); |
| 63 |
} |
| 64 |
|
| 65 |
$product_id = isset( $config['config']['product_id'] ) ? (int) $config['config']['product_id'] : 0; |
| 66 |
if ( ! $product_id ) { |
| 67 |
//error_log( 'WPbot Automator - Create Order: No product ID selected.' ); |
| 68 |
return array( 'success' => false, 'message' => 'No product selected.' ); |
| 69 |
} |
| 70 |
|
| 71 |
try { |
| 72 |
$order = wc_create_order( array( 'customer_id' => $user_id ) ); |
| 73 |
$order->add_product( wc_get_product( $product_id ), 1 ); |
| 74 |
$order->calculate_totals(); |
| 75 |
$order->update_status( 'pending', 'Created via WPbot Automator', true ); |
| 76 |
|
| 77 |
//error_log( sprintf( 'WPbot Automator - Order %d created for user %d', $order->get_id(), $user_id ) ); |
| 78 |
|
| 79 |
return array( |
| 80 |
'success' => true, |
| 81 |
'order_id' => $order->get_id(), |
| 82 |
'message' => 'Order created successfully.', |
| 83 |
); |
| 84 |
} catch ( \Exception $e ) { |
| 85 |
//error_log( 'WPbot Automator - Order creation failed: ' . $e->getMessage() ); |
| 86 |
return array( 'success' => false, 'message' => $e->getMessage() ); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|