Woocommerce show individual variations on a page - woocommerce

My woocommerce store sells books for which there are often several variations - ebook, audiobook, paperback...
I have these set up as variable products. But I would like to add pages where people can see just a single variation - so, all the audiobooks or all the ebooks. This would be linked from a menu.
Is this possible? Or should I just give up and do each one as a simple product? I know there are paid plugins out there, but my turnover is super small and I don't want to make a loss on my store.
I've picked up some understanding of code, but programming is not my thing. If you are kind enough to post something, I will need a teensy bit of hand-holding. So saying that I need to target the shop loop (or something) will leave me floundering. But saying, here is some code, just change X for your variation name and add the css or post in functions.php will be set me on the right path.
Thank you in advance.

Create new template in your active theme. In my case i named it template-audiobook-products.php - Read more about templates here - https://developer.wordpress.org/themes/template-files-section/page-template-files/
Inside the template add the follwoing code
/*
Template Name: Audio books
*/
get_header('shop');
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'variable',
),
array(
'taxonomy' => 'pa_color', //Change to proper attribute
'field' => 'slug',
'terms' => 'blue', //Change to proper value
),
),
'fields' => 'ids'
);
$get_all_variable_products = get_posts($args);
$product_ids_list = array();
foreach($get_all_variable_products as $parent_variation_id):
$_product = wc_get_product($parent_variation_id);
$variable_product_ids = $_product->get_children();
foreach($variable_product_ids as $variable_product_id):
$_variable_product = wc_get_product($variable_product_id);
$_variable_product_attributes = $_variable_product->get_attributes();
if($_variable_product_attributes['pa_color'] == 'blue'):
$product_ids_list[] = array( 'parent_id'=> $parent_variation_id, 'variation_id' => $variable_product_id);
endif;
endforeach;
endforeach;
woocommerce_product_loop_start();
foreach($product_ids_list as $_product_id):
//Since $product is obj we can pass it and use our default content-product template.
$product = wc_get_product($_product_id['variation_id']);
//Keep in mind this template by default doesnt support ajax (use css to hide button or use woo hook/filters)
wc_get_template_part( 'content', 'product');
//In case we want custom design or ajax button uncomment following.
// echo 'Add to cart';
endforeach;
woocommerce_product_loop_end();
get_footer('shop');
Create new page and select this template
To work properly the standart woocommerce template we need to include the classes and other html tags that we may need in our template. Add this function in your active theme functions.php file.
function my_custom_body_class($classes) {
if(is_page_template('template-audiobook-products.php')) {
$classes[] = 'woocommerce';
}
return $classes;
}
add_filter('body_class','my_custom_body_class');

Related

Wordpress Custom Post Type category used as homepage

I have a Custom Post Type called portfolio and a custom taxonomy portfolio_category that has a term called narrative.
I'm able to access the archive page using the URL /portfolio-category/narrative/.
I want the homepage to display all the items the same as on the narrative archive page without using a redirect.
I've added the following to functions.php
function custom_front_page($wp_query){
if($wp_query->get('page_id')==get_option('page_on_front')){
$wp_query->set('post_type','portfolio');
$wp_query->set('page_id',''); // empty
// fix conditional functions
$wp_query->is_page = false;
$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true;
}
}
add_action('pre_get_posts','custom_front_page');
This is displaying all of the portfolio items on my homepage, but I would like it to be just showing the narrative items.
I've tried this in the php template file, but it isn't working either
<?php
$custom_query_args = array(
'post_type' => 'portfolio',
'portfolio_category' => 'narrative',
);
$custom_query = new WP_Query( $custom_query_args );
?>
How can I get just the narrative items for to show on the homepage?
You're on the right lines but you are not searching by custom taxonomy correctly. Searching by custom taxonomy is different than searching by categories.
If you take a look at the WP_Query documentation for searching by taxomony, you will see that you need to use tax_query to search posts for a custom taxonomy term.
You can use the code below in the template file you use for your homepage. The comments explain what each part does:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 5, // number of results to get, or -1 to get all of them
'tax_query' => array(
array( // important note, this is an array inside the tax_query arrays
'taxonomy' => 'portfolio_category', // your custom taxonomy
'field' => 'slug', // what to search, e.g. slug, term_id, name
'terms' => 'portfolio' // the term(s) to search for. If you want to search for multiple terms, you can use an array
)
)
)
// do a new query with your arguments
$portfolio_query = new WP_Query($args);
// if the query returns any results, loop through them and process them as required
if( $portfolio_query->have_posts() ):
while ( $portfolio_query->have_posts() ) :
$portfolio_query->the_post();
// do stuff with the post here, e.g. display the post...
endwhile;
endif;
// IMPORTANT! The custom query will overwrite the $post values that are used in the the_post etc.
// This restores the the current post in the main query - i.e. your homepage post.
wp_reset_postdata();

