So I've managed to get something working in 3 parts.
- Show the global add-ons as a multi-select in the product data metabox on the Add-ons tab
- Save the custom meta field
- Add the global add-ons into the list of add-ons to display on the front-end
NB: I suggest creating a dummy product category. Assign the global addon to appear for products in the dummy category, that keeps them from displaying everywhere so they only appear when you add them to the metabox.
/**
* Add a custom field to the Add-ons meta panel
*/
function kia_add_custom_addons_fields() {
global $product_object;
if( ! $product_object instanceof WC_Product ) {
return;
}
$global_addons = WC_Product_Addons_Groups::get_all_global_groups();
?>
<div class="wc-pao-field-header">
<p><strong><?php esc_html_e( 'Global Add-on Groups', 'your-text-domain' ); ?><?php echo wc_help_tip( __( 'Add a global group to this particular product', 'your-text-domain' ) ); ?></strong></p>
</div>
<div class="wc-pao-global-addons">
<p class="form-field">
<label for="global_addons"><?php _e( 'Apply Global Add-ons to this product', 'your-text-domain' ); ?></label>
<?php
// Generate some data for the select2 input.
$product_add_ons = array_filter( (array) $product_object->get_meta( '_global_addons' ) );
?>
<select id="global_addons" class="wc-enhanced-select" name="global_addons[]" multiple="multiple" style="width: 400px;" data-sortable="sortable" data-placeholder="<?php esc_attr_e( 'Search for a Global Add-on…', 'your-text-domain' ); ?>" >
<?php
foreach ( $global_addons as $add_on ) {
echo '<option value="' . esc_attr( $add_on['id'] ) . '"' . selected( in_array( $add_on['id'], $product_add_ons ), true, false ) . '>' . wp_kses_post( $add_on['name'] ) . '</option>';
}
?>
</select>
</p>
</div>
<?php
}
add_action( 'woocommerce_product_addons_panel_start', 'kia_add_custom_addons_fields' );
/**
* Save the custom field data.
*
* @param obj $product WC_Product - the product object.
*/
function kia_save_custom_addons_fields( $product ) {
// Layout.
if ( ! empty( $_POST[ 'global_addons' ] ) ) {
$meta = array_map( 'intval', (array) $_POST[ 'global_addons' ] );
$product->add_meta_data( '_global_addons', $meta, true );
}
}
add_action( 'woocommerce_admin_process_product_object', 'kia_save_custom_addons_fields' );
/**
* Force the custom add-on into the product display.
*
* @param array $product_addons
* @param int $post_id
* @param return array
*/
function kia_add_global_product_addons( $product_addons, $post_id ) {
global $product;
if( ! is_admin() && $product instanceof WC_Product && $post_id === $product->get_id() ) {
$meta = $product->get_meta( '_global_addons', true );
if( ! empty( $meta ) ) {
$args = array(
'posts_per_page' => -1,
'post_type' => 'global_product_addon',
'post_status' => 'publish',
'suppress_filters' => true,
'include' => $meta
);
$global_addons = get_posts( $args );
if ( $global_addons ) {
$new_addons = array();
foreach ( $global_addons as $global_addon ) {
$new_addon = apply_filters( 'get_product_addons_fields', array_filter( (array) get_post_meta( $global_addon->ID, '_product_addons', true ) ), $global_addon->ID );
$new_addons = $new_addons + $test;
}
$product_addons = array_merge( $new_addons, $product_addons );
}
}
}
return $product_addons;
}
add_filter( 'get_product_addons_fields', 'kia_add_global_product_addons', 10, 2 );