Im having problems with retrieving product infos trough object functions in Woocommerce.
This is how I do:
public function table_data()
{
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$products = get_posts( $args );
$pfactory = new WC_Product_Factory();
foreach($products as $product)
{
$_product = $pfactory->get_product($product);
}
}
This returns product informations from wp_posts only, it won't give me the information stored in wp_postmeta.
I need the full information from all products (such as price, sku, stock etc.) in one array, but I seem to be missing something and Im unsure if it has with the hierarchy of the functions in my code. However I thought this was possible to do without SQL-queries.
Basicly, what im trying to do is a complete duplicate of product list in admin for listing products with information in the panel.
Thanks for all the help I can get.
Use WP_Query instead, which will give you access to the global WooCommerce $product variable from within the loop. From there, you can grab the price, sku, stock and all other kinds of data. http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post();
global $product;
$price = $product->get_price_html();
$sku = $product->get_sku();
$stock = $product->get_stock_quantity();
endwhile; endif; wp_reset_postdata();
?>
First thing I came up with was this.
function products() {
return array_map('wc_get_product', get_posts(['post_type'=>'product','nopaging'=>true]));
}
I'd love to know from someone more experienced with WooCommerce if there is a better way
As of WooCommerce 3 you can do it using the wc_get_products function like this:
$args = array(
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'limit' => -1,
);
$products = wc_get_products($args);
if (count($products) > 0) {
foreach ($products as $product) {
echo $product->get_id() . '<br>';
echo $product->get_name() . '<br>';
echo $product->get_price_html() . '<br>';
echo $product->get_sku() . '<br>';
echo $product->get_stock_quantity() . '<br>';
}
}
Related
I've got some code (see below) from an online tutorial that displays an alphabetical list of Category names and then,
underneath each category, a list of post titles for that category.
It works, but I want the post titles to also be displayed alphabetically. At present it's only category names that are alphabetical - see image:
I've done some research online and I think I may need to set up a 'nested loop' - but I have no idea how to edit my code to do this.
Hoping someone can show me how to edit code to get both category names AND post titles to display alphabetically.
This is the code I'm using:
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
We can add the orderby argument with title.
Maybe this questions already asked but that not helpful for my question.
Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.
I get products list using this code:
android/all_products.php
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
I got all products, but i want to show products with pagination.
Note: all the API's are written in inside android folder.
Thanks in advance.
below pagination code is to show next products you can also do it to show previous product by applying same method
if(isset( $_GET['page_num'] ) ) {
$page_number = $_GET['page_num'];
} else {
$page_number = 2;
}
$args = array (
'limit' => 3,
'page' => $page_number,
);
$products = wc_get_products( $args );
foreach( $products as $product ) {`enter code here`
//your data here
// e.g $product_name = $product->get_name();
}
Next
If you want to create API's then just use Woocommerce rest API's which are built in function given in wordpress please use below url it will give you list of products with lots of options:
{{your_url}}/wp-json/wc/v2/products
Go to wp-admin and go to Woocommerce -> settings -> API
-> Enable rest API
-> Go to Key/Apps and create Auth Keys both secret and Private (Copy that keys)
Then give this above url to add it for use in application with o_auth of secret key and private key.
Please see this link for all woocommerce API's : https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product
Finally i creating a code and send the date using API.
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$brand_product_list = new WP_Query( $args);
$pagination_count = 1;
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$pagination_count++;
endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();
?>
<?php
$pagination = round($pagination_count/12);
for($i=1;$i<=$pagination;$i++)
{
$pagination_no[] = $i;
?>
<!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
<?php } ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$product_data = wc_get_product( $post->ID );
if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
{
$img = get_the_post_thumbnail_url($product_data->get_id());
}
else
{
$img = "";
}
$product_list[] = array(
'product_id' => $product_data->get_id(),
'product_name' => $product_data->get_title(),
'product_regular_price' => $product_data->get_regular_price(),
'product_sale_price' => $product_data->get_sale_price(),
'product_price' => $product_data->get_price(),
'img' => $img,
'rating' => $product_data->get_average_rating(),
'stock_quantity' => $product_data->get_stock_quantity(),
'stock' => $product_data->is_in_stock(),
);
endwhile; wp_reset_query();
$data[] = array(
'pagination' => $pagination_no,
'product_list' => $product_list
);
//echo json_encode($peoduct_list, $pagination)
echo json_encode($data)
?>
I am using the recent products shortcode for the Woocommerce plugin and it returns the product image, title and price. All of my products have more attributes which I would like to be returned as well. How can I achieve this?
I would create a custom loop like this:
https://www.philowen.co/blog/show-latest-woocommerce-products-in-your-template/
Either create it as a custom shortcode or action depending on where your planning to pull this in and have the attributes pulled in using products class within the loop:
<?php
$product = new WC_Product;
$attributes = $product->get_attributes();
foreach loop...
?>
Something like:
<?php
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 4,
'orderby' =>'date',
'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
global $product;
// Get the attibutes
$attributes = $product->get_attributes();
// Loop and display the value
// var_dump $attributes to see what you can output
foreach ($attributes as $attribute) {
echo $attribute['value'];
}
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like to set the SKU to the ID of the product.. but cannot find the right filter to use?
Is there a filter that sends the product SKU?
add_filter( 'woocommerce_get_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
//set $newsku as product id
return $newsku;
}
How do i accomplish this? I have 1000s of products without a SKU. I've tried a plugin called SKU generator but that doesn't work. Any help appreciated.
You can do it by adding following code to your functions.php file and after adding go to you site example.com for only once.
After that remove below code from the functions.php.
add_filter( 'init', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$random_sku = mt_rand(100000, 999999);
update_post_meta($loop->post->ID,'_sku',$random_sku);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
NOTE : If you don't remove after one use, products SKU will change everytime you visit any page of your site.
Add it as shortcode and make SKU same as Product id.
add_shortcode( 'update_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta($loop->post->ID,'_sku',$loop->post->ID);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
Then use this shortcode in a page and open it once. so it will not re-create sku's every time.
In case: If I want to display a popular post in a category post. So, when I open "XXX" or "YYY" category, will display a popular post first from "XXX" or "YYY" category.
The question title is a bit confusing. there is a way of getting the "popular" posts by comment count but that "by view" in the title of your question suggest you are looking for a different way to go about it?
.
if you want to check post popularity by post views...
First you need to attach a "views count" to each post.
a complete function here: catWhoCodes
Now that you got a way to check which posts are popular
you need to create a list of post related to the current
category but filtered by the post count... here is an easy
way to go about it.
<?php
$category = get_category( get_query_var( 'cat' ) );
$curCatId = $category->cat_ID;
$args = array(
'numberposts' => 10,
'cat' => $curCatId,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$popPosts = get_posts( $args );
echo '<ul>';
foreach ( $popPosts as $popPost ) {
setup_postdata( $popPost );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
wp_reset_postdata();
?>
.
To get popular posts by comments count
<?php
$category = get_category( get_query_var( 'cat' ) );
$curCatId = $category->cat_ID;
$args = array(
'numberposts' => 10,
'cat' => $curCatId,
'orderby' => 'comment_count'
);
$popPosts = get_posts( $args );
echo '<ul>';
foreach ( $popPosts as $popPost ) {
setup_postdata( $popPost );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
wp_reset_postdata();
?>
.
Related:
How to list most popular post
.
Best of luck,
Sagive.