| 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 is_enabled(): bool { |
| 118 |
return $this->product->get_meta( '_subscrpt_enabled' ); |
| 119 |
} |
| 120 |
|
| 121 |
public function get_trial_timing_per(): int { |
| 122 |
return (int) ( $this->product->get_meta( '_subscrpt_trial_timing_per' ) ?? 1 ); |
| 123 |
} |
| 124 |
|
| 125 |
public function get_trial_timing_option() { |
| 126 |
return $this->product->get_meta( '_subscrpt_trial_timing_option' ); |
| 127 |
} |
| 128 |
|
| 129 |
public function get_signup_fee(): int { |
| 130 |
return 0; |
| 131 |
} |
| 132 |
} |
| 133 |
|