Created
October 18, 2024 10:31
-
-
Save kish2011/2d99f088d4bdc952e067587f706d47d2 to your computer and use it in GitHub Desktop.
WooCommerce Product Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WooCommerce Product Collections | |
Plugin URI: http://upnrunn.com | |
Description: Set WooCommerce Product Collections based on Price range | |
Author: Kishore Sahoo | |
Version: 0.0.1 | |
Author URI: http://upnrunn.com | |
*/ | |
add_action( 'woocommerce_init', 'woocommerce_collections_init' ); | |
function woocommerce_collections_init() { | |
$labels = [ | |
'name' => esc_html__( 'Collections', 'wc-product-collections' ), | |
'singular_name' => esc_html__( 'Collection', 'wc-product-collections' ), | |
'menu_name' => esc_html__( 'Collections', 'wc-product-collections' ), | |
'all_items' => esc_html__( 'All Collections', 'wc-product-collections' ), | |
'edit_item' => esc_html__( 'Edit Collection', 'wc-product-collections' ), | |
'view_item' => esc_html__( 'View Collection', 'wc-product-collections' ), | |
'update_item' => esc_html__( 'Update Collection', 'wc-product-collections' ), | |
'add_new_item' => esc_html__( 'Add New Collection', 'wc-product-collections' ), | |
'new_item_name' => esc_html__( 'New Collection Name', 'wc-product-collections' ), | |
'parent_item' => esc_html__( 'Parent Collection', 'wc-product-collections' ), | |
'parent_item_colon' => esc_html__( 'Parent Collection:', 'wc-product-collections' ), | |
'search_items' => esc_html__( 'Search Collections', 'wc-product-collections' ), | |
'popular_items' => esc_html__( 'Popular Collections', 'wc-product-collections' ), | |
'back_to_items' => esc_html__( '← Go to Collections', 'wc-product-collections' ), | |
'separate_items_with_commas' => esc_html__( 'Separate collections with commas', 'wc-product-collections' ), | |
'add_or_remove_items' => esc_html__( 'Add or remove collections', 'wc-product-collections' ), | |
'choose_from_most_used' => esc_html__( 'Choose from the most used collections', 'wc-product-collections' ), | |
'not_found' => esc_html__( 'No collections found', 'wc-product-collections' ) | |
]; | |
$args = [ | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'query_var' => true, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_in_menu' => true, | |
'show_in_rest' => true, | |
'show_admin_column' => true, | |
'rewrite' => [ | |
'slug' => apply_filters( 'wc_product_taxonomy_slug', 'collections' ), | |
'hierarchical' => true, | |
'with_front' => apply_filters( 'wc_product_taxonomy_with_front', true ) | |
] | |
]; | |
register_taxonomy( 'collections', [ 'product' ], $args ); | |
} | |
add_action( 'collections_add_form_fields', 'add_term_fields' ); | |
add_action( 'collections_edit_form_fields', 'edit_form_fields', 10, 2 ); | |
//add_action( 'edit_collections', [ $this, 'save_form_fields' ] ); | |
//add_action( 'create_collections', [ $this, 'save_form_fields' ] ); | |
function add_term_fields( $taxonomy ) { | |
?> | |
<div class="form-field"> | |
<label for="price_range">Price Range</label> | |
<input type="number" name="price_range" id="price_range" /> | |
<p>Put a price like Under 900</p> | |
</div> | |
<?php | |
} | |
function wpdocs_product_meta( $post_id ) { | |
// Check the logged in user has permission to edit this post | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return $post_id; | |
} | |
$term_id = 30; // term id: change it | |
// get meta data value | |
$price_range = get_term_meta( $term_id, 'price_range', true ); | |
$IDs = get_ids_on_price_range($price_range); | |
//print_r($IDs); | |
foreach ($IDs as $key => $value) { | |
// code... | |
wp_set_post_terms( $value, $term_id, 'collections' ); | |
} | |
//die; | |
} | |
add_action( 'save_post_product', 'wpdocs_product_meta' ); | |
function edit_form_fields( $term, $taxonomy ) { | |
// get meta data value | |
$price_range = get_term_meta( $term->term_id, 'price_range', true ); | |
?><tr class="form-field"> | |
<th><label for="price_range">Price Range</label></th> | |
<td> | |
<input name="price_range" id="price_range" type="number" value="<?php echo esc_attr( $price_range ) ?>" /> | |
<p>Put a price like Under 900</p> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'created_collections', 'rudr_save_term_fields' ); | |
add_action( 'edited_collections', 'rudr_save_term_fields' ); | |
function rudr_save_term_fields( $term_id ) { | |
update_term_meta( | |
$term_id, | |
'price_range', | |
sanitize_text_field( $_POST[ 'price_range' ] ) | |
); | |
} | |
function get_ids_on_price_range($value='100') | |
{ | |
$IDs = array(); | |
$query = array( | |
'post_status' => 'publish', | |
'post_type' => 'product', | |
'posts_per_page' => 99999999, | |
'meta_query' => array( | |
array( | |
'key' => '_price', | |
'value' => array(0, $value), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC' | |
) | |
) | |
); | |
$wpquery = new WP_Query($query); // return 10 products within the price range 50 - 100 | |
foreach ($wpquery->posts as $key => $value) { | |
$IDs[] = $value->ID; | |
} | |
return $IDs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment