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.
Related
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');
For fast loading in mobile view i want to remove woocommerce related products, what is the code to be added in function.php?
and I do not want to use CSS, thankyou.
The function woocommerce_output_related_products is pluggable. So you can just re-write it.
See: WooCommerce Code Reference
Include this in your functions.php
function woocommerce_output_related_products() {
// Check if mobile and bail if so.
if (wp_is_mobile()) return;
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand', // #codingStandardsIgnoreLine.
);
woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
}
You can use wordpress inbuilt function - wp_is_mobile()
https://developer.wordpress.org/reference/functions/wp_is_mobile/
your complete code will be-
if (wp_is_mobile()) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
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();
I have a Wordpress plugin which I've named 'Solve Maths'. I want this plugin name to be displayed only at the Admin header of the Arthur alone for easy access.
I used the admin_head action hook but once I installed the plugin, the system tells me my plugin has generated these number of characters at the header.
<?php function Solve_Maths{ echo "<a href='Solve_Maths.php'>Solve Maths</a>";} add_action('admin_head','Solve_Maths');?>
The Solve_Maths.php is the name of the main plugin file with the header information. I want this file name in the tag to be shown at the admin header of the user and should execute the file when the link is linked. Thank you all for your help.
This is not the correct way to do it. Please check the WordPress Codex and refer to the function add_node: https://codex.wordpress.org/Function_Reference/add_node
add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );
function toolbar_link_to_mypage( $wp_admin_bar ) {
$args = array(
'id' => 'my_page',
'title' => 'My Page',
'href' => 'http://example.com/my-page/',
'meta' => array( 'class' => 'my-toolbar-page' )
);
$wp_admin_bar->add_node( $args );
}
i have created a custom taxonomy named 'singer' using pods plugin and inside that plugin i have defined a label named 'details'. what i want to do is to generate a short code in which returns that details. i have searched my way through lots of documentation, but could not find one.image showing the custom field i have added inside the taxonomy
Thanks for helping!
Do you need it to be that complicated? (Meaning, do you need a plugin?)
Register a new taxonomy in your functions "singer":
function taxonomies_init() {
// create a new taxonomy
register_taxonomy(
'singer',
'post',
array(
'label' => __( 'Singer' ),
'rewrite' => array( 'slug' => 'singer' ),
)
);
}
add_action( 'init', 'taxonomies_init' );
Register the shortcode:
function showtax_func( $atts ) {
if (is_single()) {
$a = shortcode_atts( array(
'tax' => '',
), $atts );
$termname = get_the_terms(get_the_ID(),$a['tax'])[0]->name;
return $termname;
}
}
add_shortcode( 'show_tax', 'showtax_func' );
Use the shortcode like this: [show_tax tax="singer"]
You can add more taxonomies by expanding the first function by duplicating the register_taxonomy() function. And get any taxonomy with the shortcode by just changing the value of the taxonomy name.