I'm looking for a way to show random WooCommerce products on a page. This is nothing to do with "featured products" just random from any category.
I've been looking but cant seem to find any plugin or script to do this? Does anyone have an idea how to do this?
Alright folks here is a bit of code I am using for mine its for recent products but is doing the job. Just add to page you want to show them on.
[recent_products per_page="4" columns="4" orderby="rand" order="rand"]
Try this.
Paste code into functions.php
Go to wp-admin/ Woocommerce > Settings > Products > Display
View settings drop down order by random will be a new option. *note: it will be the the last option.
// Shop random order. View settings drop down order by Woocommerce > Settings > Products > Display
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'random_list' == $orderby_value ) {
$args['orderby'] = 'rand';
$args['order'] = '';
$args['meta_key'] = '';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['random_list'] = 'Random';
return $sortby;
}
You can try it. let post it in function.php
add_filter('woocommerce_get_catalog_ordering_args', 'set_sort_order');
function set_sort_order($args) {
$args['orderby'] = 'rand';
return ($args);
}
This works for me:
<?php
global $post; // setup_postdata will not work without this being set (outside of the foreach loop)
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();
?>
This was the code I used [featured_products per_page="8" columns="4" orderby="rand"]
I just used this [products limit="8" columns="4" orderby="rand" order="rand" visibility="visible"].and it works as expected
If you would like to use code to make the shop homepage sort by random, then use the following code. All you need to do is install a plugin called "code snippets" that allows you to make changes in WordPress without changing any core files.
add_action( 'pre_get_posts', 'shop_default_orderby_rand' );
function shop_default_orderby_rand( $query ) {
if ( is_shop() && ( ! isset($_GET['orderby']) || 'menu_order' === $_GET['orderby'] ) ) {
$query->set( 'orderby', 'rand' );
}
}
Related
i'm trying to make a pluging that show a custom category list and i'm using
function page_template( $template )
{
if ( get_post_type( get_the_ID() ) == 'gallery' ) {
$template = plugin_dir_path(__FILE__) . '/custom-page-template.php';
}
return $template;
}
add_filter( 'template_include', 'page_template' );
and is working good, now i get the url from each category using
<?php
$value = term_exists( 'category', 'taxonomy' ); // array is returned if taxonomy is given
if(isset($value)){?>
<a href="<?php echo get_term_link( 'category', taxonomy ) ?>" > Blepharoplasty</a>
<?php } ?>
and too working working good.
now i need know how add a new template when i do click in the url that obtain with the get_term_link().
Thanks
I'm using WooCommerce with WordPress to build a store of Antique Photos.
I'm using the WooCommerce product attributes feature to store information about an item photographer.
See here for an example, photographer attribute is pulled out in the box on the left http://bit.ly/1Dp999O
The code that pulls out the attribute looks like this:
if($attribute['is_taxonomy']) {
$values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));
echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values);
}
$values looks like this:
Array ( [0] => Photographer 1 )
The problem is, how do I get to the permalink for the Photographer which is automatically generated by WordPress and WooCommerce:
http://bit.ly/1JtwBna
I can't find any documentation for this within WooCommerce, and this seems to be a taxonomy within a taxonomy which goes a bit further than stabdard WordPress, but I'm assuming this is a fairly standard requirement. Any pointers appreciated.
Once you have the attribute term (in this case the photographer's name) you can use get_term_link() to get the URL. Because the $product id isn't passed to the woocommerce_attribute folder, I couldn't filter that, but instead create an override of the product-attributes.php template. And modified the relevant section to be the following:
if ( $attribute['is_taxonomy'] ) {
$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
$html = '';
$counter = 1;
$total = count( $terms );
foreach( $terms as $term ){
$html .= sprintf( '%s',
esc_url( get_term_link( $term ) ),
esc_attr( $term->name ),
wptexturize( $term->name )
);
if( $counter < $total ){
$html .= ', ';
}
$counter++;
}
echo wpautop( $html );
}
For some reason, the URL isn't a pretty permalink. It's late and I can't tell if this has to do with my configuration or what exactly, but it is a start.
This basically does what I require:
$terms = get_the_terms($product->id, $attribute['name']);
foreach ($terms as $term){
echo ''.$term->name.'';
}
But could do with a bit of refinement for sure.
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.
On my blog's archive page if I click on a month, it takes me to a page showing me all the posts that I've created that month (obviously). Is there a way to filter that page so it only shows me posts from one of my categories?
archive.php
<?php if ( have_posts() ) : ?>
<div class="rightColumn">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div>
<?php
get_footer();
Thanks.
I ended up finding a solution that worked for me:
function only_show_blog_posts( $query ) {
// Only modify the main loop query
// on the front end
if ( $query->is_main_query() && ! is_admin() ) {
// Only modify date-based archives
if ( is_date() ) {
// Only display posts from category ID 1
$query->set( 'cat', '12' );
}
}
}
add_action( 'pre_get_posts', 'only_show_blog_posts' );
try using the pre_get_posts hook, something along the lines of:
function filter_by_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() && basename( $_SERVER['PHP_SELF'] ) == 'archive.php' ) {
$category_id = get_cat_ID( 'THE_CATEGORY_NAME' ); //change to the actual name of the category you are filtering with
$query->set( 'cat', $category_id );
}
}
add_action( 'pre_get_posts', 'filter_by_category' );
you can drop this code into your functions.php file
you can find more info about the pre_get_posts hook here
That simple hook helped me too. I modified the function a little to get the category Id from $_GET:
function only_show_blog_posts( $query ) {
if ( $query->is_main_query() && ! is_admin() ) {
$catId = (int)$_GET['catId'];
if ( is_date() && is_int($catId))
$query->set( 'cat', $catId);
}
}
No filters or hooks necessary. Just pass the category you want to filter for in the URL.
With an ID
https://myblog.com/2018/?cat=1234
With a slug
https://myblog.com/2018/?category_name=my-category-slug
I search all the web for that answer. I use wp_list_categories to make a submenu with custom taxonomy, It works well, and puts current-cat when I browse those categories.
The thing is, when I browse single posts with this menu, the highlight no more works.
For the blog part of that site, I use the following code to highlight current category on wp_list_categories():
function sgr_show_current_cat_on_single($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');
But as far as I tried, can't make it work for single posts that are ordered by custom taxonomy. :/ > I don't know how to custom it.
Is it even possible ?
You need to use get_the_terms( $id, $taxonomy ); instead of wp_get_post_categories(); for getting custom taxonomy term IDs.
You can hardcode taxonomy name into the functon or get it from the $args you passed into wp_list_categories( $args );.
Final code:
add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
if ( is_single() ) :
global $post;
$terms = get_the_terms( $post->ID, $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
$output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
}
}
endif;
return $output;
}