create-discount.php
10 months ago
create-purchase.php
8 months ago
delete-discount.php
10 months ago
edd-create-discount.php
10 months ago
fetch-discount-details.php
10 months ago
find-user-have-active-inactive-license-for-specific-download.php
2 years ago
find-user-have-subscription-for-specific-download.php
2 years ago
find-user-purchased-download.php
10 months ago
revoke-purchase.php
8 months ago
update-license-status.php
8 months ago
update-purchase-meta.php
8 months ago
update-purchase.php
8 months ago
create-discount.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateDiscountFree. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateDiscountFree |
| 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\EDD\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use Exception; |
| 19 | |
| 20 | /** |
| 21 | * CreateDiscountFree |
| 22 | * |
| 23 | * @category CreateDiscountFree |
| 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 CreateDiscountFree extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'EDD'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'create_discount_edd_free'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register action. |
| 50 | * |
| 51 | * @param array $actions action data. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'Create Discount (Free)', '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|bool |
| 72 | * @throws Exception Exception. |
| 73 | */ |
| 74 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 75 | if ( ! function_exists( 'edd_add_discount' ) || ! function_exists( 'edd_get_discount' ) ) { |
| 76 | return [ |
| 77 | 'status' => 'error', |
| 78 | 'message' => 'EDD plugin is not active.', |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | if ( empty( $selected_options['name'] ) || empty( $selected_options['code'] ) || empty( $selected_options['amount'] ) ) { |
| 83 | throw new Exception( 'Missing required parameters (name, code, amount)' ); |
| 84 | } |
| 85 | |
| 86 | $args = [ |
| 87 | 'name' => sanitize_text_field( $selected_options['name'] ), |
| 88 | 'code' => sanitize_text_field( $selected_options['code'] ), |
| 89 | 'amount' => floatval( $selected_options['amount'] ), |
| 90 | 'amount_type' => sanitize_text_field( $selected_options['type'] ), |
| 91 | 'status' => 'active', |
| 92 | 'product_reqs' => isset( $selected_options['products'] ) && is_array( $selected_options['products'] ) |
| 93 | ? array_map( |
| 94 | function( $product ) { |
| 95 | return sanitize_text_field( $product['value'] ); |
| 96 | }, |
| 97 | $selected_options['products'] |
| 98 | ) |
| 99 | : [], |
| 100 | |
| 101 | 'excluded_products' => isset( $selected_options['excluded_downloads'] ) && is_array( $selected_options['excluded_downloads'] ) |
| 102 | ? array_map( |
| 103 | function( $product ) { |
| 104 | return sanitize_text_field( $product['value'] ); |
| 105 | }, |
| 106 | $selected_options['excluded_downloads'] |
| 107 | ) |
| 108 | : [], |
| 109 | |
| 110 | 'scope' => sanitize_text_field( $selected_options['scope'] ), |
| 111 | 'product_condition' => sanitize_text_field( $selected_options['product_condition'] ), |
| 112 | 'start_date' => isset( $selected_options['start_date'] ) ? sanitize_text_field( $selected_options['start_date'] ) : '', |
| 113 | 'categories' => isset( $selected_options['categories'] ) && is_array( $selected_options['categories'] ) |
| 114 | ? array_map( |
| 115 | function( $category ) { |
| 116 | return intval( $category['value'] ); |
| 117 | }, |
| 118 | $selected_options['categories'] |
| 119 | ) |
| 120 | : [], |
| 121 | 'end_date' => isset( $selected_options['expiration_date'] ) ? sanitize_text_field( $selected_options['expiration_date'] ) : '', |
| 122 | 'min_charge_amount' => isset( $selected_options['min_charge_amount'] ) ? floatval( $selected_options['min_charge_amount'] ) : 0, |
| 123 | 'max_uses' => isset( $selected_options['max_uses'] ) ? intval( $selected_options['max_uses'] ) : 0, |
| 124 | 'once_per_customer' => isset( $selected_options['once_per_customer'] ) ? $selected_options['once_per_customer'] : 0, |
| 125 | ]; |
| 126 | |
| 127 | $discount_id = edd_add_discount( $args ); |
| 128 | $discount = edd_get_discount( $discount_id ); |
| 129 | |
| 130 | if ( ! $discount_id ) { |
| 131 | $error_message = 'Failed to create discount. Please check your parameters and try again.'; |
| 132 | return [ |
| 133 | 'status' => 'error', |
| 134 | 'message' => $error_message, |
| 135 | ]; |
| 136 | } |
| 137 | |
| 138 | $discount_data = [ |
| 139 | 'id' => $discount->get_id(), |
| 140 | 'name' => $discount->get_name(), |
| 141 | 'code' => $discount->get_code(), |
| 142 | 'status' => $discount->get_status(), |
| 143 | 'amount_type' => $discount->get_amount_type(), |
| 144 | 'amount' => $discount->get_amount(), |
| 145 | 'product_reqs' => $discount->get_product_reqs(), |
| 146 | 'scope' => $discount->get_scope(), |
| 147 | 'excluded_products' => $discount->get_excluded_products(), |
| 148 | 'product_condition' => $discount->get_product_condition(), |
| 149 | 'start_date' => $discount->get_start_date(), |
| 150 | 'end_date' => $discount->get_end_date(), |
| 151 | 'use_count' => $discount->get_use_count(), |
| 152 | 'max_uses' => $discount->get_max_uses(), |
| 153 | 'min_charge_amount' => $discount->get_min_charge_amount(), |
| 154 | 'once_per_customer' => $discount->get_once_per_customer(), |
| 155 | 'categories' => $discount->get_categories(), |
| 156 | 'term_condition' => $discount->get_term_condition(), |
| 157 | ]; |
| 158 | |
| 159 | return [ |
| 160 | 'discount_id' => $discount_id, |
| 161 | 'discount' => $discount_data, |
| 162 | ]; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | } |
| 167 | |
| 168 | CreateDiscountFree::get_instance(); |
| 169 |