skip to Main Content

Good day,

I have a woocommerce eshop in which I do not use variants … all products are simple. But for partner shops I need to create XML with variable products.

I have products that are not published are “hidden” (custom post status).

I also have “parent_sku” for custom field products where my parent product is specified.

I have a SQL code that extracts all the necessary products from the database and also sku and parent_sku

I would need to modify the SQL code to display the post_title of the parent product for the product variant.

I enclose my SQL code and some pictures

    SELECT 
    wp_posts.post_title AS title,
    wp_posts.post_excerpt AS shortdesc,
    wp_posts.post_content AS longdesc,
    wp_postmeta1.meta_value AS sku,
    wp_postmeta6.meta_value AS parent_sku,
    wp_postmeta2.meta_value AS price,
    wp_posts1.guid AS img,
    wp_postmeta5.meta_value AS mall,
    GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
    ON wp_postmeta1.post_id = wp_posts.ID
    AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
    ON wp_postmeta2.post_id = wp_posts.ID
    AND wp_postmeta2.meta_key = '_regular_price'
LEFT JOIN wp_postmeta wp_postmeta3
    ON wp_postmeta3.post_id = wp_posts.ID
    AND wp_postmeta3.meta_key = '_thumbnail_id'
LEFT JOIN wp_posts wp_posts1
    ON wp_posts1.ID = wp_postmeta3.meta_value
    AND wp_posts1.post_type = 'attachment'
LEFT JOIN wp_postmeta wp_postmeta5
    ON wp_postmeta5.post_id = wp_posts.ID
    AND wp_postmeta5.meta_key = 'mall'
LEFT JOIN wp_postmeta wp_postmeta6
    ON wp_postmeta6.post_id = wp_posts.ID
    AND wp_postmeta6.meta_key = 'parent_sku'


LEFT JOIN wp_term_relationships
    ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy
    ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    AND wp_term_taxonomy.taxonomy = 'product_cat'
LEFT JOIN wp_terms
    ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
GROUP BY wp_posts.ID
ORDER BY sku ASC

my code show this table

IMG_1

I need to display a spreadsheet like this

IMG_2

thank you for any help <3

2

