• Resolved Anas

    (@anasmesy)


    PHP Snippet To Programmatically Populate the “Sale” Product Category With Products On Sale

    add_action( 'woocommerce_product_query', 'bbloomer_sale_category' );
     
    function bbloomer_sale_category( $q ) {
       if ( "sale" !== $q->get( 'product_cat' ) ) return; // "sale" = slug
       $q->set( 'post_type', 'product' );
       $q->set( 'product_cat', null );
       $product_ids_on_sale = wc_get_product_ids_on_sale() ? wc_get_product_ids_on_sale() : array();
       $q->set( 'post__in', $product_ids_on_sale );
    }

    when plugin deactivated, the code works = showing on sale products only.
    when plugin activated, No products. !

    how to solve this ?
    no products showing

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author annastaa

    (@annastaa)

    Hello Anas,

    I have just tested your custom code on a clean site with Storefront theme, and it works. I believe it works on your page as well, you simply do not have any on sale products added to the “Sale” product category!

    The on sale products are those that have their sale price set. If you follow the ???? ????? menu item of your site (sorry, I don’t know the language, but from the link I can see that it filters your shop page for all the on sale products), it returns only 1 product, and this products doesn’t belong to the “Sale” products category.

    You can check the products count of your “Sale” category in the Products > Categories section of the WordPress admin.

    A few other notes:

    1. Are you sure that you want to alter the main WooCommerce query in this manner:

       $q->set( 'post_type', 'product' );
       $q->set( 'product_cat', null );

    ??

    2. You have your AJAX mode set to the Dedicated AJAX. In this mode, your custom code doesn’t influence the filters’ AJAX queries.

    3. If you need the product counts to reflect your custom query condition, you need to add it to the product counts query with the help of the ‘awf_product_counts_query’ filter. Let me know here if you ever want to go ahead with that, and I will look into providing you with an example.

    4. And finally, the way the post__in is set in this line of your custom code:

    $q->set( 'post__in', $product_ids_on_sale );

    .. removes any values previously added to the post__in property by the filters or other plugins. Can’t speak for the 3d parties, but the string search and some other filters use the post__in, so you will be resetting them with this line of code.

    You could refine the existing post__in by first getting its current value, and then using array_intersect to intersect it with your own set of product ids (please see the class-a-w-f-frontend.php file of our plugin’s code folder for examples).

    Thread Starter Anas

    (@anasmesy)

    thank you so much for your replay. Yes you are right no products added to sale category. This code will add discounted products to sale category and remove them accordingly when I update prices. No need adding them manually.

    I added tow menu items: sale and featured from filter and works fine but:
    if I am viewing sale products:
    https://jasmine.com.co/shop/?onsale=yes

    Now I want to move to other category by using filter category option not the main menu, I the filter will add option to sale filter:
    https://jasmine.com.co/shop/?onsale=yes&product-categories=baby-boy-care

    I want to show category without sale filter:
    https://jasmine.com.co/shop/baby-boy-care/?archive-filter=1

    anyway I would be happy, If my first code worked as a separate link from menu.

    thank you

    Thread Starter Anas

    (@anasmesy)

    Plugin Author annastaa

    (@annastaa)

    Hello again Anas,

    For the products to appear in (any) category, you would need to add them to it, manually or programmatically, so I don’t know if this would anyway be the most efficient way of dealing with the task at hand.

    Would you perhaps like to try the WooCommerce [products] shortcode with on_sale parameter set to true (for example: [products columns=”5″ limit=”10″ paginate=true on_sale=true])? You can place it on any page (which means that you can make a permalink of something like https://www.mysite.com/products-on-sale/), and if needed, you can also make this page filterable by declaring it in the annasta Filters > Plugin settings > Shortcodes pages dropdown.

    As for moving from one category to another without the rest of the filtering parameters getting attached to the link, while there is an option in the premium version of our plugin that provides this functionality, you could also simply use the WooCommerce native Product Categories List widget as a category switch.

    Thread Starter Anas

    (@anasmesy)

    Thank you very much I really happy with your plugin and support. yes sure I try your recommendation [products columns=”5″ limit=”10″ paginate=true on_sale=true].

    happy with your kind replay it helped me ??

    hope you the best

    Thanks again

    Plugin Author annastaa

    (@annastaa)

    Dear Anas,

    Thank you for your kind words, it’s always a pleasure to be of help ??

    Have a great day!

    Plugin Author annastaa

    (@annastaa)

    Hello Anas,

    We’ll close this inactive thread, but please don’t hesitate to open a new one if you have any further questions about our filtering plugin!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘showing custom php sale category’ is closed to new replies.