I am trying to create a query that displays products and some information about them.
I am stuck on showing their variable product options (they will all be variable products). After this I have to create an add to cart button that will add to cart, produce a success or error message, and let the user continue shopping. Once there is something in the user’s cart I would like to then display a checkout button that will take them to the checkout page.
Here is what I’ve got so far
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
// Indicate the product category - in this case "training"
'product_cat' => 'training'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
// Show the product Featured Image and gallery images
woocommerce_show_product_images();
// Show the product Title
echo the_title();
// Show the product Custom Fields
echo get_post_meta($post->ID, 'Description', true);
echo get_post_meta($post->ID, 'Specifications', true);
echo get_post_meta($post->ID, 'Video', true);
// Show the Product Price
echo $product->get_price_html();
// Show the Variable Product options, for example: Color, Size etc
// stuck here!
// Show the Add to cart button
// stuck here!
// Show the Checkout button if there is an item in the users cart
// stuck here!
endwhile;
}
wp_reset_postdata();
?>
Does anyone know the functions I should be using to display this information?
I’m having a hard time finding anything about it in the documentation / on google.
Thank you all!!!
Woocommerce by default comes with the variable product option. FOr this you will first have to create attributes. You can create global if all products have same attributes or can create attributes for each product individually.
Here is a screenshot of a website i am working for. We have 4 different packages. So my attribute is “packages”, and we have 4 packages in it.
Once the attributes are saved goto the next tab called ‘variations’. Choose packages attribute and it will automatically load all the variations. Here is another screenshot. You will have to choose a default variation.
Once all the above changes are made you can then click on any variation and change the product parameters like price, image etc.
Variations can be used on products like shirts, where you can have different colored shirts and although the price will be same for the shirts but you can give user the ability to choose colors and see how the shirt looks like in that color.
Although woocommerce have setup everything by default but if you are changing the default wordpress template, then the best way to do is to copy the templates folder from woocommerce and place this folder in your active theme under the name of woocommerce, so it would be something like, wp-content/themes/theme-name/woocommerce. And inside these you need all the files and folders that were in the templates folder of the woocommerce. Woocommerce will automatically pick all these files and will serve it. This method is recommended by woocommerce as making direct changes to the plugins will loose all your customizations on the next plugin update.
EDIT For your situation : Sorry i think i got carried away :p. Anyways it depends on the situtation you are in and the file that you are using to get these values but, the code below will output all the product variations and all the attributes within that variation. I haven’t tested it out, might need minor adjustments. **
Low and behold.. The solution was so simple.
All I had to have inside the loop was:
Now my loop looks like this
Works fine, and to style the product info I simply created child version of:
– content-single-product.php
– product-images.php
– variable.php
– meta.php
– product-thumbnails.php
and edited til’ my heart was content.
I used some of Omer Farooq’s code. Thank you Omer! This will give you a full dropdown menu of the attribute of your choice. In this case, I am using pa_color. I have also removed duplicates so that it will only return the set of colors. This should be used inside of a product loop. In my case, I am using WP_Query to query products.