• Resolved paul13234

    (@pavelreuk)


    I’m trying to exclude a hidden product from the WooCommerce recent products shortcode by writing the following php code using a code snippet plugin but the empty product box still appears. Basically I was able to hide a product but recent products is still displaying an empty area of where the product used to be instead of replacing it with the next recent product. Here is my code but it seems to not be working.

    <?php
    function exclude_hidden_product( $should_index, $id ) {
    $product_id_to_exclude = array(9872);

    if (in_array($id,$product_id_to_hide) && !empty(wc_memberships_get_user_active_memberships())) {
    $should_index = false;
    }
    return $should_index;
    }
    add_filter( ‘storefront_homepage_after_recent_products’,’exclude_hidden_product’, 10, 2);

Viewing 10 replies - 1 through 10 (of 10 total)
  • Stef

    (@serafinnyc)

    Have you tried CSS first? If that doesn’t do what you need you’re going to have to look for a better script online.

    I suggest asking over at Stack Exchange or Stack Overflow as well.

    The best solution is that you use woocommerce_shortcode_products_query filter and then you pass product ids as post__not_in

    you’ll also have to duplicate the storefront_recent_products() function. and will have to remove this action

    add_action( 'homepage', 'storefront_recent_products', 30 );
    and add your new function in this action.

    and the above filter will be called before $shortcode_content = storefront_do_shortcode( this line

    and after that variable, you’ll have to remove the filter to avoid conflicts with other shortcodes.

    Thread Starter paul13234

    (@pavelreuk)

    Vijay,

    What do you mean by post__not_in and how do I pass the product ids?

    By new function, did you mean the one I mentioned on the initial post?

    Thanks.

    post__not_in is a query parameter to exclude the post ids in the query. By default, you cannot pass the excluding ids in products shortcode, so in order to add excluding ids, you’ll have to use the filter that I gave you above.

    When when you’ll hook the post__not_in in the filter then it will be applied for all product shortcodes so we need to hook and unhook it before and after our shortcode call.

    By new function I mean another duplicate function of storefront_recent_products() and your initial post function will be removed that is not useful for your requirement.

    I am not sure how expert you’re with hooks and customization in WordPress but this customization will require good knowledge of WordPress.

    Thread Starter paul13234

    (@pavelreuk)

    Hi Vijay,

    I’m currently using code snippet plugin to include custom php code, do you know if that is a good place to add this function?

    Thanks.

    If you don’t change the child theme then the code snippet plugin is good to use but make sure you add/test custom code in stating or local website because if you make mistakes then you can lose the dashboard access due to error.

    Thread Starter paul13234

    (@pavelreuk)

    Thank you for the heads up.

    Thread Starter paul13234

    (@pavelreuk)

    Vijay,

    Would you have a code snippet sample of how this would work?

    Mirko P.

    (@rainfallnixfig)

    Hi @pavelreuk,

    Just to mention, if you do require more help with the actual coding, we’d recommend hiring a developer or one of the customization experts listed at https://woocommerce.com/customizations/.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Feel free to start a new thread if you have any more questions.

    Cheers.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WordPress WooCommerce Recent Products Exclude Hidden Products’ is closed to new replies.