Answers


  1. Chosen as BEST ANSWER

    my fully functional code

     SELECT baseSQL.title p_title, 
               parentSQL.title AS p_parent_title, -- second field in picture
               baseSQL.shortdesc AS p_shortdesc,
               baseSQL.longdesc AS p_longdesc,
               baseSQL.sku AS p_sku,
               baseSQL.parent_sku AS p_parent_sku,
               baseSQL.price AS p_price,
               baseSQL.img AS p_img,
               baseSQL.mall AS p_mall,
               baseSQL.product_categories AS p_categories
        FROM
          ( SELECT 
              wp_posts.post_title AS title,
              wp_posts.post_excerpt AS shortdesc,
              wp_posts.post_content AS longdesc,
              wp_postmeta1.meta_value AS sku,
              wp_postmeta6.meta_value AS parent_sku,
              wp_postmeta2.meta_value AS price,
              wp_posts1.guid AS img,
              wp_postmeta5.meta_value AS mall,
              GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
            FROM wp_posts
            LEFT JOIN wp_postmeta wp_postmeta1
              ON wp_postmeta1.post_id = wp_posts.ID
              AND wp_postmeta1.meta_key = '_sku'
            LEFT JOIN wp_postmeta wp_postmeta2
              ON wp_postmeta2.post_id = wp_posts.ID
              AND wp_postmeta2.meta_key = '_regular_price'
            LEFT JOIN wp_postmeta wp_postmeta3
              ON wp_postmeta3.post_id = wp_posts.ID
              AND wp_postmeta3.meta_key = '_thumbnail_id'
            LEFT JOIN wp_posts wp_posts1
              ON wp_posts1.ID = wp_postmeta3.meta_value
              AND wp_posts1.post_type = 'attachment'
            LEFT JOIN wp_postmeta wp_postmeta5
              ON wp_postmeta5.post_id = wp_posts.ID
              AND wp_postmeta5.meta_key = 'send'
            LEFT JOIN wp_postmeta wp_postmeta6
              ON wp_postmeta6.post_id = wp_posts.ID
              AND wp_postmeta6.meta_key = 'parent_plu'
    
            LEFT JOIN wp_term_relationships
              ON wp_term_relationships.object_id = wp_posts.ID
            LEFT JOIN wp_term_taxonomy
              ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
              AND wp_term_taxonomy.taxonomy = 'product_cat'
            LEFT JOIN wp_terms
              ON wp_term_taxonomy.term_id = wp_terms.term_id
            WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
            OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
            GROUP BY wp_posts.ID
            ORDER BY sku ASC
          ) baseSQL
    
        LEFT JOIN
          (
            SELECT 
              wp_posts.post_title AS title,
              wp_postmeta1.meta_value AS sku
            FROM wp_posts
            LEFT JOIN wp_postmeta wp_postmeta1
              ON wp_postmeta1.post_id = wp_posts.ID
              AND wp_postmeta1.meta_key = '_sku'
          ) parentSQL
    
        ON baseSQL.parent_sku = parentSQL.sku 
        AND baseSQL.parent_sku > 0
    
        GROUP BY baseSQL.sku
    

  2. My first approach would be something like this. I can not test it cause I have no sample DB

    Make your sample SQL a subquery of a new query (it has parent SKU)
    Join with another subquery which contains parent SKU and the data you want.
    The main Query selects the fields in the desired order.

    SELECT baseSQL.title p_title, 
               parentSQL.title AS p_parent_title, -- second field in picture
               baseSQL.shortdesc AS p_shortdesc,
               baseSQL.longdesc AS p_longdesc,
               baseSQL.sku AS p_sku,
               baseSQL.parent_sku AS p_parent_sku,
               baseSQL.price AS p_price,
               baseSQL.img AS p_img,
               baseSQL.mall AS p_mall,
               baseSQL.product_categories AS p_categories
        FROM
          ( SELECT 
              wp_posts.post_title AS title,
              wp_posts.post_excerpt AS shortdesc,
              wp_posts.post_content AS longdesc,
              wp_postmeta1.meta_value AS sku,
              wp_postmeta6.meta_value AS parent_sku,
              wp_postmeta2.meta_value AS price,
              wp_posts1.guid AS img,
              wp_postmeta5.meta_value AS mall,
              GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
            FROM wp_posts
            LEFT JOIN wp_postmeta wp_postmeta1
              ON wp_postmeta1.post_id = wp_posts.ID
              AND wp_postmeta1.meta_key = '_sku'
            LEFT JOIN wp_postmeta wp_postmeta2
              ON wp_postmeta2.post_id = wp_posts.ID
              AND wp_postmeta2.meta_key = '_regular_price'
            LEFT JOIN wp_postmeta wp_postmeta3
              ON wp_postmeta3.post_id = wp_posts.ID
              AND wp_postmeta3.meta_key = '_thumbnail_id'
            LEFT JOIN wp_posts wp_posts1
              ON wp_posts1.ID = wp_postmeta3.meta_value
              AND wp_posts1.post_type = 'attachment'
            LEFT JOIN wp_postmeta wp_postmeta5
              ON wp_postmeta5.post_id = wp_posts.ID
              AND wp_postmeta5.meta_key = 'send'
            LEFT JOIN wp_postmeta wp_postmeta6
              ON wp_postmeta6.post_id = wp_posts.ID
              AND wp_postmeta6.meta_key = 'parent_plu'
    
            LEFT JOIN wp_term_relationships
              ON wp_term_relationships.object_id = wp_posts.ID
            LEFT JOIN wp_term_taxonomy
              ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
              AND wp_term_taxonomy.taxonomy = 'product_cat'
            LEFT JOIN wp_terms
              ON wp_term_taxonomy.term_id = wp_terms.term_id
            WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
            OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
            GROUP BY wp_posts.ID
            ORDER BY sku ASC
          ) baseSQL
    
        LEFT JOIN
          (
            SELECT 
              wp_posts.post_title AS title,
              wp_postmeta1.meta_value AS sku
            FROM wp_posts
            LEFT JOIN wp_postmeta wp_postmeta1
              ON wp_postmeta1.post_id = wp_posts.ID
              AND wp_postmeta1.meta_key = '_sku'
          ) parentSQL
    
        ON baseSQL.parent_sku = parentSQL.sku 
        AND baseSQL.parent_sku > 0
        GROUP BY baseSQL.sku
    

    Check the performance of this, maybe moving grouping and ordering outside will become in a better performance.

    I don’t propose to make a subquery in a nested JOIN within original LEFT JOINS because I cannot test it and from here and now, I can’t image how would react this “cross joining” with postmeta1 and postmeta6.

    I recommend to implement some trigger and have sku and parent_sku fields in table wp_posts and you could do simple subquery with the table itself.
    In mySQL there is no hierarchical queries (until I know mySQL) like in Oracle, but maybe this helps to improve your query.
    It’s up to you.

    Sorry If this don’t work on copy/paste, It’s just an approach.

    Edited with final functional SQL

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search