| 1 |
<?php |
| 2 |
use SureCart\Models\Coupon; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
class ESHB_Coupon { |
| 5 |
|
| 6 |
public function __construct() { |
| 7 |
// Initialize the save coupon action |
| 8 |
add_action( 'save_post_eshb_coupon', [$this, 'save_coupon'], 10, 3 ); |
| 9 |
} |
| 10 |
|
| 11 |
public function create_coupon($post_id) { |
| 12 |
$settings = get_option('eshb_settings', []); |
| 13 |
$booking_type = $settings['booking-type'] ?? 'woocommerce'; |
| 14 |
$wc_coupon_id = get_post_meta($post_id, 'eshb_coupon_wc_id', true); |
| 15 |
|
| 16 |
// Get coupon metabox data |
| 17 |
$coupon = get_post_meta($post_id, 'eshb_coupon_metaboxes', true); |
| 18 |
if (empty($coupon)) return; |
| 19 |
|
| 20 |
$accomodation_ids = $coupon['accomodation-ids'] ?? []; |
| 21 |
$product_ids = []; |
| 22 |
|
| 23 |
foreach ($accomodation_ids as $accomodation_id) { |
| 24 |
$accomodation_id = (int) $accomodation_id; |
| 25 |
$thumbnail_id = get_post_thumbnail_id($accomodation_id); |
| 26 |
|
| 27 |
if ($booking_type === 'woocommerce' && class_exists('WC_Product')) { |
| 28 |
$product_ids[] = ESHB_Helper::get_or_create_woocommerce_product($accomodation_id, $thumbnail_id); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
$product_ids = apply_filters( 'eshb_coupon_product_ids', $product_ids, $accomodation_ids); |
| 33 |
|
| 34 |
// Prepare coupon data |
| 35 |
$title = get_the_title($post_id); |
| 36 |
$product_ids_string = implode(',', $product_ids); |
| 37 |
$coupon_code = $coupon['coupon-code'] ?? ''; |
| 38 |
$coupon_amount = $coupon['coupon-amount'] ?? ''; |
| 39 |
$discount_type = $coupon['discount-type'] ?? 'percent'; |
| 40 |
$expiry_date = $coupon['expiry-date'] ?? ''; |
| 41 |
$usage_limit = $coupon['usage-limit'] ?? ''; |
| 42 |
$usage_limit_per_user = $coupon['usage-limit-per-user'] ?? '1'; |
| 43 |
$free_shipping = 'no'; |
| 44 |
|
| 45 |
// Create or update the WooCommerce coupon |
| 46 |
do_action('eshb_before_create_coupon', $post_id, $coupon, $product_ids); |
| 47 |
|
| 48 |
if ($booking_type === 'woocommerce' && class_exists( 'WooCommerce' )) { |
| 49 |
$coupon_id = $this->create_woocommerce_coupon( |
| 50 |
$title, $coupon_code, $coupon_amount, $discount_type, |
| 51 |
$usage_limit, $usage_limit_per_user, $expiry_date, |
| 52 |
$free_shipping, $product_ids_string, $wc_coupon_id |
| 53 |
); |
| 54 |
if(!empty($coupon_id)){ |
| 55 |
update_post_meta($post_id, 'eshb_coupon_wc_id', $coupon_id); |
| 56 |
} |
| 57 |
|
| 58 |
if(!empty($product_ids_string)){ |
| 59 |
update_post_meta($post_id, 'eshb_coupon_wc_product_ids', $product_ids_string); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private function get_or_create_woocommerce_product($accomodation_id, $thumbnail_id) { |
| 65 |
$product_id = get_post_meta($accomodation_id, '_woocommerce_product_id', true); |
| 66 |
|
| 67 |
if (empty($product_id)) { |
| 68 |
$product = new WC_Product(); |
| 69 |
$product->set_name(get_the_title($accomodation_id)); |
| 70 |
$product->set_price(1); |
| 71 |
$product->set_regular_price(1); |
| 72 |
$product->set_virtual(true); |
| 73 |
$product->set_image_id($thumbnail_id); |
| 74 |
$product->save(); |
| 75 |
|
| 76 |
$product_id = $product->get_id(); |
| 77 |
|
| 78 |
update_post_meta($accomodation_id, '_woocommerce_product_id', $product_id); |
| 79 |
update_post_meta($accomodation_id, '_regular_price', 1); |
| 80 |
} |
| 81 |
|
| 82 |
return $product_id; |
| 83 |
} |
| 84 |
|
| 85 |
public function create_woocommerce_coupon($title, $coupon_code, $amount, $discount_type = 'percent', $usage_limit = '', $usage_limit_per_user = '1', $expiry_date = '', $free_shipping = 'no', $product_id = '', $wc_coupon_id = '') { |
| 86 |
|
| 87 |
// Check if WooCommerce is active |
| 88 |
if (!class_exists('WooCommerce')) { |
| 89 |
return 'WooCommerce is not active.'; |
| 90 |
} |
| 91 |
|
| 92 |
// Check if the coupon already exists |
| 93 |
|
| 94 |
if (!empty($wc_coupon_id)) { |
| 95 |
// Update existing coupon |
| 96 |
$coupon_id = $wc_coupon_id; |
| 97 |
wp_update_post(array( |
| 98 |
'ID' => $coupon_id, |
| 99 |
'post_title' => $coupon_code, |
| 100 |
'post_excerpt' => $title, |
| 101 |
)); |
| 102 |
} else { |
| 103 |
// Create a new coupon |
| 104 |
$coupon = array( |
| 105 |
'post_title' => $coupon_code, |
| 106 |
'post_excerpt' => $title, |
| 107 |
'post_status' => 'publish', |
| 108 |
'post_author' => 1, |
| 109 |
'post_type' => 'shop_coupon', |
| 110 |
); |
| 111 |
$coupon_id = wp_insert_post($coupon); |
| 112 |
} |
| 113 |
|
| 114 |
// Add meta data for the coupon |
| 115 |
if(!empty($product_id) && is_array($product_id)){ |
| 116 |
$product_id = implode(',', $product_id); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
// Add meta data for the coupon |
| 121 |
update_post_meta($coupon_id, 'discount_type', $discount_type); |
| 122 |
update_post_meta($coupon_id, 'coupon_amount', $amount); |
| 123 |
update_post_meta($coupon_id, 'individual_use', 'yes'); // Allow individual use only |
| 124 |
update_post_meta($coupon_id, 'product_ids', $product_id); // Products this coupon applies to, empty for all |
| 125 |
update_post_meta($coupon_id, 'exclude_product_ids', ''); // Excluded products |
| 126 |
update_post_meta($coupon_id, 'usage_limit', $usage_limit); // Limit usage to X items |
| 127 |
update_post_meta($coupon_id, 'usage_limit_per_user', $usage_limit_per_user); // Limit usage to 1 per user |
| 128 |
update_post_meta($coupon_id, 'expiry_date', $expiry_date); // Expiration date (Y-m-d format) |
| 129 |
update_post_meta($coupon_id, 'free_shipping', $free_shipping); // Enable free shipping if applicable |
| 130 |
|
| 131 |
return $coupon_id; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
public function delete_coupon($post_id) { |
| 136 |
// Check if the post is being deleted or if it's the wrong post type |
| 137 |
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || get_post_type($post_id) != 'eshb_coupon') { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
$eshb_settings = get_option('eshb_settings', []); |
| 142 |
$booking_type = isset($eshb_settings['booking-type']) && !empty($eshb_settings['booking-type']) ? $eshb_settings['booking-type'] : 'woocommerce'; |
| 143 |
|
| 144 |
do_action( 'eshb_before_delete_coupon', $post_id, $booking_type ); |
| 145 |
|
| 146 |
if($booking_type == 'woocommerce'){ |
| 147 |
|
| 148 |
// Get the WooCommerce coupon ID from the custom post type |
| 149 |
$coupon_id = get_post_meta( $post_id, 'eshb_coupon_wc_id', true ); |
| 150 |
wp_delete_post($coupon_id); |
| 151 |
|
| 152 |
// delete wc coupon id |
| 153 |
delete_post_meta($post_id, 'eshb_coupon_wc_id'); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
public function save_coupon($post_id, $post, $update) { |
| 160 |
// Check if the post is being updated |
| 161 |
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || $post->post_type != 'eshb_coupon') { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
// Check if the post is being deleted |
| 166 |
if(get_post_status($post_id) != 'publish'){ |
| 167 |
$this->delete_coupon($post_id); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$this->create_coupon($post_id); |
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
// Initialize the class |
| 176 |
$eshb_coupon = new ESHB_Coupon(); |