• Hello all,

    This is my first forum post so apologies if I do not follow the correct procedure!
    I have a wordpress site and also a MYSQL database (not the wordpress database). I use a discord SSO for users to log into my website. I would like to be able to control what products a user can see/add to cart/purchase based on a database query of MY database. Is this possible? I am happy to create the php code to manage the db query but not sure where I put the code.

    Many thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Your request definitely requires some individual development. I would recommend developing your own plugin for this. You can find an introduction to this in the manual: https://developer.www.ads-software.com/plugins/

    To access another database, you need to create a database connection to it. WordPress provides the wpdb-object for this. You can create a connection to your database like this, for example:

    $mydb = new wpdb('username','password','database','localhost');

    You can then send all the queries you need to $mydb. Have a look at the manual for wpdb: https://developer.www.ads-software.com/reference/classes/wpdb/

    How you then connect this with Discord SSO is more a question of your database structure and cannot be controlled by WordPress. However, WordPress provides numerous tools for less complex PHP development.

    Thread Starter tranquilityeden

    (@tranquilityeden)

    Hi there @threadi

    Thanks you for responding. I can manage the database connection (have already done so for the end of the purchase process). What I would like to do is be able to control what products are visible. I would like to query my database (the query I can organise) to determine if the user has access to a product line. What I don’t know how to do is:

    • How do I stall the code that loads the available products when a user clicks on the store so I can make my query
    • How do I then filter the products; removing the products they are not eligible to buy
    • How to run this check on their cart to ensure they don’t already have ineligible products in their cart.

    You say the products are all in your own database? So you don’t use a shop plugin like WooCommerce for their output? Then it’s entirely up to you how you query them. You certainly need a template for the list view and one for the detail view. In both, you need to fire the respective queries to retrieve and then display the data.

    Filtering also takes place at this point. You have to develop this completely yourself. As already mentioned, WordPress only provides you with the environment, but no ready-made solutions for such customised developments.

    Thread Starter tranquilityeden

    (@tranquilityeden)

    @threadi

    No, the products are held in the woocommerce database. The criteria that determines if the products are eligible is however held in my database

    Moderator bcworkz

    (@bcworkz)

    You cannot directly relate data from one DB to data from another DB. There needs to be something within the product DB you can use to determine what should be visible to any particular user. However, what you could do is query one DB for data, then use that data to qualify another query in the product DB. For example, in the criteria DB, use whatever criteria to get a list of product IDs to exclude, then use those results to query for products in the product DB, excluding the IDs in the list.

    The point being there’d need to be something in common to relate between the two DBs. Even if you cannot directly relate the two in a single query, you can still make multiple queries as long as there is some logical relation. It doesn’t have to be IDs, but there has to be something in common, much like there needs to be something in common to relate two different tables in the same DB.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show Products conditional based on database search’ is closed to new replies.