ulgm_license_attributes
Filters license attributes before they are added to the plugin's core data.
add_filter( 'ulgm_license_attributes', $callback, 10, 2 );
Description
Filter the arguments used to create a WooCommerce product when a license is generated. This allows developers to modify product details like name, price, and other attributes before the license product is saved. Use this to customize license product data on the fly.
Usage
add_filter( 'ulgm_license_attributes', 'your_function_name', 10, 2 );
Parameters
-
$args(mixed) - This parameter contains an array of arguments used to create a new product.
-
$product(mixed) - This parameter contains an array of arguments used to create a new license product, including its title.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the ulgm_license_attributes hook.
* This example adds a new attribute to the license product.
*
* @param array $attributes The original attributes array.
* @param WC_Product $product The WC_Product object being created.
* @return array Modified attributes array.
*/
add_filter( 'ulgm_license_attributes', function( $attributes, $product ) {
// Ensure $attributes is an array, if not initialize it.
if ( ! is_array( $attributes ) ) {
$attributes = array();
}
// Add a new attribute for 'license_duration'.
// We assume 'license_duration' is a custom taxonomy or meta field that needs to be an attribute.
// You would typically fetch this value from $args passed to create_license_product
// or from a global variable if available. For this example, let's assume a value.
// Let's pretend we have a duration of '12 months' we want to set.
// In a real scenario, this would be dynamically determined.
$license_duration_value = '12 months';
// If using WC_Product_Attribute objects directly, it's more complex.
// For simplicity, let's assume we are adding a simple string attribute.
// If 'license_duration' attribute already exists, we might want to update it.
$found = false;
if ( ! empty( $attributes ) && is_array( $attributes ) ) {
foreach ( $attributes as $key => $attribute ) {
if ( isset( $attribute['name'] ) && 'license_duration' === $attribute['name'] ) {
// If it exists, update its value. Note: This is a simplified update.
// Realistically, you'd check for attribute object type and update accordingly.
$attributes[ $key ]['value'] = $license_duration_value;
$found = true;
break;
}
}
}
// If the attribute doesn't exist, add it.
if ( ! $found ) {
$attributes[] = array(
'name' => 'license_duration',
'value' => $license_duration_value,
'is_visible' => 1, // Set to 1 if you want it visible on the product page
'is_variation' => 0, // Set to 1 if it's a variation attribute
'is_taxonomy' => 0, // Set to 1 if it's a taxonomy-based attribute
);
}
// You might also want to ensure the attribute is set as 'default'.
// This depends on how the product is intended to be used and if it has variations.
// For a simple product with a single license, this might not be necessary.
return $attributes;
}, 10, 2 ); // Priority 10, accepts 2 arguments ($attributes, $product)
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:352
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;
}