| @@ -1,9 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Storefront product handling for subscriptions. | |
| 4 | + * | |
| 5 | + * @package SpringDevs\Subscription | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace SpringDevs\Subscription\Frontend; |
| 4 | 9 | |
| 5 | 10 | use SpringDevs\Subscription\Illuminate\Helper; |
| 11 | +use SpringDevs\Subscription\Illuminate\Subscription\Subscription; | |
| 6 | 12 | |
| 7 | 13 | // HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS). |
| 8 | 14 | // All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, wc_get_order_item_meta, etc.). |
| 9 | 15 | // All direct post meta access is for subscription data only, not WooCommerce order data. |
| @@ -27,9 +33,15 @@ | ||
| 27 | 33 | ), |
| 28 | 34 | 10, |
| 29 | 35 | 2 |
| 30 | 36 | ); |
| 31 | - add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_single_add_to_cart_text' ), 10, 2 ); | |
| 37 | + add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_loop_add_to_cart_text' ), 10, 2 ); | |
| 38 | + // Plan-tied simple products behave like a variable product on the shop / | |
| 39 | + // Product Collection loops: a "Select options" link to the product page (so | |
| 40 | + // the customer picks a plan), never a direct/ajax add-to-cart. These native | |
| 41 | + // product-method filters drive both the classic loop and the block button. | |
| 42 | + add_filter( 'woocommerce_product_add_to_cart_url', array( $this, 'plan_loop_add_to_cart_url' ), 10, 2 ); | |
| 43 | + add_filter( 'woocommerce_product_supports', array( $this, 'plan_loop_disable_ajax' ), 10, 3 ); | |
| 32 | 44 | add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 ); |
| 33 | 45 | add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_input_args' ), 10, 2 ); |
| 34 | 46 | add_filter( 'woocommerce_add_to_cart', array( $this, 'update_cart_quantity' ), 10, 3 ); |
| 35 | 47 | add_filter( |
| @@ -72,9 +84,9 @@ | ||
| 72 | 84 | * |
| 73 | 85 | * @return mixed |
| 74 | 86 | */ |
| 75 | 87 | public function remove_button_active_products( $button, $product ) { |
| 76 | - $product = sdevs_get_subscription_product( $product ); | |
| 88 | + $product = Subscription::get_subs_product( $product ); | |
| 77 | 89 | if ( ! $product->is_type( 'simple' ) ) { |
| 78 | 90 | return $button; |
| 79 | 91 | } |
| 80 | 92 | |
| @@ -97,9 +109,9 @@ | ||
| 97 | 109 | * Display notice if already purchased. |
| 98 | 110 | */ |
| 99 | 111 | public function text_if_active() { |
| 100 | 112 | global $product; |
| 101 | - $sdevs_product = sdevs_get_subscription_product( $product ); | |
| 113 | + $sdevs_product = Subscription::get_subs_product( $product ); | |
| 102 | 114 | if ( ! $sdevs_product->is_type( 'simple' ) ) { |
| 103 | 115 | return; |
| 104 | 116 | } |
| 105 | 117 | |
| @@ -108,18 +120,18 @@ | ||
| 108 | 120 | if ( 'unlimited' === $limit ) { |
| 109 | 121 | return; |
| 110 | 122 | } |
| 111 | 123 | if ( 'one' === $limit ) { |
| 112 | - $unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) ); | |
| 124 | + $unexpired = Helper::subscription_exists( $sdevs_product->get_id(), array( 'active', 'pending' ) ); | |
| 113 | 125 | if ( ! $unexpired ) { |
| 114 | 126 | return false; |
| 115 | 127 | } else { |
| 116 | - echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'wp_subscription' ) . '</strong>'; | |
| 128 | + echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'subscription' ) . '</strong>'; | |
| 117 | 129 | } |
| 118 | 130 | } |
| 119 | 131 | if ( 'only_one' === $limit ) { |
| 120 | - if ( ! Helper::check_trial( $product->get_id() ) ) { | |
| 121 | - echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'wp_subscription' ) . '</strong>'; | |
| 132 | + if ( ! Helper::check_trial( $sdevs_product->get_id() ) ) { | |
| 133 | + echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'subscription' ) . '</strong>'; | |
| 122 | 134 | } |
| 123 | 135 | } |
| 124 | 136 | } |
| 125 | 137 | } |
| @@ -132,9 +144,9 @@ | ||
| 132 | 144 | * |
| 133 | 145 | * @return boolean |
| 134 | 146 | */ |
| 135 | 147 | public function check_if_purchasable( $is_purchasable, $product ) { |
| 136 | - $product = sdevs_get_subscription_product( $product ); | |
| 148 | + $product = Subscription::get_subs_product( $product ); | |
| 137 | 149 | if ( $product->is_enabled() ) { |
| 138 | 150 | $limit = $product->get_limit(); |
| 139 | 151 | if ( 'unlimited' === $limit ) { |
| 140 | 152 | return true; |
| @@ -238,9 +250,9 @@ | ||
| 238 | 250 | * @param string $text Add-to-cart button Text. |
| 239 | 251 | * @param \WC_Product $product Product Object. |
| 240 | 252 | */ |
| 241 | 253 | public function change_single_add_to_cart_text( $text, $product ) { |
| 242 | - $product = sdevs_get_subscription_product( $product ); | |
| 254 | + $product = Subscription::get_subs_product( $product ); | |
| 243 | 255 | if ( $product->is_type( 'variable' ) || '' === $product->get_price() ) { |
| 244 | 256 | return $text; |
| 245 | 257 | } |
| 246 | 258 | $cart_btn_label = $product->get_button_label(); |
| @@ -245,9 +257,9 @@ | ||
| 245 | 257 | } |
| 246 | 258 | $cart_btn_label = $product->get_button_label(); |
| 247 | 259 | $expired = Helper::subscription_exists( $product->get_id(), 'expired' ); |
| 248 | 260 | if ( $expired ) { |
| 249 | - $text = __( 'Renew', 'wp_subscription' ); | |
| 261 | + $text = __( 'Renew', 'subscription' ); | |
| 250 | 262 | } elseif ( $product->is_enabled() && $cart_btn_label ) { |
| 251 | 263 | $text = $cart_btn_label; |
| 252 | 264 | } |
| 253 | 265 | |
| @@ -254,8 +266,71 @@ | ||
| 254 | 266 | return $text; |
| 255 | 267 | } |
| 256 | 268 | |
| 257 | 269 | /** |
| 270 | + * Whether this is a plan-tied simple product that should behave like a | |
| 271 | + * variable product on the shop / Product Collection loops. | |
| 272 | + * | |
| 273 | + * @param mixed $product Product object. | |
| 274 | + * | |
| 275 | + * @return bool | |
| 276 | + */ | |
| 277 | + private function is_plan_loop_product( $product ) { | |
| 278 | + return $product instanceof \WC_Product | |
| 279 | + && $product->is_type( 'simple' ) | |
| 280 | + && subscrpt_plan_offered( $product->get_id() ); | |
| 281 | + } | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * Loop / block add-to-cart button text: "Select options" for plan products, | |
| 285 | + * otherwise the classic label logic. | |
| 286 | + * | |
| 287 | + * @param string $text Add-to-cart button text. | |
| 288 | + * @param \WC_Product $product Product object. | |
| 289 | + * | |
| 290 | + * @return string | |
| 291 | + */ | |
| 292 | + public function change_loop_add_to_cart_text( $text, $product ) { | |
| 293 | + if ( $this->is_plan_loop_product( $product ) ) { | |
| 294 | + return __( 'Select options', 'subscription' ); | |
| 295 | + } | |
| 296 | + | |
| 297 | + return $this->change_single_add_to_cart_text( $text, $product ); | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Loop / block add-to-cart URL: the product page for plan products, so the | |
| 302 | + * customer lands on the plan selector instead of a bare add-to-cart. | |
| 303 | + * | |
| 304 | + * @param string $url Add-to-cart URL. | |
| 305 | + * @param \WC_Product $product Product object. | |
| 306 | + * | |
| 307 | + * @return string | |
| 308 | + */ | |
| 309 | + public function plan_loop_add_to_cart_url( $url, $product ) { | |
| 310 | + return $this->is_plan_loop_product( $product ) ? $product->get_permalink() : $url; | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Disable ajax add-to-cart for plan products so their loop / block button | |
| 315 | + * renders as a "Select options" link (navigates to the product page) rather | |
| 316 | + * than an ajax add. | |
| 317 | + * | |
| 318 | + * @param bool $supports Whether the feature is supported. | |
| 319 | + * @param string $feature Feature name. | |
| 320 | + * @param \WC_Product $product Product object. | |
| 321 | + * | |
| 322 | + * @return bool | |
| 323 | + */ | |
| 324 | + public function plan_loop_disable_ajax( $supports, $feature, $product ) { | |
| 325 | + if ( 'ajax_add_to_cart' === $feature && $this->is_plan_loop_product( $product ) ) { | |
| 326 | + return false; | |
| 327 | + } | |
| 328 | + | |
| 329 | + return $supports; | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** | |
| 258 | 333 | * Add trial, signup fee etc. with product price. |
| 259 | 334 | * |
| 260 | 335 | * @param mixed $price Price. |
| 261 | 336 | * @param \WC_Product $product Product. |
| @@ -262,9 +337,9 @@ | ||
| 262 | 337 | * |
| 263 | 338 | * @return mixed |
| 264 | 339 | */ |
| 265 | 340 | public function change_price_html( $price, $product ) { |
| 266 | - $product = sdevs_get_subscription_product( $product ); | |
| 341 | + $product = Subscription::get_subs_product( $product ); | |
| 267 | 342 | if ( ! $product->is_type( 'simple' ) || '' === $price ) { |
| 268 | 343 | return $price; |
| 269 | 344 | } |
| 270 | 345 | |
| @@ -269,16 +344,21 @@ | ||
| 269 | 344 | } |
| 270 | 345 | |
| 271 | 346 | if ( $product->is_enabled() ) : |
| 272 | 347 | $timing_option = $product->get_timing_option(); |
| 273 | - $type = Helper::get_typos( 1, $timing_option ); | |
| 274 | - $trial = null; | |
| 348 | + $type = ucfirst( Helper::get_typos( 1, $timing_option, true ) ); | |
| 349 | + | |
| 350 | + $trial = null; | |
| 275 | 351 | if ( $product->has_trial() ) { |
| 276 | 352 | $meta_trial_time = $product->get_trial_timing_per(); |
| 277 | - $trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . Helper::get_typos( $meta_trial_time, $product->get_trial_timing_option() ) . ' free trial!</small>'; | |
| 353 | + $trial_type = Helper::get_typos( $meta_trial_time, $product->get_trial_timing_option(), true ); | |
| 354 | + $trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . ucfirst( $trial_type ) . ' free trial!</small>'; | |
| 278 | 355 | } |
| 279 | 356 | |
| 280 | - return apply_filters( 'subscrpt_simple_price_html', ( $price . ' / ' . $type . $trial ), $product, $price, $trial ); | |
| 357 | + $timing_html = "<span class='wpsubs-subscription-timing'> / {$type}</span>"; | |
| 358 | + $price_html = $price . $timing_html . $trial; | |
| 359 | + | |
| 360 | + return apply_filters( 'subscrpt_simple_price_html', $price_html, $product, $price, $trial ); | |
| 281 | 361 | else : |
| 282 | 362 | return $price; |
| 283 | 363 | endif; |
| 284 | 364 | } |