Woocommerce get parent's description from variation product - woocommerce

I'm trying to get the descritpiton of the parent product from a series of variation.
I tried this way
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC' );
$products = get_posts( $args );
foreach ( $products as $productId ) {
$product = wc_get_product( $productId );
$parent = $product->get_parent_data();
....
where product is my variation. But in parent data i haven't a description.
Any suggestions on how can I get the description?

You can use get_parent_id to get the parent product id. Try the below code.
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC'
);
$products = get_posts( $args );
$productsIds = array(); // store product id for avoid duplication.
foreach ( $products as $variation_id ) {
$variation = wc_get_product($variation_id);
$product = wc_get_product( $variation->get_parent_id() );
if( !in_array( $product->get_id(), $productsIds ) && $product ){
$productsIds[] = $product->get_id();
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
}

Related

Related Products display first same subcategory

Trying to display related products in WooCommerce first by same subcategory and when there are no products to show in that, display another products from parent category, avoiding being empty.
For example:
Fashion > Clothes > T-Shirts
When there are no t-shirts available to show, display another products from parent category (clothes).
Tried it but it is not working:
function wc_related_products_by_last_available_depth_term( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$terms = wp_get_post_terms( $product_id, 'product_cat' );
$hierarchy = array();
$cat_id = '';
// find the depth of terms
foreach ( $terms as $key => $term ) {
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
if( $ancestors && count( $ancestors ) > 1 ) {
$hierarchy[$term->term_id] = max($ancestors);
}elseif( $ancestors ) {
$hierarchy[$term->term_id] = $ancestors[0];
}
$cat_id = $term->term_id;
}
// if level of depth term available replace $cat_id
if( $hierarchy ){
$cat_id = max( array_keys( $hierarchy ) );
}
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'exclude' => array( $product_id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( $cat_id )
)
)
));
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'wc_related_products_by_last_available_depth_term', 99, 3 );

Order posts by Title length in WP_QUERY

I am having problem ordering the posts based on the title length. Here is my code:
<?php
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
?>
<?php
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'primary_category',
'meta_value' => $term->term_id,
);
$eventqry = new WP_Query($eventargs);
?>
How can i sort the posts based on the title length in ascending order.
You can save title length in post meta on save_post hook. and then you can retrieve post order by post meta value.
You can use save_post hook to save the post meta. put this code in your active theme.
//for existing vendors
add_action('admin_init', 'udpate_existing_vendor');
function udpate_existing_vendor(){
$existing_vendor_updated = get_option('existing_vendor_updated', 'no');
if( $existing_vendor_updated == 'no' ){
$vendor_args = array(
'post_type' => 'vendor',
'post_status' => 'publish',
'posts_per_page' => -1
);
$vendors = new WP_Query( $vendor_args );
if( $vendors->have_posts() ) {
while ( $vendors->have_posts() ) { $vendors->the_post();
$length = strlen( get_the_title() );
update_post_meta( get_the_ID(), 'title_length', $length );
} wp_reset_postdata();
}
update_option('existing_vendor_updated', 'yes');
}
}
// for new vendor
function save_vendor_title_length( $post_id ) {
$length = strlen( get_the_title( $post_id ) );
update_post_meta( $post_id, 'title_length', $length );
}
add_action( 'save_post_vendor', 'save_vendor_title_length');
Here your query will look like.
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'title_length'
);
$eventqry = new WP_Query( $eventargs );
}

show low in stock products

for my website, I need a page to display the low in stock products only, as same as choosing to display only top rated, sales, recent products.
I found a code to display the out of stock .. so I just replace "outofstock" value to "lowinstock", but that doesn't work, it shows all of the products
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
Do you have any solutions for this??
I appreciate your help
You could filter the product IDs and check if the amount in stock is lower than the low stock threshold but greater than zero (out of stock).
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
);
$product_ids = get_posts( $args );
foreach ( $product_ids as $key => $product_id ) {
if ( $product = wc_get_product( $product_id ) ) {
$stock = $product->get_stock_quantity();
$low_stock_amount = $product->get_low_stock_amount();
if ( !empty( $stock ) && !empty( $low_stock_amount ) ) {
if ( $stock > $low_stock_amount || $stock < 1 ) {
unset( $product_ids[$key] );
}
} else { //Not enough stock data
unset( $product_ids[$key] );
}
}
}
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}

Querying posts from same categories / tags

I want to find all posts from same categories and same tags like a specific post in a plugin. So I do it actually separately by querying for tags and categories:
$taxonomy_arr = wp_get_post_tags( $this->post->ID, array( "fields" => "ids" ) );
add_filter( 'posts_where', array( $this, 'additional_filter' ) );
foreach ( $taxonomy_arr as $tag_id ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results,
'tag_id' => (int) $tag_id,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
) );
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_taxonomy_selection[] = (int) $post_obj->ID;
}
}
}
}
// get post categories
$category_array = get_the_category( $this->post->ID );
foreach ( $category_array as $category ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results*2,
'category' => $category->cat_ID,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
));
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_category_selection[] = (int) $post_obj->ID;
}
}
}
}
// combine post id's arrays
$all_posts = $local_taxonomy_selection + $local_category_selection;
It's working but is there a way to do it in one query?
This question already has answer on wordpress.stackexchange. So copying same code here.
https://wordpress.stackexchange.com/questions/4201/how-to-query-posts-by-category-and-tag
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;

Wordpress - Allow showing same posts multiple times in one loop

I got an array of Post Ids
$ids = array(1277,6098,6709, 6098);
I want to loop throw these specific posts with:
$args = array( 'orderby' => 'post__in',
'post__in' => $ids);
get_posts($args);
$custom_posts = get_posts($args);
foreach( $custom_posts as $post ) : setup_postdata($post);
the_title();
...
endforeach;
But wordpress automatically excludes the repeated Id (6098). How can I avoid this?
I tryed to create my own function. But unfortunately it doesnt work. I created my own get_posts function like this:
function get_posts_jt($args = null) {
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = $r['include'];
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
I changed the line from:
$incposts = wp_parse_id_list( $r['include'] );
to:
$incposts = $r['include'];
to avoid removing duplicated Ids from array. But this function still doesnt show the duplicated posts from my Id List.
Any Ideas?
You can loop your ids and call get_post with setup_postdata :
global $post;
foreach ($ids as $id) :
$post = get_post($id);
setup_postdata( $post );
the_title();
endforeach;
You can't: get_posts uses wp_parse_id_list to remove duplicate ids from the supplied array.

Resources