| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Utils; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Abstract Product class that wraps a WC_Product object. |
| 9 |
* |
| 10 |
* @method int get_id() |
| 11 |
* @method string get_name() |
| 12 |
* @method string get_type() |
| 13 |
* @method string get_slug() |
| 14 |
* @method string get_date_created() |
| 15 |
* @method string get_date_modified() |
| 16 |
* @method string get_status() |
| 17 |
* @method string get_featured() |
| 18 |
* @method string get_catalog_visibility() |
| 19 |
* @method string get_description() |
| 20 |
* @method string get_short_description() |
| 21 |
* @method string get_sku() |
| 22 |
* @method float get_price() |
| 23 |
* @method float get_regular_price() |
| 24 |
* @method float get_sale_price() |
| 25 |
* @method string get_date_on_sale_from() |
| 26 |
* @method string get_date_on_sale_to() |
| 27 |
* @method float get_total_sales() |
| 28 |
* @method bool get_tax_status() |
| 29 |
* @method bool get_tax_class() |
| 30 |
* @method bool get_manage_stock() |
| 31 |
* @method int get_stock_quantity() |
| 32 |
* @method string get_stock_status() |
| 33 |
* @method string get_backorders() |
| 34 |
* @method bool get_sold_individually() |
| 35 |
* @method float get_weight() |
| 36 |
* @method float get_length() |
| 37 |
* @method float get_width() |
| 38 |
* @method float get_height() |
| 39 |
* @method array get_dimensions() |
| 40 |
* @method bool get_upsell_ids() |
| 41 |
* @method bool get_cross_sell_ids() |
| 42 |
* @method string get_parent_id() |
| 43 |
* @method string get_reviews_allowed() |
| 44 |
* @method string get_purchase_note() |
| 45 |
* @method string get_attribute() |
| 46 |
* @method array get_attributes() |
| 47 |
* @method array get_default_attributes() |
| 48 |
* @method string get_menu_order() |
| 49 |
* @method string get_category_ids() |
| 50 |
* @method string get_tag_ids() |
| 51 |
* @method string get_virtual() |
| 52 |
* @method string get_gallery_image_ids() |
| 53 |
* @method string get_shipping_class_id() |
| 54 |
* @method string get_downloads() |
| 55 |
* @method int get_download_expiry() |
| 56 |
* @method int get_downloadable() |
| 57 |
* @method int get_download_limit() |
| 58 |
* @method string get_image_id() |
| 59 |
* @method string get_rating_counts() |
| 60 |
* @method string get_average_rating() |
| 61 |
* @method string get_review_count() |
| 62 |
* @method bool is_virtual() |
| 63 |
*/ |
| 64 |
abstract class Product { |
| 65 |
|
| 66 |
protected \WC_Product $product; |
| 67 |
|
| 68 |
public function __construct( \WC_Product $product ) { |
| 69 |
$this->product = $product; |
| 70 |
} |
| 71 |
|
| 72 |
public function get_trial(): ?string { |
| 73 |
if ( $this->has_trial() ) { |
| 74 |
$product_trial_per = $this->get_trial_timing_per(); |
| 75 |
|
| 76 |
return $product_trial_per . ' ' . Helper::get_typos( $product_trial_per, $this->get_trial_timing_option() ); |
| 77 |
} |
| 78 |
|
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
public function has_trial(): bool { |
| 83 |
$trial_timing_per = $this->get_trial_timing_per(); |
| 84 |
$product_id = $this->product->get_id(); |
| 85 |
|
| 86 |
return ( $trial_timing_per > 0 ) && Helper::check_trial( $product_id ); |
| 87 |
} |
| 88 |
|
| 89 |
public function get_button_label() { |
| 90 |
return $this->product->get_meta( '_subscrpt_cart_btn_label' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Magic method to call methods from the WC_Product object. |
| 95 |
* |
| 96 |
* @param string $name The method name. |
| 97 |
* @param array $arguments The method arguments. |
| 98 |
* @return mixed The result of the called method. |
| 99 |
* @uses \WC_Product::__call() |
| 100 |
*/ |
| 101 |
public function __call( $name, $arguments ) { |
| 102 |
return $this->product->{$name}( ...$arguments ); |
| 103 |
} |
| 104 |
|
| 105 |
public function get_timing_per(): int { |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
public function get_timing_option(): string { |
| 110 |
return Helper::get_typos( $this->get_timing_per(), $this->product->get_meta( '_subscrpt_timing_option' ) ); |
| 111 |
} |
| 112 |
|
| 113 |
public function get_limit() { |
| 114 |
return $this->product->get_meta( '_subscrpt_limit' ); |
| 115 |
} |
| 116 |
|
| 117 |
public function get_renewal_limit() { |
| 118 |
return $this->product->get_meta( '_subscrpt_max_no_payment' ); |
| 119 |
} |
| 120 |
|
| 121 |
public function get_max_no_payment() { |
| 122 |
return $this->product->get_meta( '_subscrpt_max_no_payment' ); |
| 123 |
} |
| 124 |
|
| 125 |
public function is_enabled(): bool { |
| 126 |
if ( empty( $this->product->get_meta( '_subscrpt_enabled' ) ) ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
// Once plan-connected, a product is a subscription only while it still has |
| 131 |
// a plan — it never falls back to the legacy terms after a detach. |
| 132 |
if ( 'yes' === $this->product->get_meta( '_subscrpt_plan_connected_before' ) && function_exists( 'subscrpt_product_has_plan' ) ) { |
| 133 |
$parent = $this->product->get_parent_id(); |
| 134 |
$has_plan = $parent |
| 135 |
? subscrpt_product_has_plan( $parent, $this->product->get_id() ) |
| 136 |
: subscrpt_product_has_plan( $this->product->get_id() ); |
| 137 |
if ( ! $has_plan ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
public function get_trial_timing_per(): int { |
| 146 |
return (int) ( $this->product->get_meta( '_subscrpt_trial_timing_per' ) ?? 1 ); |
| 147 |
} |
| 148 |
|
| 149 |
public function get_trial_timing_option() { |
| 150 |
return $this->product->get_meta( '_subscrpt_trial_timing_option' ); |
| 151 |
} |
| 152 |
|
| 153 |
public function get_signup_fee(): float { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get the payment type (recurring or split_payment). |
| 159 |
* |
| 160 |
* @return string Payment type. |
| 161 |
*/ |
| 162 |
public function get_payment_type(): string { |
| 163 |
return $this->product->get_meta( '_subscrpt_payment_type' ) ?: 'recurring'; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check if product uses split payment type. |
| 168 |
* |
| 169 |
* @return bool True if split payment, false otherwise. |
| 170 |
*/ |
| 171 |
public function is_split_payment(): bool { |
| 172 |
return 'split_payment' === $this->get_payment_type(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get access ends timing for split payments. |
| 177 |
* |
| 178 |
* @return string Access ends timing option. |
| 179 |
*/ |
| 180 |
public function get_access_ends_timing(): string { |
| 181 |
return $this->product->get_meta( '_subscrpt_access_ends_timing' ) ?: 'after_full_duration'; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get custom access duration time. |
| 186 |
* |
| 187 |
* @return int Custom access duration time. |
| 188 |
*/ |
| 189 |
public function get_custom_access_duration_time(): int { |
| 190 |
return (int) ( $this->product->get_meta( '_subscrpt_custom_access_duration_time' ) ?: 1 ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get custom access duration type. |
| 195 |
* |
| 196 |
* @return string Custom access duration type (days, weeks, months, years). |
| 197 |
*/ |
| 198 |
public function get_custom_access_duration_type(): string { |
| 199 |
return $this->product->get_meta( '_subscrpt_custom_access_duration_type' ) ?: 'months'; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Check if product has custom access duration. |
| 204 |
* |
| 205 |
* @return bool True if custom duration, false otherwise. |
| 206 |
*/ |
| 207 |
public function has_custom_access_duration(): bool { |
| 208 |
return 'custom_duration' === $this->get_access_ends_timing(); |
| 209 |
} |
| 210 |
} |
| 211 |
|