WordPress: Taxonomy archives based on Custom Post Type

I've generated three different custom post types (e.g. books, movies, games).
And I've a custom taxonomy for all of them (e.g. genre).
What I need are archives for the taxanomy based on the post types.
For example: "books-genre", "movies-genre"...
Is there any solution to do that? Now I've only the taxonomy archive for "genre".
The way I like to approach custom post archives is to create a custom archive template with WP_Query sections where I need them. You'd create the blank file in the root of your theme at archive-cptnamehere.php.
You might have some template partials to add in but the core of the page looks like this:
<?php
// 1- Get ID of the page's path by URL (reliable)
$pagePath = $_SERVER['REQUEST_URI'];
$pageObject = get_page_by_path($pagePath);
$pagePathID = $pageObject->ID;
// 2- Print page title
$teamPageTitle = get_the_title($pagePathID);
echo $teamPageTitle;
// 3 - Do a query that gets the data you need
// Args: -1 shows all locations, orders locations alphabetically by title, orders locations a-z, only query against team items
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'team',
'meta_query' => array(
array(
'key' => 'team_page_categorization',
'value' => $team_page_categorization_options_array_item,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
// 4- Setup query while loop
if($the_query->have_posts()) {
while($the_query->have_posts()) {
$the_query->the_post();
// 5- Do what you need to inside the loop
// 6- Close it all up, don't forget to reset_postdata so you can do additional queries if necessary!
}
wp_reset_postdata();
}
?>

Wordpress - Add class to subcategories in loop

Im trying to fix a plugin that lists post categories in relation to a restaurant menu. I need to somehow add a class to all subcategories. right now it lists categories and subcategories alike. The loop is here
<ul>
<?php
$menu_types = get_terms( 'rm-menu-type');
//list tabs first
$tab_count = 1;
foreach ( $menu_types as $menu_type ) {
$args = array(
'post_type' => 'rm-menu-entry',
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'rm-menu-type',
'field' => 'slug',
'terms' => $menu_type
)
)
);
?>
<li><?php echo $menu_type->name; ?></li>
<?php
$tab_count++;
}
?>
</ul>
What i am looking for is a way to visually represent what is main, and what is subcategories.
Have you try? With Version 3.0 the taxonomy parameter was added to enable wp_list_categories()
http://codex.wordpress.org/Template_Tags/wp_list_categories#Display_Terms_in_a_custom_taxonomy
Trying to fix the plugin was simply to much hassle, and i decided to redo the functionality with a custom post type and the bootstrap tabs. Works alot better, and will be a lot easier to scale in the future.

Move woo commerce reviews from tabs

I have been trying to move woocommerce reviews. I was able to remove the reviews and replace them outside of the tab, but I am having trouble getting the form for the review to show up.
This is the code I have so far:
function woocommerce_review_list() {
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
}
add_action('woocommerce_after_single_product_summary', 'woocommerce_review_list', 10);
You could try loading WooCommerce's entire comments template:
wc_get_template('single-product-reviews.php');
Or failing that you could simply copy out the relevant parts from that template.

Child_of in Wordpress called with a variable not just returning childs

Situation
I am using the Advanced Custom Fields plugin to give Custom Post Types a checkbox. If this box is checked, it should be returned by this Query. The Query works this far.
The issue
The pages I want it to display are all childs of a page. And this Query returns all pages with the checkbox, instead of just the childs of the current page with the checkbox.
My code
top of page
<?php $tid = get_the_ID(); ?>
Further along the page
<?php
$i == 0;
global $post;
$myposts = new WP_Query(array(
'post_type' => 'keuringen',
'child_of' => $tid,
'meta_query' => array(
array(
'key' => 'first_to_load',
'value' => '1',
'compare' => '=='
)
)
));
?>
The question
What can I do to make it return only the children? Or should this already return only the children and is this a bug in one of the plugins?
Use post_parent instead of child_of in WP_Query

Resources