• Resolved Lust

    (@inumedia)


    Hello I wish create a function in function php which is switching a specific post status from pubslished to draft and then from draft to published

    I’ve tried this:

    function change_post_status($product_id,$status){

    $current_month = date(‘M’);

    if( $current_month == ‘Dec’ || $current_month == ‘Jan’ ) {

    $postid = 789;
    wp_update_post(array(
    ‘ID’ => $postid,
    ‘post_status’ => ‘published’
    ));

    } else {

    $postid = 789;
    wp_update_post(array(
    ‘ID’ => $postid,
    ‘post_status’ => ‘draft’
    ));

    }
    }

    but it’s no working.

    Have you a solution to resolve this problem ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Luminus Alabi a11n

    (@luminus)

    Automattic Happiness Engineer

    @inumedia,

    Taking a quick look at this code, you’re passing a $product_id to the function which never gets used and I don’t see you actually calling the function. The function isn’t going to work by itself if it isn’t called.

    All of your quotes are also wrong. You have the fancy style quotes instead of the regular ones e.g. ‘ID’ instead of 'ID'. It looks like you may have edited the code in Microsoft Word or something of the sort.

    I don’t see what this has to do with WooCommerce, though, since you’re trying to alter the post status of a regular post.

    If you’re not sure how to get this working for your context you may want to speak to a developer. If you don’t have a go-to developer, we highly recommend contacting one of the services on our customizations page: https://woocommerce.com/customizations/.

    Thread Starter Lust

    (@inumedia)

    Hello

    Thanks for your answer.

    In my code it’s the good quotes, may be it’s because i’v already past there in your tickets support and made a copy from this.

    My goal is to switch product as publish to unplublish by activating an option with a custom field. My customer have some “seasonal” product and some of them are not avalaible in some months per years.

    Luminus Alabi a11n

    (@luminus)

    Automattic Happiness Engineer

    @inumedia,

    It isn’t working because you’re not hooking the function to anything, i.e. you haven’t told it when to run.

    I attached it to the woocommerce_shortcode_before_products_loop hook in the code snippet below and it works as expected. You can see a visual representation of available hooks here – https://hooks.wpdesk.org/

    This will cause the function to run every time the home page is loaded so it probably isn’t the best way to go about it if you have a popular site as it will run for every user that hits the homepage.

    
    function change_post_status(){
    
        $current_month = date('M');
    
        if( $current_month == 'Dec' || $current_month == 'Jan' ) {
    
            $postid = 789;
            wp_update_post(array(
                'ID' => $postid,
                'post_status' => 'publish'
            ));
    
        } else {
    
            $postid = 789;
            wp_update_post(array(
                'ID' => $postid,
                'post_status' => 'draft'
            ));
    
        }
    }
    add_action( 'woocommerce_shortcode_before_products_loop', 'change_post_status', 5 );
    

    You’re probably better off doing this with a cron job that runs at 00:01 on December 1st to publish the product and again at 00:01 on February 1 to set it to draft.

    Plugin Support John Coy a11n

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @inumedia

    We haven’t heard back from you in a while so I am going to mark this thread as resolved. Feel free to open a new thread if you have any questions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘function change statut product’ is closed to new replies.