Do an action when woocomerce saves a product - php

I need to save some options when a woocommerce product gets saved.
Is there any way to do it?

You could use save_post action hook!
save_postDocs
You could use this boilerplate to do your custom work when a product gets saved!
add_action('save_post', 'do_some_custom_work');
function do_some_custom_work( $post_id )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!!');
}
}
Note:
In addition to $post_ID, you could pass $post and $update to your callback function as well. Please read the documentation for more details!
add_action('save_post', 'do_some_custom_work', 10, 3);
function do_some_custom_work( $post_id, $post, $update )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!! This is the product id: ' . $post->ID);
}
}

Related

WordPress - Update permalink automatically after posting an update

I am calculating custom title for a product using WooCommerce add product page. After the user post product's information, title is generated and saved by a save_post filter hook.
add_filter('save_post', 'modify_post_title', '99', 1);
function modify_post_title($post_id)
{
// some logic to form a new $title
// ...
if (!empty($title)) {
// update the title in database
$wpdb->update($wpdb->posts, array('post_title' => $title), array('ID' => $post_id));
// UPDATE PERMALINK
}
}
I need to know what function to use to re-generate the permalink after updating title.
Thanks in advance
add_filter( 'wp_insert_post_data', 'custom_slug_change', 50, 2 );
function custom_slug_change( $data, $postarr ) {
//Check for the post statuses you want to avoid
if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
Would you please add above code in your functions.php ?
I think you can go with the window.history.pushState to maipulate the Browser History.
I think these may help you.
window.history.pushState("object or string", "Title", surl[0]);

WordPress : how add new post meta before saving product woocommerce

i have a api from another wocommerce website and take some information with my product sku
how can i add this information to post meta after created new product ?
i wanna when i create a product , the information take with api and product sku and save to post meta.
i found this hook but i think this not working
<?PHP
function my_product_att( $sku ) {
// my api codes and take information and use
add_post_meta('product_id','key','value');
// i haven't problem here i just need Appropriate hook
}
add_action( 'save_post', 'my_product_att' ); // this hook not work for woocommerce
Woocommerce Products are wordPress posts. You can use wordpress hooks like save_post & $post_id as its argument. You are passing $sku which is wrong.
add_action( 'save_post', 'wpse_110037_new_posts' );
function wpse_110037_new_posts($post_id){
$post_type = get_post_type($post_id);
if($post_type == 'products') {
add_post_meta($post_id,'key','value');
}
}
Try below code. Note that in code $unique is true or false based on if meta value should be unique or not.
add_action('transition_post_status', 'product_created_function', 10, 3);
function product_created_function($newstatus, $oldstatus, $post) {
if($oldstatus != 'publish' && $newstatus == 'publish' && !empty($post->ID) && in_array( $post->post_type, array( 'product') ) ) {
add_post_meta( $post->ID, $key, $value,$unique );
}
}
add_action( 'save_post', 'prowp_save_meta_boxdddd' );
function prowp_save_meta_boxdddd( $post_id ) {
global $wpdb;
$table_prefix = $wpdb->prefix;
$tablename = $table_prefix.'postmeta';
$wpdb->query($wpdb->prepare('UPDATE '.$tablename.' SET meta_value="test" WHERE post_id='.$post_id));
}

Determine whether on shop page in woocommerce

My client wants the checkout to be "streamlined" to skip the cart page and go straight to checkout one product at a time. I got that covered but I also need to automatically empty the cart if the customer decides not to confirm checkout.
For this I wanted to check whether or not I'm on any other page than cart or checkout and do it there but all the commands I tried (is_shop(), is_front_page(), is_page('Shop'), is_product(), is_home()) always return false so I'm not sure what to do about it. This is how I'm trying to do it (in my themses functions.php):
function reset_cart_front() {
global $woocommerce;
echo "Attempting to empty<br>";
if (is_shop()) {
echo "is right page<br>";
$woocommerce->cart->empty_cart();
} else {
echo "is not right<br>";
}
}
add_action( 'init', 'reset_cart_front' );
what gives?
Nevermind, I figure it out!
function reset_cart_shop_loop() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action( 'woocommerce_before_shop_loop', 'reset_cart_shop_loop' );
Correct me if I'm wrong...but I think you should check if the current page is_checkout(), and empty the cart if it's not:
function reset_cart_front() {
global $woocommerce;
if ( !is_checkout() ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'template_redirect', 'reset_cart_front' );
I also think 'init' is too early to hook (and would recommend trying 'template_redirect').

Change post author on post update in wordpress

When an author clicks "update" on a post in the dashboard, how would I have the posts author change automatically to whatever author it was?
I'm trying to use this code to trigger something when a post is updated but nothing happens. Any Ideas?
add_action( 'publish_post', 'changeAuthor' );
function changeAuthor($post_id){
echo "hello";
}
this could be the function to call...
code ist not tested.
add_action('save_post', 'functiontocall');
functiontocall () {
if ( ! wp_is_post_revision( $post_id ) ){
$my_post = array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'functiontocall');
// update the post, which calls save_post again
wp_update_post( $my_post );
// re-hook this function
add_action('save_post', 'functiontocall');
}
}
Did some more research and got an answer:
To make sure you hit the right action use the following
add_action('edit_post', 'functiontocall');
add_action('save_post', 'functiontocall');
add_action('publish_post', 'functiontocall');
add_action('edit_page_form', 'functiontocall');
Also, do not test this by echoing something because of some way wordpress redirects the echo will not appear! But anything else works :)

Unble to get save_post to work in wordpress

I have the following code to update the post_meta when a post is created. It is very simple,just storing the its own post_id in a meta field(might add more in the future)
The following code is not working, I guess it is because the $post_ID is blank, how do I pass the post_id of newly created post to the function update_postmeta (in function.php)?
//code from function.php
add_action('save_post', 'update_postmeta');
function update_postmeta($post_ID) {
update_post_meta($post_ID, 'related_id',$post_ID);
}
Here is a good boilerplate for you:
function update_postmeta($post_id) {
global $post;
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
update_post_meta($post->ID, 'related_id', $rand_id);
}
No where in your code is $rand_id defined though.

Categories