• Resolved Bojan Deni?

    (@evilmc)


    I have page with all discount products with this shortcode: [sale_products]

    How to display my taxonomy discounts on the same page?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Unfortunately, that’s not easy to achieve.

    WooCommerce gets the products on sale with an internal function called wc_get_product_ids_on_sale, that offers no filter we can hook in to, to add the products on sale because of taxonomy rules:

    function wc_get_product_ids_on_sale() {
    	// Load from cache.
    	$product_ids_on_sale = get_transient( 'wc_products_onsale' );
    
    	// Valid cache found.
    	if ( false !== $product_ids_on_sale ) {
    		return $product_ids_on_sale;
    	}
    
    	$data_store          = WC_Data_Store::load( 'product' );
    	$on_sale_products    = $data_store->get_on_sale_products();
    	$product_ids_on_sale = wp_parse_id_list( array_merge( wp_list_pluck( $on_sale_products, 'id' ), array_diff( wp_list_pluck( $on_sale_products, 'parent_id' ), array( 0 ) ) ) );
    
    	set_transient( 'wc_products_onsale', $product_ids_on_sale, DAY_IN_SECONDS * 30 );
    
    	return $product_ids_on_sale;
    }

    The way we solved that problem in a customer’s website we developed some time ago, was by implementing custom code and a new shortcode that gets the products on sale returned by the wc_get_product_ids_on_sale function, and then we get the taxonomy ones with our internal function wctd_get_product_ids_on_sale, merge them together and create a custom loop to show them all with that new shortcode.

    That’s not something we will be able to do for you as this is a free plugin.

    Thread Starter Bojan Deni?

    (@evilmc)

    We make shortcode and display all sale products on page:

    function productsshort() {
    global $product;
    $product_ids = wctd_get_product_ids_on_sale();
    $product_ids2 = implode(",",$product_ids); //converts an array to JSON string
    $content = do_shortcode( '[products ids="'.$product_ids2.'" columns="3" orderby="post__in"]' );
    return $content;
    }
    add_shortcode ("productsshort", "productsshort");
    Thread Starter Bojan Deni?

    (@evilmc)

    I add custom text, when there is no sale products:

    function productsshort() {
    global $product;
    $product_ids = wctd_get_product_ids_on_sale();
    $product_ids2 = implode(",",$product_ids); //converts an array to JSON string
    if ($product_ids2){
    $content = do_shortcode( '[products ids="'.$product_ids2.'" columns="3" orderby="post__in"]' );
    } else {
    $content = "No products on sale.";
    }
    
    return $content;
    }
    add_shortcode ("productsshort", "productsshort");

    Great!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Diplay taxonomy products in page’ is closed to new replies.