*/ protected static $instances = array(); public function __construct($order = null) { $this->order = $order; } public function setOrder($order) { $this->order = $order; } public static function fromOrderInstance($order) { $order_id = $order->get_id(); if (isset(self::$instances[$order_id])) { return self::$instances[$order_id]; } self::$instances[$order_id] = new self($order); return self::$instances[$order_id]; } public static function getVariables($variable_name = null) { $variables = array( 'currency' => [ 'label' => __( 'Currency', 'afroit-messaging-workflows' ), 'example' => 'EUR', 'condition' => GenericCondition::class, ], 'currency_symbol' => [ 'label' => __( 'Currency Symbol', 'afroit-messaging-workflows' ), 'example' => '€', 'condition' => GenericCondition::class, ], 'firstname' => [ 'label' => __( 'First Name', 'afroit-messaging-workflows' ), 'example' => 'Cyril', 'condition' => GenericCondition::class, ], 'lastname' => [ 'label' => __( 'Last Name', 'afroit-messaging-workflows' ), 'example' => 'Kim', 'condition' => GenericCondition::class, ], 'fullname' => [ 'label' => __( 'Full Name', 'afroit-messaging-workflows' ), 'example' => 'Cyril Kim', 'condition' => GenericCondition::class, ], 'phone' => [ 'label' => __( 'Phone', 'afroit-messaging-workflows' ), 'example' => '+1234567890', 'condition' => GenericCondition::class, ], 'billing_phone' => [ 'label' => __( 'Billing Phone', 'afroit-messaging-workflows' ), 'example' => '+1234567890', 'condition' => GenericCondition::class, ], 'user_email' => [ 'label' => __( 'User Email', 'afroit-messaging-workflows' ), 'example' => 'customer@example.com', 'condition' => GenericCondition::class, ], 'cart_url' => [ 'label' => __( 'Cart URL', 'afroit-messaging-workflows' ), 'example' => 'https://example.com/cart', 'condition' => GenericCondition::class, ], 'cart_recovery_url' => [ 'label' => __( 'Cart Recovery URL', 'afroit-messaging-workflows' ), 'example' => 'https://example.com/cart-recovery/1234567890', 'condition' => GenericCondition::class, ], 'item_count' => [ 'label' => __( 'Item Count', 'afroit-messaging-workflows' ), 'example' => '1', 'condition' => GenericCondition::class, ], 'item_ids' => [ 'label' => __( 'Item IDs', 'afroit-messaging-workflows' ), 'example' => '[2,3]', 'condition' => GenericCondition::class, ], 'order_id' => [ 'label' => __( 'Order ID', 'afroit-messaging-workflows' ), 'example' => '999', 'condition' => GenericCondition::class, ], 'order_number' => [ 'label' => __( 'Order Number', 'afroit-messaging-workflows' ), 'example' => 'order-999', 'condition' => GenericCondition::class, ], 'order_total' => [ 'label' => __( 'Order Total', 'afroit-messaging-workflows' ), 'example' => '100.00', 'condition' => GenericCondition::class, ], 'order_status' => [ 'label' => __( 'Order Status', 'afroit-messaging-workflows' ), 'example' => 'processing', 'condition' => GenericCondition::class, ], 'order_date' => [ 'label' => __( 'Order Date', 'afroit-messaging-workflows' ), 'example' => '2017-11-19 12:00:00', 'condition' => GenericCondition::class, ], 'payment_method' => [ 'label' => __( 'Payment Method', 'afroit-messaging-workflows' ), 'example' => 'CreditCard', 'condition' => GenericCondition::class, ], 'shipping_method' => [ 'label' => __( 'Shipping Method', 'afroit-messaging-workflows' ), 'example' => 'Standard', 'condition' => GenericCondition::class, ], 'product_list' => [ 'label' => __( 'Product List', 'afroit-messaging-workflows' ), 'example' => 'Product 1(x1), Product 2(x2)', 'condition' => GenericCondition::class, ], 'product_list_full' => [ 'label' => __( 'Product List Full', 'afroit-messaging-workflows' ), 'example' => 'Product 1 - 1 x 100.00\nProduct 2 - 2 x 200.00', 'condition' => GenericCondition::class, ], 'coupon_codes' => [ 'label' => __( 'Coupon Codes', 'afroit-messaging-workflows' ), 'example' => 'COUPON1, COUPON2', 'condition' => GenericCondition::class, ], 'order_url' => [ 'label' => __( 'Order URL', 'afroit-messaging-workflows' ), 'example' => 'https://example.com/order/999', 'condition' => GenericCondition::class, ], 'store_name' => [ 'label' => __( 'Store Name', 'afroit-messaging-workflows' ), 'example' => 'Example Store', 'condition' => GenericCondition::class, ], 'store_url' => [ 'label' => __( 'Store URL', 'afroit-messaging-workflows' ), 'example' => 'https://example.com', 'condition' => GenericCondition::class, ], 'store_title' => [ 'label' => __( 'Site Title', 'afroit-messaging-workflows' ), 'example' => 'Example Store', 'condition' => GenericCondition::class, ], 'store_tagline' => [ 'label' => __( 'Site Tagline', 'afroit-messaging-workflows' ), 'example' => 'Just another WordPress site', 'condition' => GenericCondition::class, ], 'admin_email' => [ 'label' => __( 'Admin Email', 'afroit-messaging-workflows' ), 'example' => 'admin@example.com', 'condition' => GenericCondition::class, ], 'dummy' => [ 'label' => __( 'Dummy', 'afroit-messaging-workflows' ), 'example' => '1', 'condition' => GenericCondition::class, ] ); /** * Filter workflow template / condition variables. * * @param array> $variables Variable map. */ $variables = apply_filters( 'afromewo_workflow_variables', $variables ); if ( ! is_array( $variables ) ) { $variables = array(); } if ($variable_name) { if (isset($variables[$variable_name])) { return $variables[$variable_name]; } return null; } return $variables; } public static function getList() { $variables = array_keys(self::getVariables()); $variables = array_map(function($variable) { return '{{' . $variable . '}}'; }, $variables); return $variables; } public function get($variable, $example = false, $order = null) { if ($order instanceof \WC_Order) { $this->setOrder($order); } try { $variable_name = $this->getVariableName($variable); if ($example) { $varArray= self::getVariables($variable_name); if ($varArray) { return $varArray['example']; } return ''; } else { $method = $this->getGetterMethod($variable_name); if (!method_exists($this, $method)) { return $variable; } return $this->{$method}(); } } catch (\Exception $e) { // don't kill the shopping flow, just return an empty string return ''; } } protected function getGetterMethod($name) { $name = $this->getVariableName($name); $parts = explode('_', $name); $camelCased = ''; foreach ($parts as $part) { $camelCased .= ucfirst($part); } return "get{$camelCased}"; } /** * Get the variable name from the variable name. * * @param string $variable_name * @return string */ protected function getVariableName($variable_name) { return trim(str_replace(['{{', '}}'], '', strtolower($variable_name))); } public function getCurrency() { if (!$this->order) { return get_woocommerce_currency(); } return $this->order->get_currency(); } public function getCurrencySymbol() { if (!$this->order) { return get_woocommerce_currency_symbol(); } return get_woocommerce_currency_symbol($this->order->get_currency()); } public function getFirstname() { return $this->order->get_billing_first_name(); } public function getLastname() { return $this->order->get_billing_last_name(); } public function getFullname() { return $this->order->get_billing_first_name() . ' ' . $this->order->get_billing_last_name(); } public function getPhone() { return $this->order->get_billing_phone(); } public function getBillingPhone() { return $this->order->get_billing_phone(); } public function getUseremail() { return $this->order->get_billing_email(); } public function getCartUrl() { return wc_get_cart_url(); } public function getOrderId() { return $this->order->get_id(); } public function getOrderNumber() { return $this->order->get_order_number(); } public function getOrderTotal() { return $this->order->get_total(); } public function getOrderStatus() { return $this->order->get_status(); } public function getOrderDate() { return $this->order->get_date_created()->format('Y-m-d H:i:s'); } public function getPaymentMethod() { return $this->order->get_payment_method_title(); } public function getShippingMethod() { return $this->order->get_shipping_method(); } public function getProductList() { $items = $this->order->get_items(); $product_list = ''; foreach ($items as $item) { $product_list .= $item->get_name() . ', '; } return rtrim($product_list, ', '); } public function getProductListFull() { $items = $this->order->get_items(); $product_list = ''; foreach ($items as $item) { $product_list .= $item->get_name() . ' - ' . $item->get_quantity() . "\n"; } return $product_list; } public function getCouponCodes() { $coupons = $this->order->get_coupons(); $coupon_codes = ''; foreach ($coupons as $coupon) { $coupon_codes .= $coupon->get_code() . ', '; } return $coupon_codes; } public function getOrderUrl() { return $this->order->get_edit_order_url(); } public function getStoreName() { return get_bloginfo('name'); } public function getStoreUrl() { return get_bloginfo('url'); } public function getStoreTitle() { return get_bloginfo('name'); } public function getStoreTagline() { return get_bloginfo('description'); } public function getAdminEmail() { return get_option('admin_email'); } public function getDummy() { return ''; } public static function getOrderIndependentVariables() { $self = new self(); return array( 'currency' => $self->getCurrency(), 'currency_symbol' => $self->getCurrencySymbol(), 'store_name' => $self->getStoreName(), 'store_url' => $self->getStoreUrl(), 'store_title' => $self->getStoreTitle(), 'store_tagline' => $self->getStoreTagline() ); } public function getCartRecoveryUrl() { if ( !empty($this->order->is_abandoned_cart) ) { return $this->order->get_cart_recovery_url(); } return wc_get_cart_url(); } public function getItemCount() { $items = $this->order->get_items(); return count($items); } public function getItemIds() { $items = $this->order->get_items(); $ids = []; foreach ($items as $item) { $ids[] = $item->get_product_id(); } return count($ids) > 0 ? wp_json_encode($ids) : '[]'; } }