Skip to content

Implementing product type constants #4474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.7.0 - xxxx-xx-xx =
* Dev - Makes use of the new product type constants in the whole codebase
* Add - Introduces a new banner to promote the Optimized Checkout feature in the Stripe settings page for versions 9.8 and above
* Add - Introduces a new inbox note to promote the Optimized Checkout feature on version 9.8 and later
* Update - Removes BNPL payment methods (Klarna and Affirm) when other official plugins are active
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Automattic\WooCommerce\Enums\ProductType;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand Down Expand Up @@ -92,7 +95,7 @@ public function ajax_add_to_cart() {
// First empty the cart to prevent wrong calculation.
WC()->cart->empty_cart();

if ( ( 'variable' === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {
if ( ( ProductType::VARIABLE === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very much non-blocking personal preference (which may be worth discussing as a team): I prefer using fully-qualified references inline as much as possible, mostly because it's not always easy to understand what the class is when viewing isolated sections of the code, especially via diffs.

Suggested change
if ( ( ProductType::VARIABLE === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {
if ( ( Automattic\WooCommerce\Enums\ProductType::VARIABLE === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! To add to this, I found a page with a nice discussion about it: https://www.sitepoint.com/community/t/using-namespace-prefix-versus-use-statement/351357/11

$attributes = wc_clean( wp_unslash( $_POST['attributes'] ) );

$data_store = WC_Data_Store::load( 'product' );
Expand Down Expand Up @@ -232,7 +235,7 @@ public function ajax_get_selected_product_data() {
throw new Exception( sprintf( __( 'Product with the ID (%1$s) cannot be found.', 'woocommerce-gateway-stripe' ), $product_id ) );
}

if ( in_array( $product->get_type(), [ 'variable', 'variable-subscription' ], true ) && isset( $_POST['attributes'] ) ) {
if ( in_array( $product->get_type(), [ ProductType::VARIABLE, 'variable-subscription' ], true ) && isset( $_POST['attributes'] ) ) {
$attributes = wc_clean( wp_unslash( $_POST['attributes'] ) );

$data_store = WC_Data_Store::load( 'product' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Automattic\WooCommerce\Enums\ProductType;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand Down Expand Up @@ -225,7 +227,7 @@ public function get_product_data() {
return false;
}

if ( in_array( $product->get_type(), [ 'variable', 'variable-subscription' ], true ) ) {
if ( in_array( $product->get_type(), [ ProductType::VARIABLE, 'variable-subscription' ], true ) ) {
$variation_attributes = $product->get_variation_attributes();
$attributes = [];

Expand Down Expand Up @@ -424,9 +426,9 @@ public function supported_product_types() {
return apply_filters(
'wc_stripe_payment_request_supported_types',
[
'simple',
'variable',
'variation',
ProductType::SIMPLE,
ProductType::VARIABLE,
ProductType::VARIATION,
'subscription',
'variable-subscription',
'subscription_variation',
Expand Down Expand Up @@ -689,7 +691,7 @@ public function should_show_express_checkout_button() {
return false;
}

if ( $is_product && $product && in_array( $product->get_type(), [ 'variable', 'variable-subscription' ], true ) ) {
if ( $is_product && $product && in_array( $product->get_type(), [ ProductType::VARIABLE, 'variable-subscription' ], true ) ) {
$stock_availability = array_column( $product->get_available_variations(), 'is_in_stock' );
// Don't show if all product variations are out-of-stock.
if ( ! in_array( true, $stock_availability, true ) ) {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.7.0 - xxxx-xx-xx =
* Dev - Makes use of the new product type constants in the whole codebase
* Add - Introduces a new banner to promote the Optimized Checkout feature in the Stripe settings page for versions 9.8 and above
* Add - Introduces a new inbox note to promote the Optimized Checkout feature on version 9.8 and later
* Update - Removes BNPL payment methods (Klarna and Affirm) when other official plugins are active
Expand Down
Loading