Show WooCommerce Parent Attributes Only - woocommerce

I'm trying to create 3 rows of buttons to select. Think sorting boxes. AT&T has it on this page. Where you see iPhone, Samsung and then sorting. I'm trying to do the same thing. AT&T's example page
The first row will be sub categories of parent categories. That part I was successful in completing. However, I'm struggling to call to all the parent attributes.
<?php
global $product;
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
$product_attributes = array_keys($product_attribute);
$count = 0;
echo '<a class="archive-attributes" href="#">';
foreach ($product_attributes as $product_attribute){
$attribute_name = ucfirst( str_replace('pa_', '',
$product_attributes) );
echo $attribute_name . '';
if($count != count($product_attribute)) echo '';
else echo '';
}
echo '</a>';
}
?>
I only need the parent for this row. When one of these is clicked the children of that attribute will display below it. What am I doing wrong?

Related

How display the spell out of an amount with currency details in WooCommerce

I am trying to spell out WooCommerce order total amount, in my invoice.php template file can reach order total amount.
First I tried:
$total = $order->get_total();
<?php echo ( $total ); ?> -
<?php
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($total); ?>
The order total displayed is 225.00 and the spell out display is: two hundred twenty-five
Edit:
I found the following solution:
<?php $number = $order->get_total() ;
$formatter = new NumberFormatter('tr', NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
echo $formatter->format($number); ?>
But the result shows like that: two hundred twenty-five
The desired display should be: two hundred twenty-five turkish liras , zero penny.
How can i do this ?
The NumberFormatter SPELLOUT constant doesn't handle the decimals.
You can use the following custom function to display a float number amount spell out like (with the currency details):
function wc_spellout_amount( $amount, $country_code = 'tr' ) {
$formatter = new NumberFormatter($country_code, NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
$amounts = explode('.', (string) $amount); // Separating decimals from amount
$output = $formatter->format($amounts[0]);
$output .= ' ' . _n('turkish lira', 'turkish liras', $amounts[0], 'woocommerce');
$output .= ', ' . $formatter->format($amounts[1]);
$output .= ' ' . _n('penny', 'pennies', ( $amounts[1] > 0 ? $amounts[1] : 1 ), 'woocommerce');
return $output;
}
Code goes in functions.php file of the active child theme (or active theme).
Usage: Then you will use it as follows in your code:
echo wc_spellout_amount( $order->get_total() );
Tested and works.

Best way to do this? Add tags as images on product pages

I am trying to be clever, but this is a bit beyond my abilities.
I have products with tags, these tags denote if something is environmentally friendly or not. Some of these tags are "biodegradable", compostable" and "recycled" for example.
If a product has these tags, I want to echo it on the front end.
I have the code to do this, and it is working as expected:
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//create a list to hold our tags
echo '<ul class="product_tags">';
//for each tag we create a list item
foreach ($current_tags as $tag) {
$tag_title = $tag->name; // tag name
echo '<li><img src="/img/tags/'.$tag_title.'.png"></li>';
}
echo '</ul>';
}
However, the only way to get this working is for me to edit content-single-product.php or single-product.php and place it in my theme in the woocommerce folder.
Is there a better way?
I'd like to control exactly where in the source order of that page it is displayed.
Figured it out. In functions.php:
/**
* Add test
*/
add_action( 'woocommerce_after_single_product_summary', 'tjobbetest', 5 );
function tjobbetest() {
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//create a list to hold our tags
echo '<ul class="product_tags">';
//for each tag we create a list item
foreach ($current_tags as $tag) {
$tag_title = $tag->name; // tag name
echo '<li><img src="/img/tags/'.$tag_title.'.png"></li>';
}
echo '</ul>';
}
}

Custom hierarchy WordPress menu - show only child pages or current active page

I am trying to create custom code for complex WordPress menu.
Let's assume that i have menu wordpress menu structure like this - Granchild is last node, there is no more children nodes:
Parent1
-Child1
--Grandchild11
--Grandchild12
-Child2
--Grandchild21
--Grandchild22
Parent2
My task is to create something like this:
When user is on homepage, only Parent1 and Parent2 are displayed as menu list items.
When user clicks for ex. to Parent1 and enter that page he can see menu structure like this( static, as list item inline)-only children pages of clicked page, but also only first level items :
|---------Parent1---------|--------Parent2-----------|
|---Child1---||---Child2---|-----ChildN----|
Then let's assume that user clicks on Child1 in newly created ribbon.
When that page load user must see next structure:
|---------Parent1---------|--------Parent2-----------|
|---Child1---||---Child2---|-----ChildN----|
|---Grandchild11---||---Grandchild12---|
Now user can click on any Grandchild element from list, and when load that page,which has not own children, on that page needs to be displayed same menu structure as in graph above.
I tried to implement it via this code (do shortcode statement is in header after menu):
function wpb_list_child_pages() {
global $post;
if ( is_page() && $post->post_parent )
{$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0&depth=1' );}
else
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0&depth=1' );
if ( $childpages ) {
$string = '<ul class="menidva"><center>' . $childpages . '</center></ul><br>';
}
return $string;
}
but it is not working as required.
I kept wordpress menu just for everyone can see real structure in dropdown styling.
Here is a link to site to test :LINK
Can anybody please help me with this ?
Sorry just wanted to be clear as it's not a straight forward thing to solve. I've had to mangle your code a little so I could test it locally and I may not be achieving exactly what you want but the answer below should help you solve your problem as I believe the else if statements are more or less what you need.
<?php
function wpb_list_child_pages() {
global $post;
$ancestors = get_post_ancestors($post->ID);
$root_parent_page = end($ancestors);
echo $root_parent_page;
if(is_page() && (count($ancestors) >= 2)){
echo 'YOU ARE ON A GRAND CHILD PAGE';
echo '<ul>';
wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $root_parent_page . '&echo=1&depth=1' );
echo '</ul>';
echo '<ul>';
wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=1&depth=1' );
echo '</ul>';
}
elseif ( is_page() && $post->post_parent ){
echo 'YOU ARE ON A CHILD PAGE';
echo '<ul>';
wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=1&depth=1' );
echo '</ul>';
}
else{
echo 'YOU ARE ON A PARENT PAGE';
echo '<ul class="menidva">';
wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=1&depth=1' );
echo '</ul>';
}
}
wpb_list_child_pages();
?>

Drupal - Show child terms of current term, only if child items exist - code modification needed

Having fought with the problem of showing child terms for a given taxonomy term in a block, I finally stumbled on a piece of code that does exactly what I want here
As per the instructions I've added the following to my template.php
function themename_child_terms($vid = 1) {
if(arg(0) == 'taxonomy' && arg(1) == 'term') {
$children = taxonomy_get_children(arg(2), $vid);
if(!$children) {
$custom_parent = taxonomy_get_parents(arg(2));
$parent_tree = array();
foreach ($custom_parent as $custom_child => $key) {
$parent_tree = taxonomy_get_tree($vid, $key->tid);
}
$children = $parent_tree;
}
$output = '<ul>';
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
}
I've then created a block and added :
<?php // $vid is the vocabulary id.
print themename_child_terms($vid = 1);
?>
This shows the child terms of the current term perfectly. However, it shows ALL terms that exist under the parent term, even if there is no piece of content using that term.
e.g.
viewing the page with all items in Term 1, I get
child 1
child 2
child 3
correctly listed in the block. But, if there are no pieces of content tagged with 'child 3' for example, it's still showing up that term in the block. This isn't very useful as it links to an empty term page.
How would I modify the code to only show children that actually have items associated with them. So if there are no children tagged 'Child 3', then that term would not show up. Is it an easy modification?
Thank you kindly for any solutions.
Nick
Working with Drupal 6
Thanks to user jerdiggity for posting the following reply over on drupal.stackexchange here. Works perfectly.
Hmm... I'd try something like this:
Change this part of your code:
foreach ($children as $term) {
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
to something like this:
// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
// If not empty, run the foreach loop:
foreach ($children as $term) {
// Then check to see if any nodes exist for that term id:
$number_of_nodes = taxonomy_term_count_nodes($term->tid);
// If there ARE nodes...
if ($number_of_nodes > 0) {
// ... then add them to the output:
$output .= '<li>';
$output .= l($term->name, 'taxonomy/term/' . $term->tid);
$output .= '</li>';
}
}
}
Hope that helps... :)

3 column WordPress layout help needed

I'm busy trying to put together a 3x3 magazine-style layout for the home page of my blog.
I want the divs containing each post to flow vertically so there aren't big white spaces beneath each row of 3. It seems like the easiest way to do this would be to have three columns and fill each of these (which will stop there from being a big space beneath each post-block) and then place these three columns side-by-side.
The problem is that the Wordpress loop needs to pull posts sequentially. I don't know how to change the order of the Wordpress loop and, even though I've tried some PHP trickery using counts and for loops, I can't seem to get it to work.
Any thoughts or advice on a basic Wordpress loop or CSS way to make it work would be much appreciated as it's driving me crazy!
You can see how it is currently at http://www.unleashreality.com/
This looks like a job for jQuery Masonry.
Check it out at http://masonry.desandro.com/
The direct way using loop manipulation could be simpler assuming your layout is going to fixed one as shown in the mockup image.
<?php
$count = 0;
$column_1 = '';
$column_2 = '';
$column_3 = '';
$ad_block = '<div id="ad-block">Ad code here</div>';
while ( have_posts() ) : the_post();
$count++;
$content = '';
$content .= '<div class="post-block">';
$content .= '<h2>'.get_the_title().'</h2>';
$content .= '<div class="excerpt-block">'.get_the_excerpt().'</div>';
$content .= '<div class="continuebutton">...READ THIS ARTICLE</div>'; // Add appropriate code here..
if($count == 1 || $count == 4 || $count == 6) {
$column_1 .= $content;
} else if($count == 2 || $count == 7) {
$column_2 .= $content;
} else if($count == 3 || $count == 5 || $count == 8) {
$column_3 .= $content;
} else {
// Shouldn't come here...
}
// Insert the ad block in column 2 after adding 1st row
if($count == 2) {
$column_2 .= $ad_block;
}
endwhile;
?>
<div id="col1"><?php echo $column_1;?></div>
<div id="col2"><?php echo $column_2;?></div>
<div id="col3"><?php echo $column_3;?></div>
Adjust the code to work with inner pages.
If you want to do this without Javascript, you'll need to buffer the HTML for each column in your post loop and then output it in one shot once the loop is finished.
Something like the following:
<?php
// Hold the buffered column output
$cols = array("", "", "");
// Keep track of column we're appending to
$currentCol = 0;
// Get the posts
$posts = get_posts();
foreach($posts as $post){
// Run the WP post filters
setup_postdata($post);
// Append the content to the current column
$cols[$currentCol] .= '<div class="item">';
$cols[$currentCol] .= get_the_title();
$cols[$currentCol] .= get_the_content();
$cols[$currentCol] .= '</div>';
// Increment the current column and make sure we haven't
// gone over our total columns
if(++$currentCol >= count($cols)){
$currentCol= 0;
}
}
?>
<div id="col1"><?php echo $cols[0]; ?></div>
<div id="col2"><?php echo $cols[1]; ?></div>
<div id="col3"><?php echo $cols[2]; ?></div>

Resources