Woocommerce Product Variation override get_stock_quantity - accessing parent object - woocommerce

I want to override the get_stock_quantity for a specific product to allow for a single variant to not ever run out of stock ( its virtual so no stock, but you can't switch off stock management for a single variant )
I can get my method called
function test_get_digital_stock_quantity( $value) {
echo "<br/>STOCK!<br/>";
var_dump($value);
return $value;
}
add_filter( 'woocommerce_product_variation_get_stock_quantity', 'test_get_digital_stock_quantity',10,1);
Which is great, however I only want to do it for specific variations not all. So I need to access the thing this was called on. However the function only seems to take a single parameter even though the calling code suggests its passing $this.
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
So the question is, how do I get the thing that the get_prop was called on in the function that I have written. I need to be able to access the SKU in order to make the decision about the stock quantity to return...

Just realised the add_filter required a 2 not a 1 for it to be passed.
add_filter( 'woocommerce_product_variation_get_stock_quantity', 'test_get_digital_stock_quantity',10,2)

Related

auto generate post title in wordpress like ABC-123456

I want to auto-generate post title like that
Ex : ABC-123456
also i need to let (( ABC- )) fixed and random change the 06 numbers
and to dont change the post title through updating the post
First, to modify WordPress behavior the correct way, you find an appropriate hook. In this case, that would be a filter that allows changing the Post data before it is saved to the db.
The filter 'wp_insert_post_data' is exactly what you need, so you add your filter, and connect it to a function like so:
function zozson_filter_post_title(){
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
'wp_insert_post_data' is the name of the filter
'zozson_filter_post_title' is the name you give to your function, to hook to it.
50 is the priority. I chose 50 to run it after most other things. Default is 10
4 is the number of variables that the filter passes to your function.
So now we will add those variables and the logic inside it, to assign these CPT sho7nat those titles on admin saving them.
function zozson_filter_post_title( $data, $postarr, $unsanitized_postarr, $update){
//Then if it is the post type sho7nat
if( $data['post_type'] !== 'sho7nat' ){
return $data;
}
//If there is already a titled saved ($update returns true always)
if( $data['post_title'] !== '' ){
return $data;
}
//Let's build our title
$post_title = ' ABC-';
//What better random number that a unique timestamp?
$random_number = strtotime('now');
//Add the random number to the post title to save. You can do these in 1 line instead of 3
$post_title.= $random_number;
//We now have a post title with ABC- fixed and a random number, tell WordPress to use it as the post title
$data['post_title'] = $post_title;
return $data;
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
The title automatically assigned should be like in this example:

How to retrieve the latest categories assigned to a post currently under editing?

I need to limit the post tile length of a post belonging to a specific category while editing. So I need to check what categories have been assigned to the post under editing and decide whether to limit or not its post title.
I use "wp_insert_post_data" to do the job
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
But what I found is that the categories returned from passed $postarr are existing categories. Not the latest categories. So it would not work for new post or if categories being changed while editing.
$post_category = $postarr['post_category'];
I also checked get_the_category() inside the function, and that also returns existing categories, not the latest categories if category assignment changed.
My codes so far...
function limit_title_length( $data, $postarr ) {
// set up variables like max length, category id to limit
...
// get post id, title and categories from $data and $postarr passed
$title = $data['post_title'];
$id = $postarr['ID'];
$post_category = $postarr['post_category'];
// check if the specified category exists in the categories assigned to this post
...
// process $title, reset $post_title in $data
...
$data['post_title'] = $title;
return $data;
}
add_filter( 'wp_insert_post_data' , 'limit_title_length' , '99', 2 );
wp_insert_post_data fires in the very late stage of post publishing. I expected to get the latest categories from $postarr['post_category'];
but it's not in my case here. Any solutions or alternatives?
So just to be clear - You want to identify the most recent category added to a post?
If this is the case you will be hard pressed to do so as Wordpress does not save meta for added categories. If you are skilled enough you could script such a function and save the data in a custom table. Then retrieve it for use.
If you know the name of the category you are looking for you can use has_category! .

Wordpress how to retrieve aguments from filter

lets say this filter for example:
apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
I will hook on it like this:
function my_func($wut,$huh){
//...my code...
}
add_filter('wp_generate_attachment_metadata', 'my_func' );
How can I access the $metadata and $attachment_id from the initial apply_filter call in my function?
Thank you #Samvel for your comment, that totally works.
As seen on this page: https://developer.wordpress.org/reference/functions/add_filter/ I needed to add that 4th argument in my add_filter and set it to 2 to allow a second argument to pass in, otherwise it was locked at 1. Thats why I couldnt retrieve that second argument!

Wordpress - apply_filter that I can't connect to any add_filter

I am trying to edit a plugin for wordpress (woocommerce) and very often I find some lines that use the apply_filter function, for example one is this:
return apply_filters ( 'woocommerce_get_variation_sale_price', $price, $this, $min_or_max, $display );
Unfortunately I'm not able to understand what this filter do because I can't track back where the tag is used. I scanned (with search in eclipse) the workspace without any luck, I can't find any add_filter with this "woocommerc_get_variation_sale_price".
How is this possible? Reading the documentation of the two functions they should be connected...
I am stuck
The apply_filters() function will execute any function that has been been hooked to it using add_filter( $hook, $function_name, $priority, $num_arguments ) with the remaining values being passed to the function as arguments. This is generally how the WordPress Action and Filter hooks work to extend the functionality of core WordPress or plugins - WooCommerce in this case.
This means that the code in your example is optionally used to let you or other plugins change the value of $price that is being returned by the plugins. It is providing you with the parent object ($this) as well as other info about the variables used to calculate the price.
IF you can't find any other references to that string (your example is missing an 'e' in the filter name) then it means nothing is hooked to that filter and modifying the value of $price.
If you wanted to add a hook to filter the value of $price before it is returned it would look like the following.
// the name of the filter, the hooked function, the priority, and the # of args
add_filter( 'woocommerce_get_variation_sale_price', 'my_woocommerce_get_variation_sale_price', 10, 4 );
function my_woocommerce_get_variation_sale_price( $price, $product, $min_or_max, $display ){
// Use the arguments to do whatever you want to
// the $price before it is returned.
return $price;
}

What's the difference between global var post and get_post?

I use the_posts filter to add an object to each queried post. When access the added object, I get different result by using $post or get_post.
This is the code to attach the object to posts:
add_filter( 'the_posts', 'populate_posts_obj', 10,2 );
function populate_posts_obj( $posts, $query ){
if ( !count( $posts ) || !isset($query->query['post_type']) )
return $posts;
if( in_array( $query->query['post_type'], get_valid_grade_types())){
foreach ( $posts as $post ) {
if ( $obj = new Gradebook( $post->ID ) )
$post->gradebook = $obj;
}
}
return $posts;
}
Then, access the obj via $post, sometimes get the obj, sometimes not (even when it's the same post):
function get_the_gradebook(){
global $post;
return isset($post->gradebook) ? $post->gradebook : null;
}
Access the obj via get_post(), always get the obj:
function get_the_gradebook(){
global $post;
$p = get_post($post->ID);
return isset($p->gradebook) ? $p->gradebook : null;
}
I can just use the get_post() version, but it would be useful if I know why the difference.
Additional info:
If you ask the reason I attach an obj to each post, I think WordPress may take care of the caching process at the first place. Then, other caching plugins can work on my obj as if working on standard WP posts.
Lets explain you with a little bit pseudo code. I am trying to be broad with my approach so that my answer is relevant to StackOverflow however I still don't know how many down votes I may be receiving for this.
The simple difference is $post is a variable and get_post() is a method that means you can expect a different output from get_post() due to several dependencies however $post will only change when you explicitly do that.
Lets assume something like this
function get_post() {
return rand(0, 5);
}
$post = get_post(); /* lets assume random
value that was generated
this time was "2" */
Now each time you call get_post() its value be keep changing however the value of $post is always 2.
Coming back to the context of wordpress, $post is set using get_post() within the Loop and corresponds to the object referring to default post ID for current URL where as get_post() will take post ID as an input and return the post object.
$post is what WordPress considers to be the current "post" (post/page/custom post type) and can quite often end up giving you data you didn't quite expect. This is especially true if you perform WP_Query's in your template or have a template that uses data from several "posts".
By using get_post() with the ID you want the data from, you can be assured that you are getting the data you really want.

Resources