I have a bug with a Woocommerce website where by sometimes when saving the product, the regular price for all variations gets removed. This then causes a message to the customer saying that the product cannot be added to their cart when they try to buy.
Whilst I try and track down the bug, does anyone know how I may try to find all the product variations where the regular price is not set?
I could run a regular post query. I think the following parameters fit your description, but I have not tested it.
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_regular_price',
'value' => '',
'compare' => 'NOT EXISTS',
),
),
);
$variations = new WP_Query( $args );
In trying to debug your issue, I'd take a look at any plugins hooking in to woocommerce_save_product_variation or similar.
Related
I've tried to implement this, while trying to apply some bits of code taken from here and there.
I'm using Elementor Page Builder in a website and I created a custom post type with ACF. Inside that post type, there's a field for a start_date.
The idea is to only show posts in homepage when the date is equal or higher to present date.
Right now the code is like this:
add_action( 'elementor/query/por_data', function( $query ) {
// Here we set the query to display posts
// after specific date
$query->set( 'date_query', array(
array(
'after' => 'May 17, 2020',
)
) );
} );
This obviously just shows posts created after 17 May 2020, which isn't what I want. The idea is to grab the date from that ACF field, compare it with current date and show results accordingly. The custom field type name is "curso"
Found this piece of code but can seem to merge the two together as I have very to little knowledge in programming (yet)
$args = array(
'post_type' => 'events',
'orderby' => 'event_date',
'meta_key' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date('Y-m-d',time()),
'compare' => '>'
)
),
);
$q = new WP_Query($args);
var_dump($q->request);
Can someone help? Thanks
I tried to explain it in the comments - hopefully you will get a better understanding of it now.
add_action( 'elementor/query/por_data', function( $query ) {
$meta_query = $query->get( 'meta_query' );
// Append our meta query instead of overwriting all elementors own metaqueries
if($meta_query == ""){
$meta_query = array();
}
$meta_query[] = array(
array(
'key' => 'start_date', //Or whatever you field is called
'value' => date('Y-m-d',time()), //Make sure that the format is correct here
'compare' => '>' //Means that the value should be higher than the key, so this is what you want - If you also need the current dates posts, then you need to use '>=' instead
)
);
$query->set( 'meta_query', $meta_query ); //since we appended our query, you can safely set the meta querie now.
} );
You might need to tweak it a little, but this will lead you the right way.
I Would like to figure out which of my Woo Commerce customers have not ordered from me in x days and send them a reminder email about something store related.
I would like to be able to do a user meta query to grab all inactive users.
Trying to avoid having to do the more expensive method which is grabbing all orders between a range, and then comparing them with recent orders to see which users aren't in the recent orders using this query...
$orders_query = array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_completed_date',
'value' => $fromDate,
'compare' => '>=',
'type' => 'DATETIME'
),
array(
'key' => '_completed_date',
'value' => $toDate,
'compare' => '<=',
'type' => 'DATETIME'
)
) //AND
);
$result = get_posts($orders_query);
Does Woo Commerce add any metadata for the user for the last purchase?
I couldn't find anything in documentation/database digging.
Woocommerce does save a meta value for a user's last active time. Its a timestamp - meta key is "wc_last_active". As far as I can tell through the code - it gets updated whenever a user logs in or one of their orders is updated (including being placed). I'm not sure if that will work for your purposes?
If what you're worried about is efficiency of this query, you could also save this meta yourself? On placing an order you could simply add your own meta value for last order. That would make it much easier when you need to pull the data for display.
Hope that helps!
late to the party but for future Google-er
today I had to check what is saved in users meta and found out this peace of info: _last_order and some others are in meta .. and to get the date you simple do something like this:
$order = wc_get_order( $order_id );
$date = $order->get_date_created();
:) hope this help
I have this code that i need to get all product order by price :
I'm trying to sort my products by price but it doesn't work fine.
This is my code to get products, it works fine if I don't use the "orderby".
$args = array(
'post_type'=> 'product',
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$wooCommerceargs = new WP_Query( $args );
But the problem is no any product show !
First, do not use WP_Query() or get_posts(). From the WooCommerce doc:
wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.
See WooCommerce documentation
Second, you cannot order by price directly in the query. Get your products then call the wc_products_array_orderby() function.
$args = array(); // Optional arguments
$products = wc_get_products( $args );
$ordered = wc_products_array_orderby( $products, 'price', 'ASC' );
that works perfectly in 2022
$args = array(
'post_type'=> 'product',
'orderby' => '_price', //price metakey is '_price' !
'order' => 'ASC'
);
$wooCommerceargs = new WP_Query( $args );
I'm trying to link a custom field to a custom shortcode so the shortcode shows displays only the posts with the custom field selected to it.
This is my code below for my shortcode as you can see the key is my custom felid "flash_deal". When I enter the shortcode I just get all the perk psots and no the custom field perk posts?
add_shortcode('foundry_flash', 'shortcode_query_flash');
function shortcode_query_flash($atts, $content){
extract(shortcode_atts(array( // a few default values
'post_type' => 'perks',
'posts_per_page' => -1 ,
'meta_query' => array(
array(
'key' => 'flash_deal', // name of custom field
'value' => '"yes"', // matches exactly "red"
'compare' => 'LIKE'
)
)
), $atts));
The code you show is simply setting up variables - not running any queries at all (see extract documentation & shortcode_atts documentation).
I assume you have more code you just didn't add here, so likely the problem is the double quotes around yes that are causing issues. It's literally looking for "quoteyesquote".
As a general working example to get "Parks" Post based on custom meta, you need to use WP_Query as in the docs here.
$args = array(
'post_type' => 'parks',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'flash_deal',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
Then use a Nested Loop setup to loop through the results.
I have coded up a woocommerce theme and the website has a shop page. You can find out the shop page http://bit.ly/1TSMKrV
Now, I need few things to happen on this page
The products which are priced more than $50 should only display
The products must be sorted by popularity
The filters on the left side must function properly
I have tried to place the following code above the while loop
$args = array(
'post_type' => 'product',
'paged' => $page,
'posts_per_page' => 15,
'meta_key'=>'_sale_price',
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => '50',
'compare' => '>',
'type' => 'NUMERIC'
)
)
);
But this code just fulfils the first condition and not the other. For time being, I have removed the code and just used the default while loop that Woo Commerce provides.
Let me know where I am going wrong.
Thanks in advance!