Home > OS >  is there anyway to create link for specific products in shop page wordpress woocommerce?
is there anyway to create link for specific products in shop page wordpress woocommerce?

Time:07-30

I am trying to find a way to create a link to my shop page in woo commerce WordPress

for example, let's say I have 10 products

target products: 1, 3

and I want to send my friend a link that has two products (1,3) I want to show him. so is there any way to create a link like that

www.mywebiste/shop/product-1,product-2

if that idea doesn't work, can you share with me some opinions that would help me?

CodePudding user response:

Add this code to your theme functions.php file.

add_filter( 'pre_get_posts', function ( $query ) {
    if ( is_shop() && $query->is_main_query() ) {
        if ( ! empty( $_GET['share_products'] ) ) {
            $products    = sanitize_text_field( $_GET['share_products'] );
            $inc_product = explode( ',', $products );
            
            if ( ! empty( $inc_product ) ) {
                $query->set( 'post__in', $inc_product );
            }
        }
    }
    
    return $query;
} );

Then you can share your shop page link with products ID. like this (comma seperated product IDs),

www.mywebiste/shop/?share_products=54,41
  • Related