WooCommerce - How To Update 'Net Sales' Only When Payment is Complete - wordpress

I have a WooCommerce set up, whereby all orders are added manually.
When I add a (pending) order, the WooCommerce order status hook registers the sale (for reporting).
I wish the switch this process to the hook called only when the order is (again manually) set to 'complete'.
There are a couple plugins ( eg. https://docs.woocommerce.com/document/woocommerce-order-status-control/ / https://wordpress.org/plugins/advanced-reporting-for-woocommerce/ etc ), but these are either overkill or simply don't provide this functionality..
I've also found a couple related posts, essentially describing overriding the woocommerce hooks to this end ( Getting order data after successful checkout hook etc, but unfortunately whilst the solutions correspond ( ie adapting the correct hooks - the context differs ).
I'm reluctant to prevent functionality in these hooks when attempting to overwrite/reorder the actions so any pointers which hooks I can use to achieve this would be really helpful.
Many thanks!

maybe you can try to use the ... filter, something like (untested):
add_filter( 'woocommerce_reports_order_statuses', 'fc_order_status_reports', 20, 1 );
function fc_order_status_reports( $statuses ) {
$statuses = array('completed');
return $statuses;
}
The code snippet is to add to your active theme's functions.php file.
Let me know if it does the job ;)

Related

Cron Wordpress Update four post every hour

I have function that update post content automatically based from custom field like this:
function update_content(){
$mycustomfield = get_post_meta( get_the_ID(), 'customfield', true);
$post = array();
$post['ID'] = get_the_ID();
$post['post_content' ] = $mycustomfield ;
$post['post_title' ] = $mycustomfield ;
// Update the post into the database
wp_update_post( $post );
We update only custom field to make content. For now, we launch this function manually on save_post hook, but the articles are so many and we need now a cron to automate this function: process 4 posts every hour until all posts are completed, then start over.
How to make this, thank you
WordPress actually has a built-in psuedo cron system called WP Cron. It doesn't function exactly like a proper server cron, but can perform a similar function in many cases. You can find documentation on it here:
https://developer.wordpress.org/plugins/cron/#:~:text=WP%2DCron%20is%20how%20WordPress,post%2C%20utilize%20WP%2DCron.&text=WP%2DCron%20works%20by%20checking,what%20needs%20to%20be%20run.
However thinking about your use case and looking at your function above, I'm wondering what the purpose of your cron is? It looks from that function like all you're doing is taking some content already in your database and putting it somewhere else. Why? Why not simply display your custom field in the correct spot? Or better yet, use the fields as intended?
Even if that is necessary - maybe I don't understand fully from the example above - I think your initial inclination to run this on save_post is much more correct. Unless there's some information external to your site that's changing, the only time these values will change is when you save the post. So what is the purpose of running it on a schedule?

Enable woocommerce_cart_calculate_fees in the MiniCart? is it possible?

I have a discount added via woocommerce_cart_calculate_fees.
This doesn't work in the minicart unfortunately and the subtotal isn't really correct in the minicart as a result.
Any idea how I could make it work?
Thank you,
Not a complete answer, just a small start.
I haven't found any answer to do this via a WooCommerce native function. So looks like we would have to do it ourselves. This will add the discounts in the minicart:
add_action('woocommerce_mini_cart_contents', 'addminicart');
function addminicart(){
$fees = WC()->cart;
$fees->calculate_fees();
$feesresult = $fees->get_fees();
foreach ($feesresult as $fee){
echo $fee->name.': '.$fee->amount.'<br />';
}
}
2 problems remain:
modifying the total/subtotal to include these
dealing with cart fragments / caching. I think this is a bit theme dependant. Anyway, this code will clear it via JS: $(document.body).trigger('wc_fragment_refresh');

Woocommerce "woocommerce_product_get_price" issue

The filter "woocommerce_product_get_price" to show a custom price works but casually I have discovered that this filter is triggered five times in single product page and six times in the archive product page. This I have checked including a var_damp in the following way:
add_filter('woocommerce_product_get_price', 'get_ws_price', 10, 2);
function get_ws_price($price, $product){
var_dump('HI!');
$price = 99.99;
return $price;
}
This is a problem for me, I use this filter to perform complex code and is repeating them many times.
What am I doing wrong?
Is there another way to modify the price of the product with another hook?
WooCommerce also has this problem which it solves by caching. It uses the WordPress get_transient() and set_transient() to save a version in the WordPress database. This allows the calculation to be used over multiple requests. Further, the calculation is saved in a global object so that within a single request the calculation can be retrieved from an in memory object.
WC_Product_Variable_Data_Store_CPT::read_price_data() in file '...\wp-content\plugins\woocommerce\includes\data-stores\class-wc-product-variable-data-store-cpt.php' is a good example of this technique.
You could choose to not use the filter and write your own function to replace get_price_html(). With the new function you can overwrite your WooCommerce templates such as the single product page woocommerce\templates\single-product\price.php and replace any calls to this function with your own custom function. This way it will only be called once.

How to change members per page in BuddyPress members directory

In BuddyPress, it shows 20 members per members directory page. I want to list 24 members per page with a pagination and sorting must work perfectly. I tried:
bp_has_members(bp_ajax_querystring('members').'per_page=24'))
It works but pagination and sorting are not working correctly.
For those like me wondering how to do this nowadays and ending here after searching with their fav engine, the proper way is to use a filter in bp-custom.php or the functions.php of your theme.
Cf. https://codex.buddypress.org/developer/using-bp_parse_args-to-filter-buddypress-template-loops/
For the member loop it would be something like :
function my_bp_members_per_page( $retval ) {
$retval['per_page'] = 24;
return $retval;
}
add_filter( 'bp_after_has_members_parse_args', 'my_bp_members_per_page' );
Bonus : this will still work if you use cache like WP Rocket.
Former method doesn't work with cache and logged in user.
You need an '&' for each additional argument.
Try:
bp_has_members(bp_ajax_querystring('members').'&per_page=24'))
To modify this file, you make a copy of it and put it into your child-theme
/your-child-theme/buddypress/members/members-loop.php

WP Post Meta Tags not working as expected

I have been trying to associate CUSTOM data to each of my WP POSTS using the following piece of code :
if($condition === true){
if ( ! update_post_meta ($post_id, '_someData', $someVariable) ) {
add_post_meta($post_id, '_someData', $someVariable);
}
}
However, seems like the META VALUE is RESET to default i.e. Zero OR Blank. Our WordPress website has around 40 plugins, and I think one of these WordPress plugins, is coming in my way of doing things. All of my logic works fine, on a demo WordPres website. Is there a way for me to have total control to set the META Value for a given POST ? Also, is there a way where I can be notified that the META Key is about to change and then I can decide whether OR not to change the Meta Value ?
Any pointers or reference URL's can be of great help. Thanks !
You only need to call update_post_meta in either scenario. Calling add_post_meta() is not necessary and could be causing this problem.
From the codex:
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.

Resources