Domanda

I got a plugin for hiding a product on a particular date (called WooCommerce Limited Deals). This plugin sets the visibility of the product to hidden. What I would like to do is to set the product to Draft because if visibility is hidden, the product still shows up in search results and with direct access. Below you can find the code of the plugin that sets it to 'hidden'. Is it possible to add a line that will also set it to 'draft'?

update_post_meta( $post->ID, '_visibility', 'hidden' );

For example(I know it won't work) something similar to this:

update_post_meta( $post->ID, 'status', 'draft' );

Function (updated)

public function check_single()
{
    global $post;

    if ( $this->is_enabled( $post->id ) && !$this->is_available( $post->id ) ) {

        // Set visibility to hidden
        $hide = get_option( 'tp_hide_expired' );
        if ( $hide == 'yes' ) {
            update_post_meta( $post->ID, '_visibility', 'hidden' );
            wp_update_post( array(
                'ID' => $post->ID,
                    'post_status' => 'draft',
            ) );
        }

        // Remove the add to cart button
        $disable = get_option( 'tp_disable_cart' );
        if ( $disable == 'yes' ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', array( 'WC_Limited_Deals', 'custom_add_to_cart' ), 30 );
        }
    }
}
È stato utile?

Soluzione

Just use wp_update_post.

http://codex.wordpress.org/Function_Reference/wp_update_post

wp_update_post( array(
    'ID' => $post->ID,
    'post_status' => 'draft',
) );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top