Woocommerce Filter Hooks - wordpress

I'm hoping to get a little insite for using filters in Woocommerce. My main question is, what am I looking for in the template files? or what are the variables that can be targeted using filters? If we look at the filters list I see filter name and files. Using this filter
single_product_small_thumbnail_size
Files - product-thumbnail.php and woocommerce-template.php
What am I looking for in those files that can be targeted and changed? Would you give me a simple example? Maybe something like change thumbnail size?
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable ) {
// Your code
return $variable;
}
I understand what each part of the function and filter are, but I'm not sure what code to write for "Your code." What variable am I grabbing from the file? How do I apply the change? I can't completely wrap my mind around this. Any help would be greatly appreciated.
Thanks,
~MK

As you can see in e.g. woocommerce-template.php you can filter the shop_catalog string:
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
That string is used in the subsequent code to determine the image size to be used:
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
So, if you would like to use another image size, you can filter the string like e.g.:
add_filter( 'single_product_small_thumbnail_size', 'my_single_product_small_thumbnail_size', 25, 1 );
function my_single_product_small_thumbnail_size( $size ) {
$size = 'large';
return $size;
}

Related

Wordpress / Woocommerce Hook into a Function

I appreciate questions similar to this have been asked before but the answers I've tried aren't doing what I need.
Basically,
I have this file in the woocommerce plugin folder structure:
wp-content\plugins\woocommerce\includes\wc-coupon-functions.php
Inside the file is the following function:
function wc_get_cart_coupon_types() {
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart') );
}
I need to override it so it returns an additional item in the array but nothing I've tried has worked. I've tried:
Creating the same file in my custom theme file
hooking the function in my functions file with the following code:
function woocommerce_coupon_get_cart_coupon_types()
{
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart', 'custom_discount' ) );
}
add_filter('wc_get_cart_coupon_types', 'woocommerce_coupon_get_cart_coupon_types',10, 1);
Any other suggestions would be greatly appreciated, also..... I've made the change directly in the file and it definitely works. Thanks
Your #2 approach is sort of how to do it, but you're essentially caught in a loop situation the way you did it.
You need to do it this way:
function wpso59974749_woocommerce_coupon_get_cart_coupon_types( $data ) {
$data[ 'your_new_key_here' ] = 'your new value here';
return $data;
}
add_filter('woocommerce_cart_coupon_types','wpso59974749_woocommerce_coupon_get_cart_coupon_types',10, 1);
You shouldn't add the apply_filter back in your function, as it would get stuck in a loop - essentially refiltering itself over and over.
I prefixed your function so if there is another woocommerce_coupon_get_cart_coupon_types function, it won't conflict.

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Remove a div with ID from the_content WordPress

I was trying to remove a div having some ID from the_content WordPress.
I am trying to achieve something like this
jQuery( "#some_id" ).remove();
but on server side,
I don't have a clue how I can do this on server side within the_content filter hook.
This is something I like to achieve,
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content.find('#some_id').remove();
return $content;
}
I think this is the answer for your problem: https://stackoverflow.com/a/1114925/7335278
In your case this would look like:
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
$element = $doc->getElementById('some_id');
$element->parentNode->removeChild($element);
$content = $doc->saveHTML();
return $content;
}
hth, let us know if this will work.
cheers, joel
The code is not tested. Just wrote for you... check this code... if this gives any syntactical error plz fix and let me know. But inside the content hook HTML/jsscripts are not running so you can try something like the following --
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content = preg_replace("(<([a-z]+id=\"some_id\")>.*?</\\1>)is","",$content);
return $content;
}
Sorry for the previous answer.
I saw to late that what I answered was not what you were after.
Here something that might work.
$str = "<html><head></head><body><span id='remove_this'>This is one
text</span><br /><span>this is a text after that</span></body></html>";
$id_pos = strpos($str, 'remove_this');
$tmp_str = substr($str, ($id_pos-10), (strpos(substr($str, ($id_pos-10)), '</span>' )+7));
$str = str_replace($tmp_str, '', $str);
echo $str;
in the example above I start with searching for where the id is located in the string.
Than I do a substring function to get the start of the html element, in this case being the span.
Than in the same substring call I search for the first closing span tag and add 7 to the position to include the closing span tag.
than I replace the result of the substring in the original string and you get the result you are looking for.
I know there are always better ways but this is one :)
NOTE: This example of mine does use set amount of distance between the id position and the start of the tag as it directly after the start.
So there are most likely much better ways to get to the first instance of the < in php but I don't know it yet.
Hope this might get you a bit further.
I could also have gone into regular expressions but this is easier although all but the best way.
Regular expressions on the other hand are a lot more complex and I'm not really familiar with that stuff.
Cheers
and again sorry for the wrong answer at first.

Remove header (first row) from csv export

The company I work for has bought the amazing plugin "WooCommerce Customer/Order CSV Export" which really works like a charm. The documentation is sublime, I got my extra fields and I get the snippets to customize my export, this all works perfectly!
Now, I have a small problem with the headers. I need to remove them and I found a filter to apply but I don't understand how to make a function out of this. Can you maybe provide me a way to export the data without the headers on the first row?
So basicly, can you help me write a function that removes the first row of the export or hide the header array?
This is the filter I found in the documentation:
apply_filters( 'wc_customer_order_csv_export_generated_csv', $csv, $this );
Many thanks in advance and with kind regards,
Try this:
//delete headers
function delete_headers_csv($csv){
foreach (explode("\n", $csv) as $line) {
if ($i != 0 ) {
$temp .= $line."\n";
}
$i++;
}
echo $temp;
}
add_filter( 'wc_customer_order_csv_export_generated_csv','delete_headers_csv');

paginate_links() outputs "paged=" to link instead of "page="

I've been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
...is where the link leads to. "Paged=" gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I'm playing a "shell game" with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page= to be output in every case? Conversely, if &paged= should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I've dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged variable in the URL which breaks pagination, I created a filter to simply replace it with page.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I'm not sure if I'm the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. :D
So here is what I came up with...we might as well call it a hack or work-around because its unknown if this was indeed a bug I'm dealing with (from wordpress itself) or just a perfectly hidden problem in my theme's code.
The below function filters the 'paginate_links()' function and achieves this:
I see paged variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that. if_search(), we do our changes...
If this is a search, we get the would-be page # destination using regex (or simply string replace paged to page, depending on permalink structure. We use remove_query_var() to drop faulty variable, and add_query_var() to put the working one.
This is being done because in this strange case, page always generates proper results.
Then we return the edited destination link, and life is good.
Function below:
add_filter( 'paginate_links', 'my_search_pagination_mod', 1 );
function my_search_pagination_mod( $link )
{
if ( is_search() ) {
$pattern = '/page\/([0-9]+)\//';
if ( preg_match( $pattern, $link, $matches ) ) {
$number = $matches[ 1 ];
$link = remove_query_arg( 'paged' );
$link = add_query_arg( 'page', $number );
} else {
$link = str_replace( 'paged', 'page', $link );
}
}
return $link;
}
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

Resources