I want to display my woocommerce products on two different sections on the site i.e. the shop page and on an archive page i created using Custom Post UI plugin, called artists.
Using the Advanced Custom Fields plugin, i created a field that will attach each product created to an individual artist. The plan now is, on each artist page, pull products for the artist.
This is what i have so far. I hook into woocommerce_product_query, check if i'm on the single artist page and implement my meta query:
function artist_products( $query )
{
if( is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'artist_products' );
This is not working. When i visit my single artist page, i get a 500 server error. What I'm i missing?
woocommerce_product_query was breaking my site for some reason so i switched to pre_get_posts:
function artist_products( $query )
{
if ( $query->get('post_type') == 'nav_menu_item' )
{
return $query;
}
if( ! is_admin() && is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
'compare' => '=',
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'artist_products' );
Related
I want to have a filter for an attribute "brand" in the WooCommerce products section in the backend.
I am using the following code:
function custom_woocommerce_product_filters( $output ) {
global $wp_query;
$output .= wc_product_dropdown_categories(
array(
'show_option_none' => 'Filter by brand',
'taxonomy' => 'pa_brand',
'name' => 'pa_brand',
'selected' => isset( $wp_query->query_vars['pa_brand'] ) ? $wp_query->query_vars['pa_brand'] : ''
)
);
return $output;
}
add_filter( 'woocommerce_product_filters', 'custom_woocommerce_product_filters' );
This code displays the dropdown list for the filter with all brands inside it, but clicking on "Filter" does not return the filtered products. It simply shows all the products.
Can someone help please?
Thanks
I recently discovered that you have to actually filter the results using the pre_get_posts() filter, having the dropdown alone is not enough. What I was able to come up with after searching similar questions on stackoverflow:
// Filter woocommerce admin products by artist
function apply_artist_custom_product_filters( $query ) {
global $pagenow;
// Ensure it is an edit.php admin page, the filter exists and has a value, and that it's the products page
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['pa_artist'] ) && $_GET['pa_artist'] != '' && $_GET['post_type'] == 'product' ) {
// Create meta query array and add to WP_Query
$meta_key_query = array(
array(
'taxonomy' => 'pa_artist',
'field' => 'slug',
'terms' => esc_attr( $_GET['pa_artist'] ),
'operator' => 'IN',
)
);
$query->set( 'tax_query', $meta_key_query );
}
}
add_action( 'pre_get_posts', 'apply_artist_custom_product_filters' );
The snippet that did most of the work is found here:WooCommerce: Adding a custom filter to the Product Admin area. I only changed the meta query to be a tax query. Not sure if it's the best way to do it, but it works for me. What I can't figure out is the "selected" part of the wc_product_dropdown_categories, for me when I select an artist and hit filter, the filter works but it doesn't save the selection.
i have a woocommerce e-shop website. My search is searching only in product title and sku fields. I want to search also in some custom fields (like "_barcode" and "_mpn").
I have this script but when i use it in fuctions.php i can't search by product title.
function search_filter( $query ) {
$key_fields = array ( '_barcode', '_mpn', '_sku' );
$value_field = $query->query_vars['s'];
$query->query_vars['s'] = '';
if ( $value_field != '' ) {
$filter_query = array( 'relation' => 'OR' );
foreach ( $key_fields as $one_field ) {
array_push ( $filter_query , array (
'key' => $one_field,
'value' => $value_field,
'compare' => 'LIKE'
) );
}
$query->set( 'meta_query' , $filter_query );
}
}
add_filter( 'pre_get_posts' , 'search_filter');
Why i can't search the product title when i use this script?
Any idea?
Thank you.
I have a gravity form form on my WP site and I recently changed a free text field into a drop down field.
The website is a store which hold several categories of goods and I want my drop-down to show the user all the possible categories he can choose from.
Please assist in how to "pull" the categories into the drop-down list.
Thanks in advance.
You can do using some filters of gravity form, code is following
// Here 1 is form id
add_filter( 'gform_pre_render_1', 'populate_category' );
add_filter( 'gform_pre_validation_1', 'populate_category' );
add_filter( 'gform_pre_submission_filter_1', 'populate_category' );
add_filter( 'gform_admin_pre_render_1', 'populate_category' );
function populate_category( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-category' ) === false ) {
continue;
}
// Get category list
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$choices = array();
foreach( $categories as $category ) {
$choices[] = array( 'text' => $category->name, 'value' => $category->name );
}
$field->placeholder = 'Select a Category';
$field->choices = $choices;
}
return $form;
}
This is working perfectly its tested code.
My environment:
using DWQA plugin and customized restrict categories plugin to work with DWQA plugin
Problem:
I have customized "restrict categories" plugin to work for DWQA instead of WP posts.
Now the problem is when I logged in as user with privileges set to category2 from restrict categories, it's not able to show anything for me.
To set category filter restrict categories is using the following code:
add_filter( 'pre_get_posts', array( &$this, 'posts_query' ) );
public function posts_query( $query ){
if ( $this->cat_list !== '' ) {
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
// Make sure the posts are removed by default or if filter category is ran
if ( ! isset( $_REQUEST['cat'] ) )
$query->set( 'category__in', $cat_list_array );
elseif( isset( $_REQUEST['cat'] ) && $_REQUEST['cat'] == '0' )
$query->set( 'category__in', $cat_list_array );
}
return $query;
}
If I use category__not_in in place of category__in then all the posts are showing.
I have resolved this by changing code as below
public function posts_query( $query ){
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
$taxquery = array(
array(
'taxonomy' => 'dwqa-question_category',
'field' => 'id',
'terms' => $cat_list_array,
'operator'=> 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery );
It appears as tho the search functionality for WooCommerce products does not check "product_tag" taxonomy terms, nor SKU field? I added the SKUs as product tags to their respective products, but it still returns nothing when I search for the SKU.... How do I make the search functionality check product_tag terms? I have tried many many many things from adding tax_query to pre_get_post filter, to a whole new WP_Query loop, it just fails to search product_tags for some reason....so what is the point in a Product Tag???
I've tried the code from woo and others to search SKU with no results. However, the plugin search-everything, download from repository, does the job.
I got the solution to search the products by SKU code.
It is too simple just paste the following code in function.php and then you can see the changes in search results.
function search_by_id_only( $search, &$query_vars ) {
global $wpdb, $pagenow;
if ( 'edit.php' == $pagenow || empty($search) ) {
return $search;
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $query_vars->query['s'],
'compare' => 'LIKE'
)
)
);
$posts = get_posts( $args );
if ( empty( $posts ) ) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if ( sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
}
return $search;
}
add_filter( 'posts_search', 'search_by_id_only', 999, 2 );