Skip to content

Instantly share code, notes, and snippets.

@Nicscott01
Last active April 17, 2022 19:59
Show Gist options
  • Save Nicscott01/16f0cfc4248c631f8b0ae6d9efbc37a6 to your computer and use it in GitHub Desktop.
Save Nicscott01/16f0cfc4248c631f8b0ae6d9efbc37a6 to your computer and use it in GitHub Desktop.
<?php
/**
* Additions to the Product Add-on Plugins
*
* Enables us to add a product taxonomy to the
* addon so we can further refine where it
* is displayed
*
*
*/
/**
* Add the taxonomy product_brand to the
* list of possible ones to look for
* when displaying the addon on the front end
*
*
*/
add_filter( 'get_product_addons_product_terms', function( $terms, $post_id ) {
//Add Designer terms
$designers = wp_get_post_terms( $post_id, 'product_brand', array( 'fields' => 'ids' ) );
$terms = array_merge( $terms, $designers );
return $terms;
}, 10, 2 );
/**
* Modify the query args for getting the
* addons posts
*
* We need to change the tax_query to include
* the additional taxonomy (product_brand)
*
*/
add_filter( 'get_product_addons_global_query_args', function( $args, $product_terms ) {
/*$current_product_id = wc_get_product()->get_id();
if ( ! $current_product_id ) {
return $args;
}*/
$cat_terms = [];
$extra_taxes = [];
foreach( $product_terms as $term_id ) {
$term = get_term( $term_id );
if ( ! is_wp_error( $term ) && ! empty( $term ) && $term->taxonomy !== 'product_cat' ) {
$extra_taxes[ $term->taxonomy ]['taxonomy'] = $term->taxonomy;
$extra_taxes[ $term->taxonomy ]['terms'][] = $term_id;
} else {
$cat_terms[] = $term_id;
}
}
//We are organizing the terms into proper tax groups
if ( !empty( $cat_terms ) ) {
$args['tax_query'][0]['terms'] = $cat_terms;
}
if( !empty( $extra_taxes ) ) {
//Make our relationship AND because we need to be very specific now that we're using 2 taxonomies
$args['tax_query']['relation'] = 'AND';
foreach ( $extra_taxes as $extra_tax ) {
$args['tax_query'][] = [
'taxonomy' => $extra_tax['taxonomy'],
'field' => 'id',
'terms' => $extra_tax['terms'],
'include_children' => false
];
}
}
return $args;
}, 10, 2 );
/**
* Add pick field for Designers to refine Addon location
*
* This is sort of a hack because you need to use the action
* and close the table row, then create another one.
*/
add_action( 'woocommerce_product_addons_global_edit_objects', function( $objects ) {
//Make sure we're on the edit page
if( empty( $_GET['edit'] ) ) {
return;
}
$designer_objects = (array) wp_get_post_terms( (int) $_GET['edit'], array( 'product_brand' ), array( 'fields' => 'ids' ) );
?>
</select>
<p class="description"><?php esc_html_e( 'Select which categories this add-on should apply to. Create add-ons for a single product when editing that product.', 'woocommerce-product-addons' ); ?></p>
</td>
</tr>
<tr class="custom-taxonomy-product-brand">
<th>
<label for="addon-objects-product-brand"><?php esc_html_e( 'Designers', 'woocommerce-product-addons' ); ?></label>
</th>
<td>
<select id="addon-objects-product-brand" name="addon-objects-product-brand[]" multiple="multiple" style="width:50%;" data-placeholder="<?php esc_attr_e( 'Choose designers&hellip;', 'woocommerce-product-addons' ); ?>" class="wc-enhanced-select-designers wc-pao-enhanced-select">
<option value="all_designers" <?php selected( empty( $designer_objects ), true ); ?>><?php esc_html_e( 'All Designers', 'woocommerce-product-addons' ); ?></option>
<optgroup label="<?php esc_attr_e( 'Designers', 'woocommerce-product-addons' ); ?>">
<?php
$terms = get_terms( 'product_brand', array( 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
echo '<option value="' . $term->term_id . '" ' . selected( in_array( $term->term_id, $designer_objects ), true, false ) . '>' . $term->name . '</option>';
}
?>
</optgroup>
<?php
}, 10, 1 );
/**
* Queue the script for the custom taxonomy selector
*
*/
add_action( 'admin_enqueue_scripts', 'admin_settings_mdj_addons_enqueue_scripts');
function admin_settings_mdj_addons_enqueue_scripts() {
if (
'product_page_addons' !== get_current_screen()->id &&
'product' !== get_current_screen()->id &&
'shop_order' !== get_current_screen()->id &&
'shop_subscription' !== get_current_screen()->id
) {
return;
}
wp_enqueue_script( 'wc-addons-custom-tax', get_stylesheet_directory_uri() . '/assets/js/wc-addons.js', array( 'jquery' ), null, true );
}
/**
* Process saving the Designer
*
*/
add_action( 'woocommerce_product_addons_global_edit_addons', function( $edit_post, $objects) {
//Save our custom taxonomy selector
$designer_objects = ! empty( $_POST['addon-objects-product-brand'] ) ? array_map( 'absint', $_POST['addon-objects-product-brand'] ) : array();
$result = wp_set_post_terms( $edit_post['ID'], $designer_objects, 'product_brand', false );
}, 10, 2);
/**
* Load the Designers on the Addon
*
*
*/
add_filter( 'woocommerce_product_addons_global_post_terms', function( $args ) {
$args[] = 'product_brand';
return $args;
}, 10 , 1);
jQuery( function( $ ) {
$('.custom-taxonomy-product-brand .description').text('Select which designers this add-on should apply to. You must make a selection here or this addon will not show up.');
$( '.wc-enhanced-select-designers' ).select2();
$( '.wc-enhanced-select-designers' ).on( 'select2:select', function( e ) {
var selectedID = e.params.data.id,
values = $( '.wc-enhanced-select-designers' ).val(),
all = 'all_designers',
allIndex = values.indexOf( all );
if ( all === selectedID ) {
//values = [ all ];
$('.wc-enhanced-select-designers option' ).prop('selected', 'selected' );
$('.wc-enhanced-select-designers option[value="all_designers"]' ).removeAttr( 'selected' );
$('.wc-enhanced-select-designers').trigger('change');
} else if ( 0 === allIndex ) {
//values.splice( allIndex, 1 );
}
//$( '.wc-enhanced-select-designers' ).val( values ).trigger( 'change.select2' );
} );
} );
@BrunoBros
Copy link

Hi,
Can you help me: I don't know how to install (or where to install) the js file.
I use Oxygen Builder so I don't use any theme...
Thank you to help me,
Bruo Bros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment