I need to show the total amount of products purchased within the simple pdf template, does anyone know how?
get_woocommerce_totals() as $key => $total ) :
$total_qty = 0; foreach ($this->order->get_items() as $item_id => $item) {
$total_qty += $item->get_quantity();
}
echo $total_qty;
?>?>
">
<?php endforeach; ?>
Related
I am using a custom archive-product.php file in my WooCommerce theme. I haven't changed much about the file, just added a few HTML elements for sidebars and such, but the main componants remain untouched.
In my template, I'm trying to get a list of categories with this code, in a sidebar (outside the loop):
$prodCats = get_categories(array('taxonomy'=>'product_cat','exclude'=>'26,482,474','parent'=>0));
if (!empty($prodCats)) {
//echo '<center><strong>Jump to Category</strong></center>';
echo '<ul>';
foreach ($prodCats as $cat) {
// get the thumbnail id using the queried category term_id
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
echo '<li>';
echo '';
echo '<div class="catName">'.$cat->name.'</div>';
echo '<img src="'.$image.'"/>';
echo '</li>';
}
echo '</ul>';
}
But, this outputs only the product categories that are part of the current WooCommerce loop. It will not get all the product categories. It seems to somehow be tied to the WC loop on that page instead of operating independantly.
Is this intended behavior on a custom post type archive or specifically on WooCommerce?
If not, what might I look for to solve this?
This code, which does a similar thing on a woocommerce Product Attribute (pa_brand) is also affected. Note that I'm using new WP_Term_Query here to see if that solves it, but it still only pulls the terms that are in the current WC loop.
//Get brand list and display it
function ppp_get_brand_list() {
?>
<div id="brandList">
<center><h4>Brands</h4></center>
<?php
$brandArgs = array(
'taxonomy' => 'pa_brand',
'hide_empty' => true,
);
$brands = new WP_Term_Query( $brandArgs );
echo '<ul>';
foreach ( $brands->terms as $brand ) {
$brand_image_id = get_term_meta( $brand->term_id, 'product_search_image_id', true );
$brand_image = wp_get_attachment_url( $brand_image_id );
$brand_link = get_term_link($brand->term_id,'pa_brand');
echo '<li><img src="'.$brand_image.'" alt="" /></li>';
}
echo '</ul>';
?>
</div>
<?php
}
I need help creating a wp shortcode that shows a list of users (by role) with a check if they have commented on a certain post. Post ID and user role are shortocode parameters.
In order to do this I thought of merging this handmade shortcode ( it shows for a specified user if whether or not commented on a post, [fancyshortcode user_id="" post_id=""] ):
function fancycheck( $attributes ) {
// Things that you want to do.
$args = array(
'user_id' => get_the_ID(),
'post_id' => get_the_ID()
);
if( isset( $attributes['post_id'] ) ){
$args['post_id'] = absint($attributes['post_id']);
}
if( isset( $attributes['user_id'] ) ){
$args['user_id'] = absint($attributes['user_id']);
}
$comment = get_comments($args);
if(!empty($comment)){
return '<div class="ccount">YES</div>';
}
else {
return '<div class="ccountniet">NO</div>';
}
return "";
}
// register shortcode
add_shortcode('fancyshortcode', 'fancycheck');
with a shortcode to show users list... something like this:
function wcr_list_users($atts) {
// I don't use shortcode_atts because I want to have a dynamic number of attributes
$query = new WP_User_Query($atts);
$results = $query->get_results();
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?></li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('list_users', 'wcr_list_users');
but it is a too complicated task for my skills. So I tried to simplify the shortcode just indicating for each user the number of comments left for a particular post.. something like this:
function listone($atts) {
$query = new WP_User_Query($atts); $results = $query->get_results();
$args = array( 'post_id' => get_the_ID() );
$comments = get_comments('post_id');
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?><br />
Comments: <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?>
</li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('listonesc', 'listone');
but obviously this <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?> doesn't work.
Can anyone help me or give me directions on which way to go? Thank you
I am working on a category. I have to show all the category list on-page. So I tried the below code and it's working. I am getting my all the category list.
Below is the output of my category list
My question is, Where I go and add the category list code?
I have to access the URL like http://test.com/category/ so it will display my category list.
I created the category.php page but that page is displaying the post with the related category.
For example http://test.com/category/destination so it will display all the post which is related to the destination.
<?php
/**
* A Simple Category Template
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div class="CategoryHero">
<?php
// Get the current queried object
$term = get_queried_object();
$term_id = ( isset( $term->term_id ) ) ? (int) $term->term_id : 0;
$categories = get_categories( array(
'taxonomy' => 'category',
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
?>
<ul>
<?php
foreach ( $categories as $category )
{
$cat_ID = (int) $category->term_id;
$category_name = $category->name;
// When viewing a particular category, give it an [active] class
$cat_class = ( $cat_ID == $term_id ) ? 'active' : 'not-active';
// I don't like showing the [uncategoirzed] category
if ( strtolower( $category_name ) != 'uncategorized' )
{
printf('<li>' . $category->name . '</li>');
}
}
?>
</ul>
</div>
</main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Would you help me out with this issue?
You can create a separate category(taxonomy) page on WordPress by taxonomy-{tax-name}.php and put this code in that file.
Please let me know if it helps you to display the following data on a separate page.
Regards
I have more than 800 categories, because 1 post may have more more than 10. I want to display only X categories on front page. How I can do this?
$id = get_the_ID();
$cats = wp_get_post_categories($id);
$i=0;
foreach ( $cats as $cat ){
if($i <10){
//Dosomething with $cat
}
$i++;
}
By setting a counter you could show desired categories
I've created a custom post type in WordPress, and in the single post template, I'm successfully pulling in my custom data using the get_post_custom() function.
But inside The Loop on the list of posts, the function doesn't work and comes back with an empty array.
Here's what I have:
<?php $loop = new WP_Query( array( 'post_type' => 'web-design', 'posts_per_page' => 10 ) ); ?>
<?php $i = 0; ?>
<?php while ( $loop->have_posts() && $i < 3 ) { $loop->the_post(); ?>
<article class="project-link <?php echo 'num' . $i ?>">
<div class="pad">
<?php $project_info = get_post_custom(); ?>
<?php
foreach ($project_info as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print "$key => $value\n";
}
print "}\n";
}
?>
<?php echo $project_info['url'][0]; ?>
And I don't have anything coming back at all.
Does anyone know why this doesn't work? It works fine in the single post template, why not in the loop?
Thanks!
the post_custom() has many variables stored in arrays ..
if you know the specific key , or value that you need you can use
or just itirate through them like this :
<?php
$mykey_values = get_post_custom_values('my_key');
foreach ( $mykey_values as $key => $value ) {
echo "$key => $value ('my_key')<br />";
}
?>
or for a specific post
<?php
$custom_fields = get_post_custom(72);
$my_custom_field = $custom_fields['my_custom_field'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>
It seems as though the problem lies with the database. I noticed that if I deleted certain posts, then it would start working fine. I'm yet to find out what's wrong with these posts though, if I delete them and then re-create them, they're usually fine.
My advice to anyone with this problem is try moving some of your posts to the trash and see if it starts working.