Automattic\WooCommerce\Blocks\BlockTypes
AddToCartForm::has_all_attributes_set
Check if a variation product has all attributes set. Returns true if the product is not variation, or if all variation attributes have defined values.
Method of the class: AddToCartForm{}
No Hooks.
Returns
true|false. True if all attributes are set, false otherwise.
Usage
// private - for code of main (parent) class only $result = $this->has_all_attributes_set( $product );
- $product(WC_Product) (required)
- The product to check.
AddToCartForm::has_all_attributes_set() AddToCartForm::has all attributes set code WC 10.7.0
private function has_all_attributes_set( $product ) {
// If it's not a variation product, return true.
if ( ! $product->is_type( ProductType::VARIATION ) ) {
return true;
}
// Get all variation attributes.
$variation_attributes = $product->get_variation_attributes();
// If there are no variation attributes, return true.
if ( empty( $variation_attributes ) ) {
return true;
}
// Check if any attribute has an empty value (marked as 'any').
if ( in_array( '', array_values( $variation_attributes ), true ) ) {
return false;
}
return true;
}