| @@ -1,5 +1,10 @@ | ||
| 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; |
| @@ -28,9 +33,15 @@ | ||
| 28 | 33 | ), |
| 29 | 34 | 10, |
| 30 | 35 | 2 |
| 31 | 36 | ); |
| 32 | - 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 ); | |
| 33 | 44 | add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 ); |
| 34 | 45 | add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_input_args' ), 10, 2 ); |
| 35 | 46 | add_filter( 'woocommerce_add_to_cart', array( $this, 'update_cart_quantity' ), 10, 3 ); |
| 36 | 47 | add_filter( |
| @@ -252,8 +263,71 @@ | ||
| 252 | 263 | $text = $cart_btn_label; |
| 253 | 264 | } |
| 254 | 265 | |
| 255 | 266 | return $text; |
| 267 | + } | |
| 268 | + | |
| 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; | |
| 256 | 330 | } |
| 257 | 331 | |
| 258 | 332 | /** |
| 259 | 333 | * Add trial, signup fee etc. with product price. |