• bob1974

    (@bob1974)


    hi,

    I like to customize the main page for logged users.

    I like to retrieve the products customizing the sql query, eg. retrieving by customer zip code or custom attributes.

    How to do? Can be done?

    thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • threadi

    (@threadi)

    You are talking about products. Are you talking about the output in a WooCommerce shop? If so, what are you currently using to output the products?

    I ask this because often, depending on the type currently implemented, you can customise the query used there to achieve what you want.

    It would also be important how you collect the address data of the users. Is this the address data collected by WooCommerce? Or do you use another plugin for this?

    Thread Starter bob1974

    (@bob1974)

    it’s a woocommerce shop

    eg. I like to show the products by, upon the zip code of the logged user

    (so I need to overload the sql query that populate the views)

    thanks for help

    threadi

    (@threadi)

    Are you already giving out products on the homepage? If so, with which technology? Background: if you use Elementor, you probably have an Elementor widget for this. Or you use WooCommece plain. Or something from your theme … there are many possibilities for this, which is why you need to know them to be able to tell you how best to proceed.

    Tip: you can also ask the question in the WooCommerce forum: https://wordpress.org/support/plugin/woocommerce/ – but if you already use some other plugin for the output, then their support would be better suited for this.

    Thread Starter bob1974

    (@bob1974)

    I use plain woocommerce without plugins.

    exactly, I like populate the products on the home page, etc. with a customized query ..

    threadi

    (@threadi)

    Then here is a simple example to influence the query of the archive. You have to adapt the marked position to your requirements (e.g. add meta_query or wherever you have stored the customer data).

    function custom_wpquery( $query ){
        // bail if user is not logged in.
        if( !is_user_logged_in() ) {
            return $query;
        }
    
        // bail if this is not a product-query.
        if( 'product' !== $query->get('post_type') ) {
            return $query;
        }
    
        // bail if this is not an archive-page.
        if( !is_archive() ) {
            return $query;
        }
    
        // change the query here!
    
        // return resulting query-object.
        return $query;
    };
    add_filter( 'pre_get_posts', 'custom_wpquery' );

    More about the query object can be found here: https://developer.wordpress.org/reference/classes/wp_query/

    If you have further questions, I would recommend the above WooCommerce support forum.

    Thread Starter bob1974

    (@bob1974)

    so I have to edit the Loop “object” in the theme
    can be possible to retrieve a custom field from the logged customer?eventually can be injected a plain SQL query inside the Loop code?
    example:
    1) get logged user zip code from the address
    2) within the Loop do a SQL query “select macro_area from geodb where zipcode is n”
    3) wpquery product array args where custom field macro_area=n (settled before when was inserted)

    thanks

    threadi

    (@threadi)

    Of course you can query all of this in the above-mentioned function. Unfortunately, I don’t know where you have saved your ZIP codes or the addresses you are talking about, which is why I can’t tell you anything concrete for the starting point. If it is a user meta-field, you can simply query it via https://developer.wordpress.org/reference/functions/get_user_meta/. And then execute the other queries as well.

    Thread Starter bob1974

    (@bob1974)

    thank you very much

    kind regards

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘custom sql query for logged users’ is closed to new replies.