ulgm_license_default_attributes
Filters default license attributes for a product, allowing modification before they are applied.
add_filter( 'ulgm_license_default_attributes', $callback, 10, 2 );
Description
Filters default attributes for a WooCommerce License product before creation. Developers can modify product data like name, status, price, and more. Use this to dynamically set license product details based on custom logic or external data. Ensure the modified `$args` or `$product` object remains valid.
Usage
add_filter( 'ulgm_license_default_attributes', 'your_function_name', 10, 2 );
Parameters
-
$args(mixed) - This parameter contains an array of arguments used to create a new license product.
-
$product(mixed) - This parameter is an array containing default attributes for the license product.
Return Value
The filtered value.
Examples
/**
* Example of using the ulgm_license_default_attributes filter hook.
*
* This filter allows modification of the default attributes for a license product.
* For instance, we might want to pre-set a specific license key format or
* a default expiration date.
*/
add_filter( 'ulgm_license_default_attributes', function( $default_attributes, $product ) {
// Example: Add a default attribute for "License Type" and set it to "Single Use".
$default_attributes['license_type'] = array(
'name' => __( 'License Type', 'your-text-domain' ),
'value' => 'Single Use',
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
);
// Example: If the product is a subscription, set a default recurring fee.
// This is a hypothetical check, as the product object might not directly expose subscription status this way.
// You would likely need to check WooCommerce subscription plugins or custom meta.
if ( method_exists( $product, 'is_type' ) && $product->is_type( 'subscription' ) ) {
$default_attributes['recurring_fee'] = array(
'name' => __( 'Recurring Fee', 'your-text-domain' ),
'value' => '9.99',
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
);
}
// Important: Always return the modified or original attributes array.
return $default_attributes;
}, 10, 2 ); // 10 is the priority, 2 is the number of accepted arguments.
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/woocommerce/woocommerce-buy-courses.php:355
public static function create_license_product( $args ) {
// Get an empty instance of the product object (defining it's type)
$product = new WC_Product_License();
if ( ! $product ) {
return false;
}
// Product name (Title) and slug
$product->set_name( $args['post_title'] ); // Name (title).
// Status ('publish', 'pending', 'draft' or 'trash')
$product->set_status( isset( $args['post_status'] ) ? $args['post_status'] : 'publish' );
// Visibility ('hidden', 'visible', 'search' or 'catalog')
$product->set_catalog_visibility( 'hidden' );
// Featured (boolean)
$product->set_featured( isset( $args['featured'] ) ? $args['featured'] : false );
// Virtual (boolean)
$product->set_virtual( apply_filters( 'ulgm_license_is_virtual', true, $product ) );
// Downloadable (boolean)
$product->set_downloadable( apply_filters( 'ulgm_license_is_downloadable', false, $product ) );
// Sold Individually
$product->set_sold_individually( apply_filters( 'ulgm_license_is_sold_individually', false, $product ) );
// Reviews
$product->set_reviews_allowed( apply_filters( 'ulgm_license_is_reviews_allowed', false, $product ) );
// Taxes
if ( get_option( 'woocommerce_calc_taxes' ) === 'yes' ) {
$product->set_tax_status( isset( $args['tax_status'] ) ? $args['tax_status'] : 'taxable' );
$product->set_tax_class( isset( $args['tax_class'] ) ? $args['tax_class'] : '' );
}
// Attributes et default attributes
if ( isset( $args['attributes'] ) ) {
$product->set_attributes( self::wc_prepare_product_attributes( apply_filters( 'ulgm_license_attributes', $args['attributes'], $product ) ) );
}
if ( isset( $args['default_attributes'] ) ) {
$product->set_default_attributes( apply_filters( 'ulgm_license_default_attributes', $args['default_attributes'], $product ) );
} // Needs a special formatting
// Product categories and Tags
if ( isset( $args['category_ids'] ) ) {
$product->set_category_ids( apply_filters( 'ulgm_license_category_ids', array( $args['category_ids'] ), $product ) );
}
if ( isset( $args['tag_ids'] ) ) {
$product->set_tag_ids( apply_filters( 'ulgm_license_tag_ids', array( $args['tag_ids'] ), $product ) );
}
if ( 'yes' === get_option( 'woocommerce_calc_taxes' ) ) {
$tax_status = get_option( 'ulgm_group_license_tax_status', '' );
$tax_class = get_option( 'ulgm_group_license_tax_class', '' );
if ( ! empty( $tax_class ) ) {
$product->set_tax_class( $tax_class );
}
if ( ! empty( $tax_status ) ) {
$product->set_tax_status( $tax_status );
}
}
## --- SAVE PRODUCT --- ##
$product->save();
//Set price
$get_price = SharedFunctions::get_custom_product_price( $product, true );
$product->set_price( $get_price );
$product->set_regular_price( $get_price );
return $product;
}