1

While on a product page (product 1), I am trying to pull through information about a different relevant product (product 2).

To create a kind of 'you may also like' list of products.

I am trying the simplest of WP_Query but cannot figure out how to pull through all the product data from one product (product 2) in a list so I can see ahead of time what will be available to bring across to the product I am currently browsing (product 1).

I need something along the lines of this:

<?php
$additional_forms_args = array(
    'post_type' => 'product',
    'posts_per_page' => 1,
    'p' => $other_form_product_id
);

$additional_form_query = new WP_Query($additional_forms_args);

if ( $additional_form_query->have_posts() ) :

    while ( $additional_form_query->have_posts() ) : $additional_form_query->the_post();

            global $product;

            $attributes = $product->get_attributes();

            var_dump( $attributes );

    endwhile;

    wp_reset_postdata();

endif;

I am expecting a var_dump of the products data I have inserted the product_id of.

I expect to see an array of product data for the product with the ID of what I place into the args, instead I get an empty array:

array(0) { }

Please can someone help me to gather all product data associated with a product separate to the one I am currently browsing and dump its data for inspection?

EDIT (refine question):

I also have this snippet creating a variable on my products:

woocommerce_wp_checkbox(
    array(
        'id' => 'greener_production',
        'wrapper_class' => 'show_if_simple',
        'label' => __('Greener Production', 'woocommerce'),
        'description' => __('Tick if this product is the result of Greener Production', 'woocommerce'),
    )
);

Within this same snippet could we query this to see if this is ticked or not 'yes : no'?

Thanks, Jason.

2 Answers 2

1

I literally did this same thing this morning. Here's a function that you can use. You can add the function to your functions.php (or wherever) and then call the function on your template:

 /**
 * @param array $product_ids product_id the product id for the product you want to retrieve.
 *
 * @return string the HTML output
 */
function wpse361339_relevant_products( $product_ids = [] ) {
    global $post;

    // Bail early if there is not product ids.
    if ( is_empty( $product_ids ) ) :
         return;
    endif;

    // Args here
    $args = [
        'post_type'      => 'product',
        'posts_per_page' => 1,
        'post__in'       => $product_ids,
        // MORE ARGS HERE IF NECESSARY
    ];

    $related_products = get_posts( $args );

    $product_output = '';

    foreach ( $related_products as $related_product ) {

        // Get the product object so we have access to the attributes.
        $product = wc_get_product( $related_product->ID );

        // $product_output .= BUILD YOUR MARKUP HERE
        echo '<pre>' . print_r( $product, TRUE ) . '</pre>';

    }

    return $product_output;
}

I made it so you can pass whatever product ids you want as an array to the function. Then do what you want with the attributes.

To use on your template: wpse361339_relevant_products( [ 'product_id_here' ] );

EDIT To get the attributes in the WP_Query(), you can simply do this inside of your while loop:

$product_data = wc_get_product( get_the_ID() );
var_dump( $product_data );

In context:

<?php
$additional_forms_args = array(
    'post_type' => 'product',
    'posts_per_page' => 1,
    'p' => $other_form_product_id
);

$additional_form_query = new WP_Query($additional_forms_args);

if ( $additional_form_query->have_posts() ) :

    while ( $additional_form_query->have_posts() ) : $additional_form_query->the_post();

            $product_data = wc_get_product( get_the_ID() );
            var_dump( $product_data );

    endwhile;

    wp_reset_postdata();

endif;

To get specific product data you can use the methods from: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

Like $product_data->get_title() will get you the title.

EDIT 2 The checkbox data is saved in the product meta, so you get it: $product_data->get_meta();

21
  • Thanks for contributing, are you able to follow the method I have started closer? I don't want it to be a separate function I am calling I am more interested at this stage how it is possible to access a products full attribute list using a WP_Query. I am open to using a query to WC too. Many thanks, J. Mar 25, 2020 at 10:33
  • I sure can! Give me a bit and I'll post more in line with what you have. I personally like the function method because it's transportable throughout your code.
    – disinfor
    Mar 25, 2020 at 14:27
  • I fully appreciate that, it is a very tidy way to build up a set of functions that can be used all over. Don't delete this answer, just expand into another means of achieving the same result. p.s. you open an if with a '{' and close with 'endif;'. I think that may cause an issue. Mar 25, 2020 at 15:40
  • Whoops! I normally don't use the {} hah! Good catch. I fixed the typo, also added the variables/methods to put into your loop (which I'm sure you saw)
    – disinfor
    Mar 25, 2020 at 15:43
  • Neither do I.. I wonder why that is :S I will try the snippet now! Mar 25, 2020 at 15:48
0

You don't really need to use de WP_Query object. The get_posts() function will make it in a much better way. And you can use all the $args that applies to conventional WP posts.

Note the include argument, which includes in the query only the products with given ID.

 $args = array( 'post_type' => 'product', 
            'orderby' => 'date', 'order'=>'DESC', 
            'posts_per_page' => 5,
            'include'=> array(
                'ID' => /*product ID here (int)*/
            ) 
        );
        $product = get_posts( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.