How can I use wp_query to show all product data using just the products ID?

Question

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.

0
, , , Jason Is My Name 4 years 2020-03-25T12:51:04-05:00 0 Answers 125 views 0

Leave an answer

Browse
Browse