WooCommerce view all products with variations - php

I have a WooCommerce shop that holds variable products.
I want to create some sort of export (on a page) that shows me all products including variations in a table.
I am viewing all products and created a loop to get the variations but it only shows me one product variation.
<?php
/*
Template Name: Store Management
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
// Get
$args = array(
'post_type' => 'product',
'numberposts' => -1,
);
$products = get_posts( $args );
echo '<pre>';
print_r($products);
echo '</pre>';
foreach($products as $product):
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$product = wc_get_product( $product->ID );
$variations = $product->get_available_variations();
echo '<pre>';
print_r($variations);
echo '</pre>';
endforeach;
?>
Can anyone tell me how to get all variations for all products?
M.

Here is my code, to get all products and variations in WooCommerce v3+ :
<?php
$args = [
'status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$all_products = wc_get_products($args);
foreach ($all_products as $key => $product) {
echo $product->get_title();
if ($product->get_type() == "variable") {
foreach ($product->get_variation_attributes() as $variations) {
foreach ($variations as $variation) {
echo $product->get_title() . " - " . $variation;
}
}
}
}

First of all don't override the product variable inside loop. Secondly you have to check weather product is simple or variable. Because simple product will not have variants. So your code will be like this :
foreach($products as $product):
$product_s = wc_get_product( $product->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
echo '</pre>';
}
endforeach;

Related

Get product color variable name and id

so I got a working shortcode that outputs the selected product attribute name and id alongside, however, I have no idea how to obtain the same product variable name and id which is essentially from the same attribute.
This means that my current product form URL slug cannot add to cart the selected variable e.g. ( ?add-to-cart=18395&quantity=1&?variation_id=407 ) because the variation id is fetched from the attribute, and it is wrong I know.
add_shortcode ('color_attribute', function (){
$color_attributes = get_the_terms($post->ID, 'pa_pla_color', array(
'hide_empty' => false,
));
foreach ($color_attributes as $k => $color_attribute):
if( $k > 0 ) echo "\n";
echo $color_attribute->name . ' | ' . $color_attribute->term_id;
endforeach;
} );
Any help is really appreciated!
Check this out
add_shortcode ('color_attribute', function (){
$color_attributes = get_the_terms($post->ID, 'pa_pla_color', array(
'hide_empty' => false,
));
$product = wc_get_product( $post->ID );
echo $product->get_name();
// Get Product Variations and Attributes
$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute( 'attributeid' ); //get specific attribute value
foreach ($color_attributes as $k => $color_attribute):
if( $k > 0 ) echo "\n";
echo $color_attribute->name . ' | ' . $color_attribute->term_id;
endforeach;
});
For anyone else looking for similar solutions, this is how I achieved to solve my problem:
add_shortcode ('color_attribute', function (){
$color_attributes = get_the_terms($post->ID, 'pa_pla_color', array(
'hide_empty' => false,
));
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => get_the_ID() // get parent post-ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
if( $variation_ID > 0 ) echo "\n";
// get variation ID
$variation_ID = $variation->ID;
$product_variation = new WC_Product_Variation( $variation_ID );
$variation_attribute = $product_variation->get_attribute('pa_pla_color');
echo $variation_attribute . ' | ' . $variation_ID;
};
});

Display with a shortcode a dropdown of Woocommerce products for a specific category

i want to display product by category with dropdown
and i made this code but why the value not going inside the ?
function rakitpc() {
$args = array(
'posts_per_page' => -1,
'product_cat' => 'Tshirts',
'hide_empty' => 0,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'title',
'echo' => '0'
);
$products = new WP_Query( $args );
$output = '<select>';
// foreach ( $products as $product ) {
while ( $products->have_posts() ) {
$products->the_post();
$aa = the_permalink($post);
echo $aa;
$bb = the_title($post);
$output .= "<option value=\"<?php the_permalink(); ?>\"> <?php the_permalink(); ?></option>";
}
wp_reset_query();
$output .='</select>';
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
This is the output That I get:
My reference: How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2
Try the following that will give you a dropdown with the permalinks as <option> values displaying the product names (see the screenshot at the end).
You will need to set your category name (or categories names) in the array inside the funtion.
function rakitpc() {
// HERE below, define your Product category names
$term_names = array('T-shirts');
// The WP_Query
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'hide_empty' => 0,
'orderby' => 'title',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // for a Product category (or 'product_tag' for a Product tag)
'field' => 'name', // can be 'name', 'slug' or 'term_id'
'terms' => $term_names,
) ),
'echo' => '0'
) );
$output = '<select>';
// foreach ( $products as $product ) {
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$permalink = get_permalink($query->post->ID);
$title = $query->post->post_title;
$output .= '<option value="' . $permalink . '">' . $title . '</option>';
endwhile;
wp_reset_postdata();
$output .='</select>';
else :
$output = '<p>No products found<p>';
endif;
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Show the variation label (Woocommerce)

I'm using this slightly modified script (inside a loop) which helps me generate two links which I use to add products to the cart:
<?php
// Get the ID of the product
$id = $_POST["post_var"];
// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'publish' ),
'numberposts' => 2,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $product->id
);
$variations = get_posts( $args );
$loop = 0;
if ( $variations ) {
foreach ( $variations as $variation ) {
$formats[$loop]["id"] = absint( $variation->ID );
$formats[$loop]["data"] = get_post_meta( $variation->ID );
$variation_label = "?????????";
$loop++;
}
foreach ( $formats as $type )
$response .= '' . $variation_label . '';
} ?>
<?php echo $response; ?>
The only thing I can't figure out is how to get the variation labels.
Every product has variation called "Format' with the options "Electronic" and "Physical" and I need those to be output into the link. So the $response would look something like this:
<a href="http://example.com/checkout?add-to-cart=93&variation_id=94&quantity=1&attribute_pa_format=Electronic">Electronic</a
Physical
You can see I've added a variable called $variation_label as a placeholder.
Stumped.

wordpress - php - Get name and slug from category

I've got this function:
<?php
$categories = []; //array to store all of your category slugs
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->slug) ){
array_push($categories, $category_list_item->slug);
}
}
foreach ($categories as $category) {
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => $category, 'orderby' => 'rand' );
?>
This is good, it returns the slug of a category for me in a loop. The problem is, now I can't get the category name anymore. I've tried adding another array: '$names = [];' and with that copied the if statement in which I replaced slug with name. But that didn't take. Out all of the solutions that i've tried, I only get returned 'Array' of just the whole list of names. Does anyone have a solution?
Thanks!
<?php
$categories = array();
$names = array();
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->slug) ){
$categories[] = $category_list_item->slug;
$names[] = $category_list_item->name;
}
}
foreach ($categories as $key=>$category) {
echo "Category Name :".$names[$key];
echo "Category Slug :".$category;
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => $category, 'orderby' => 'rand' );
?>

woocommerce buyers emails on frontend

i have created Website using Wordpress and WooCommerce Plugin, and i have success making users post products from frontend,
i want to display product orders
as you can see in this image
i success showing total sales for the product,
now i want to show all buyers infomations for the product/
so far i have got this code
<?php
$p = $post->ID;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
//print_r($customer_orders);
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
$fields = array_values($orderdata);
//print_r($fields);
echo 'Status: '.$fields[1];
echo '<br>Date : '.$fields[2];
echo '<br>Email : '.$fields[16];
}
?>
This Code is Working Fine But it Show details about all products
What i Want is: to show informations about about product depending on product ID
so i want to edit this code to get results depending on post->id
$p = $post->ID;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
Ok, so reading your question and assuming $post->ID is the id of the product you want to display the orders containing, this is what you need:
<?php
$products = array();
foreach (get_posts('post_type=shop_order&numberposts=-1&post_status=publish') as $order) {
$order = new WC_Order($order->ID);
foreach($order->get_items('line_item') as $item) {
$product_id = (!empty($item['variation_id'])) ? $item['variation_id'] : $item['product_id'];
$products[] = $product_id;
}
if (in_array($post->ID,$products)) {
echo 'Status: '.$order->order_status;
echo '<br>Date : '.$order->order_date;
echo '<br>Email : '.$order->billing_email;
}
}
If I understood your question well, maybe you can try this. But you have to make sure that $post is refering to the order.
$p = $post->ID;
$args = array(
'p' => $p,
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
if ( $my_query->have_posts() ) {
$my_query->next_post();
$customer_order = $my_query->post;
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
$fields = array_values($orderdata);
//print_r($fields);
echo 'Status: '.$fields[1];
echo '<br>Date : '.$fields[2];
echo '<br>Email : '.$fields[16];
}
You can read the doc of the WP_Query() function here : http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

Categories