Enable woocommerce_cart_calculate_fees in the MiniCart? is it possible? - wordpress

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');

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?

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

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 ;)

How to achieve SEO friendly urls with Wordpress and custom parameters

I am having a big problem where I cannot use the Linkedin Share button.
One of the reasons is the non friendly URLs I am using.
Basically I have built a plugin for wordpress which shows me jobs.
It is working perfect show the jobs and everything but the link looks like this
www.recruitmentagency.com/job/?id=250
and I want it to look like
www.recruitmentagency.com/job/250
or
www.recruitmentagency.com/job/job-id/250
I tried to add rewrite rules to htaccess with no luck
RewriteRule ^job/([0-9]+)/$ job/?id=$1
RewriteRule ^job-id/([0-9]+)/$ /?id=$1
and none of them worked.
Any solution will be greatly appreciated.
I tried to use the inbuilt system and it is partially working.
However I don't like the way it is doing it so.
Instead of getting the job like
$job_id=$_GET['id'];
I am getting it like
$job_id=wp_query->query_vars['page'];
when the page is load like
www.recruitmentagency.com/job/250
I can't even understand why my supposed job-id is appearing as a page, but I am using it since I need this to work.
I was also facing the same issue. after long time I have reached the below solutions:
add_filter('rewrite_rules_array','job_rewrite_rules_array');
function job_rewrite_rules_array($rules){
$job_page = get_post(123);
$job_rules = array();
if( is_object($job_page) ){
$job_slug = $job_page->post_name;
$job_rules[$job_slug.'/([^/]*)$'] = 'index.php?pagename='.$job_slug.'&jobid=$matches[1]';
}
return $job_rules + $rules;
}
//Bind Query Var
add_filter('query_vars','job_query_vars');
function job_query_vars($vars){
array_push($vars, 'jobid');
return $vars;
}
Execute job_rewrite_flush just one time
function job_rewrite_flush(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','job_rewrite_flush');
Now access the param as: $jobid= get_query_var( 'jobid', 1 )
I think the above script work as per your requirement.

Drupal: change view argument

I searched far and wide to find a working solution to this but couldn't find it.
What I want to do is change the argument that is passed to the view because I want for pathauto cleaned taxonomy terms to work as an argument. I have a code that transforms cleaned term back to a original one, but cannot make the view use it.
I saw some people changing it in hook_preprocess_views_view(&$vars) but in my case (Views 2.11) has a argument in $vars instanced so many times that it's not the way.
Can anyone please help me change this argument?
There may be a better way but you could use views_embed_view() and set the arguments yourself
I have two ideas, either to add some custom php code to the view's argument's phpcode section that does something like this
$args[0] = 1;
return $args;
or try to use the function
hook_views_pre_view(&$view, &$display_id, &$args) {
// modify $args value here
}
didn't test them so don't know which will work.
I think hook_views_pre_view might help you do just that.

Do WordPress widget (or sidebar) hooks exist?

I'm trying to filter ALL widget output through a simple filter, but can't find any hooks and was hoping to be pointed in the right direction. Or possibly my efforts are not even possible?
My simple filter is something like this:
function clean_widget_output( $input ) {
return str_replace( array( "\t", "\n", "\r" ), '', $input );
}
add_[FILTER OR ACTION]( 'need_a_hook', 'clean_widget_output', 99 );
Any ideas? I'm pretty new to PHP, but I can get around.
This was borne out of the need/desire to clean the god-awful HTML spewed by WordPress' widgets. I love what they do, but some of the output makes me cry.
The short answer is output buffering because I couldn't find any widget or sidebar hooks.
The long answer is:
function tidy_sidebar( $sidebar_name_or_id )
{
ob_start();
$bool = dynamic_sidebar( $sidebar_name_or_id);
if ( $bool )
{
$str = ob_get_contents();
$str = 'do cleanup stuff...';
}
else
{
$str = '';
}
ob_end_clean();
return $str;
}
Then call echo tidy_sidebar( 'sidebar-name-or-id' ); from your theme.
I had a similar issue and after looking through Adam Brown's list of all WordPress filter hooks, found that the hook I needed does exist (widget_title, as pxl mentions), but that there is no hook to get all widget output. I thought I'd elaborate on the solution that worked for me.
Theoretically, the widget_title hook should affect all widgets on your blog, but I'm sure some 3rd party widgets neglect to include the necessary line of code to apply any title filters, so it's not foolproof. It worked for me, however, and it can be used to apply custom 'shortcode' (more accurately, in this case, 'longcode') or syntaxes to your widget titles. For example, I wanted to occasionally include html code in my widget titles, but by default, all html is stripped out. So, in order to be able to add things like <em> tags to text in some of my titles, I chose a custom syntax: [[ instead of < & ]] instead of > (for ex, [[em]] and [[/em]]) and then created a function in my theme's functions.php file to process that custom syntax and replace it with the html equivalent:
function parse_html_widget_title( $text ) {
return str_replace(array('[[', ']]'), array('<', '>'), $text);
}
Then I added a line below it to add the function as a filter:
add_filter('widget_title', 'parse_html_widget_title', 11); // 11 is one above the default priority of 10, meaning it will occur after any other default widget_title filters
The add_filter / apply_filter functionality automatically passes the content being filtered as the first parameter to the function specified as the filter, so that's all you need to do.
In order to do something similar for the main output of the widget, you would need to look at all your widgets to see what hook they use and verify that they have a filter for their main output, than use add_filter() for each hook you find with your custom callback function (for example, it's widget_text for the Text widget output, or get_search_form for the search form [you can see it in wp-includes/general-template.php, at the get_search_form() function]). The problem is that some of the dynamically generated widgets don't have hooks (like the Meta widget), which is why the output buffering solution Jeff provides is the most versatile, though not ideal, solution.
there are lots of hooks for wordpress widgets that aren't documented. The wordpress codex doesn't list them, for whichever reason (such as these hooks may change in the future and will break unexpectedly with new updates and versions)... so use these with extreme caution.
to find out what they are, there are at least 2 places to look:
<wordpress install directory>/wp-includes/default-filters.php
<wordpress install directory>/wp-includes/default-widgets.php
contained in those two files is a pretty good listing of all the hooks wordpress uses.
An example would be a filter for widgets is widget_title
again, use these with caution, they're not guaranteed to work past the specific version of the code you're looking at.
I'm not sure when they introduced the widget_text filter, maybe they didn't have it in '09 when this question was originally asked, but since it's there now, and for the sake of anyone that gets this stackoverflow like I did from google and just happens to read far enough down to see this answer, it's now actually quite simple:
function my_widget_filter( $content )
{
// manipulate $content as you see fit
return $content;
}
add_filter( 'widget_text', 'my_widget_filter', 99 );
Also maybe check out the dynamic_sidebar_params filter --
Another link --

Resources