| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Frontend; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Product class |
| 9 |
* control single product page |
| 10 |
*/ |
| 11 |
class Product { |
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize the class |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'change_single_add_to_cart_text' ) ); |
| 18 |
add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_single_add_to_cart_text' ) ); |
| 19 |
add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 ); |
| 20 |
add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_input_args' ), 10, 2 ); |
| 21 |
add_filter( 'woocommerce_add_to_cart', array( $this, 'update_cart_quantity' ), 10, 3 ); |
| 22 |
add_filter( 'woocommerce_store_api_product_quantity_minimum', array( $this, 'update_quantity_min_max' ), 10, 2 ); |
| 23 |
add_filter( 'woocommerce_store_api_product_quantity_maximum', array( $this, 'update_quantity_min_max' ), 10, 2 ); |
| 24 |
add_action( 'woocommerce_after_cart_item_quantity_update', array( $this, 'validate_quantity_on_manual_renewal' ), 10, 2 ); |
| 25 |
add_filter( 'woocommerce_is_purchasable', array( $this, 'check_if_purchasable' ), 20, 2 ); |
| 26 |
add_action( 'woocommerce_single_product_summary', array( $this, 'text_if_active' ) ); |
| 27 |
add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'remove_button_active_products' ), 10, 2 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Remove button if product already subscribed. |
| 32 |
* |
| 33 |
* @param mixed $button Button. |
| 34 |
* @param \WC_Product $product Product. |
| 35 |
* |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public function remove_button_active_products( $button, $product ) { |
| 39 |
if ( ! $product->is_type( 'simple' ) ) { |
| 40 |
return $button; |
| 41 |
} |
| 42 |
$enabled = $product->get_meta( '_subscrpt_enabled' ); |
| 43 |
if ( $enabled ) { |
| 44 |
$limit = $product->get_meta( '_subscrpt_limit' ); |
| 45 |
if ( 'one' === $limit ) { |
| 46 |
$unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) ); |
| 47 |
if ( $unexpired ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
} elseif ( 'only_one' === $limit && ! Helper::check_trial( $product->get_id() ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $button; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Display notice if already purchased. |
| 60 |
*/ |
| 61 |
public function text_if_active() { |
| 62 |
global $product; |
| 63 |
if ( ! $product->is_type( 'simple' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
$enabled = $product->get_meta( '_subscrpt_enabled' ); |
| 67 |
if ( $enabled ) { |
| 68 |
$limit = $product->get_meta( '_subscrpt_limit' ); |
| 69 |
if ( 'unlimited' === $limit ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
if ( 'one' === $limit ) { |
| 73 |
$unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) ); |
| 74 |
if ( ! $unexpired ) { |
| 75 |
return false; |
| 76 |
} else { |
| 77 |
echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>'; |
| 78 |
} |
| 79 |
} |
| 80 |
if ( 'only_one' === $limit ) { |
| 81 |
if ( ! Helper::check_trial( $product->get_id() ) ) { |
| 82 |
echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>'; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if product pruchasable. |
| 90 |
* |
| 91 |
* @param boolean $is_purchasable True\False. |
| 92 |
* @param \WC_Product $product Product. |
| 93 |
* |
| 94 |
* @return boolean |
| 95 |
*/ |
| 96 |
public function check_if_purchasable( $is_purchasable, $product ) { |
| 97 |
$enabled = $product->get_meta( '_subscrpt_enabled' ); |
| 98 |
if ( $enabled ) { |
| 99 |
$limit = $product->get_meta( '_subscrpt_limit' ); |
| 100 |
if ( 'unlimited' === $limit ) { |
| 101 |
return true; |
| 102 |
} elseif ( 'only_one' === $limit ) { |
| 103 |
return Helper::check_trial( $product->get_id() ); |
| 104 |
} elseif ( 'one' === $limit ) { |
| 105 |
return ! Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) ); |
| 106 |
} |
| 107 |
} |
| 108 |
return $is_purchasable; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Validate quantity after add to cart cart on manual renewal process. |
| 113 |
* |
| 114 |
* @param string $cart_item_key Cart Item Key. |
| 115 |
* @param int $quantity Quantity. |
| 116 |
* |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
public function validate_quantity_on_manual_renewal( $cart_item_key, $quantity ) { |
| 120 |
$cart_item = WC()->cart->get_cart_item( $cart_item_key ); |
| 121 |
$expired = Helper::subscription_exists( $cart_item['data']->get_id(), 'expired' ); |
| 122 |
if ( $expired ) { |
| 123 |
$order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true ); |
| 124 |
$item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true ); |
| 125 |
if ( $item_quantity !== $quantity ) { |
| 126 |
WC()->cart->set_quantity( $cart_item_key, $item_quantity ); |
| 127 |
wc_add_notice( 'You can only add ' . $item_quantity . ' ' . ( $item_quantity > 1 ? 'items' : 'item' ) . ' on cart!', 'error' ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Update quantity min max for renewal process. |
| 134 |
* |
| 135 |
* @param int $value Value. |
| 136 |
* @param \WC_Product $product Product Object. |
| 137 |
* |
| 138 |
* @return int |
| 139 |
*/ |
| 140 |
public function update_quantity_min_max( $value, $product ) { |
| 141 |
$expired = Helper::subscription_exists( $product->get_id(), 'expired' ); |
| 142 |
if ( $expired ) { |
| 143 |
$order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true ); |
| 144 |
$item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true ); |
| 145 |
|
| 146 |
return $item_quantity; |
| 147 |
} |
| 148 |
return $value; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Update cart quantity on manual renewal process. |
| 153 |
* |
| 154 |
* @param string $cart_item_key Cart item key. |
| 155 |
* @param int $product_id Product Id. |
| 156 |
* @param int $quantity Quantity. |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function update_cart_quantity( $cart_item_key, $product_id, $quantity ) { |
| 161 |
$expired = Helper::subscription_exists( $product_id, 'expired' ); |
| 162 |
if ( $expired ) { |
| 163 |
$order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true ); |
| 164 |
$item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true ); |
| 165 |
if ( $item_quantity !== $quantity ) { |
| 166 |
WC()->cart->set_quantity( $cart_item_key, $item_quantity ); |
| 167 |
wc_add_notice( 'You can only add ' . $item_quantity . ' ' . ( $item_quantity > 1 ? 'items' : 'item' ) . ' on cart!', 'error' ); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Update quantity input args. |
| 174 |
* |
| 175 |
* @param array $args args. |
| 176 |
* @param \WC_Product $product Product object. |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function update_input_args( $args, $product ) { |
| 181 |
$expired = Helper::subscription_exists( $product->get_id(), 'expired' ); |
| 182 |
if ( $expired ) { |
| 183 |
$order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true ); |
| 184 |
$item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true ); |
| 185 |
$args['input_value'] = $item_quantity; |
| 186 |
$args['min_value'] = $item_quantity; |
| 187 |
$args['max_value'] = $item_quantity; |
| 188 |
$args['step'] = 1; |
| 189 |
} |
| 190 |
|
| 191 |
return $args; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Change single product add-to-cart button text. |
| 196 |
* |
| 197 |
* @param String $text Add-to-cart button Text. |
| 198 |
*/ |
| 199 |
public function change_single_add_to_cart_text( $text ) { |
| 200 |
global $product; |
| 201 |
if ( ! $product || $product->is_type( 'variable' ) || '' === $product->get_price() ) { |
| 202 |
return $text; |
| 203 |
} |
| 204 |
$cart_btn_label = $product->get_meta( '_subscrpt_cart_btn_label' ); |
| 205 |
$expired = Helper::subscription_exists( $product->get_id(), 'expired' ); |
| 206 |
if ( $expired ) { |
| 207 |
$text = __( 'Renew', 'sdevs_subscrpt' ); |
| 208 |
} elseif ( $product->get_meta( '_subscrpt_enabled' ) && $cart_btn_label ) { |
| 209 |
$text = $cart_btn_label; |
| 210 |
} |
| 211 |
|
| 212 |
return $text; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Add trial, signup fee etc. with product price. |
| 217 |
* |
| 218 |
* @param mixed $price Price. |
| 219 |
* @param \WC_Product $product Product. |
| 220 |
* |
| 221 |
* @return mixed |
| 222 |
*/ |
| 223 |
public function change_price_html( $price, $product ) { |
| 224 |
if ( ! $product->is_type( 'simple' ) || '' === $price ) { |
| 225 |
return $price; |
| 226 |
} |
| 227 |
|
| 228 |
$enabled = $product->get_meta( '_subscrpt_enabled' ); |
| 229 |
if ( $enabled ) : |
| 230 |
$timing_option = $product->get_meta( '_subscrpt_timing_option' ); |
| 231 |
$type = Helper::get_typos( 1, $timing_option ); |
| 232 |
$meta_trial_time = $product->get_meta( '_subscrpt_trial_timing_per' ); |
| 233 |
$has_trial = Helper::check_trial( $product->get_id() ); |
| 234 |
$trial = null; |
| 235 |
if ( ! empty( $meta_trial_time ) && $meta_trial_time > 0 && $has_trial ) { |
| 236 |
$trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . Helper::get_typos( $meta_trial_time, $product->get_meta( '_subscrpt_trial_timing_option' ) ) . ' free trial!</small>'; |
| 237 |
} |
| 238 |
|
| 239 |
return apply_filters( 'subscrpt_simple_price_html', ( $price . ' / ' . $type . $trial ), $product, $price, $timing_option, $trial ); |
| 240 |
else : |
| 241 |
return $price; |
| 242 |
endif; |
| 243 |
} |
| 244 |
} |
| 245 |
|