mardi 24 février 2015

call woocommerce update (button) products function by code


In my site registered users can upload downloadable products by frontend with WPUF,and they can select if the products are free, selecting the value from a dropdown list


I've added this snippet of code in functions.php:



//function to update product status to 'publish'**

// and set the price to 0 for 'free' products**

add_action('wpuf_add_post_after_insert', 'change_post_status');

function change_post_status($post_id){


$project_type = get_post_meta($post_id, 'project_type', true);

if ($project_type == 'Free')
{

update_post_meta( $post_id, '_regular_price', 0);
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = 'publish';
wp_update_post($current_post);
}
}


it seems to work, I can see the product as live after submission, ready to be downloaded , BUT I can't see the products in the woocommerce shop page, and the price (in this case "Free") isn't set. In the product page appears an additional tab "Additional information" that displays the product_cat. The only thing that I've noticed in the database is that the metakey product_count_product_cat of wp_woocommerce_termmeta, isn't correctly updated.


To view the product correctly, in the woocommerce page, and in its page, with the correct price (in this case "Free") I need to enter as admin in Products, /edit and click the "Update" button. This solves everything.


How can I call the Publish/Update function button (in woocommerce/Products ) by code?


thank you





Aucun commentaire:

Enregistrer un commentaire