Woocommerce: Get the chosen attribute from item in cart - wordpress

In the cart, i get a variation_id for all my variable products.
Is it possible to get the choosen attribute from that id in any way?
I have tried:
<?php $variation = new WC_Product_Variation($cart_item[variation_id]);?>
<?php foreach($variation->get_variation_attributes() as $name => $attr): ?>
<?php $name = substr($name, 10); //remove attribute_ from the key. ?>
<?php echo $variation->get_attribute($name); ?>
<?php endforeach; ?>
but that only outputs ALL available attributes for that variation, not the chosen one.
It feels like i am missing something obvious here, so any pointes in the correct direction would be lovely.

After looking and looking I found another method in the WC_Product_Variation class.
get_variation_attributes()
It does exactly what I want.
For more information, look here:
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Variation.html#263-270

Related

Assign an image to a taxonomy in wordpress?

I am trying to get a different icon to appear next to an event in Wordpress. I am use the "The Event Calendar" plugin and the events are stored as taxonomies. I have three taxonomies. "Tax1", "Tax2" and "Tax3".
I cannot seem to figure out how to call to check what the taxonomies are and assign an icon to each one. Here is what I have so far.
" title="" rel="bookmark">
<?php $taxonomy_exist = taxonomy_exists( 'Tax2' ); ?>
<?php if(is_tax($taxonomy_exist)) { ?>
<p>working!!!!!!!!!!</p>
<?php } else { ?>
<img class="cat-icon" src="/wp-content/uploads/2017/11/Heart.png" />
<?php } ?>
<?php $title = get_the_title($event); ?>
<?php if (strlen($title) > 30) { ?>
<?php echo mb_strimwidth($title, 0, 30, '...'); ?>
<?php } else { ?>
<?php echo $title ?>
<?php }?></a></span><!--.event-title-->
So it is getting to the if statement and failing, showing the heart icon. How can I see if it is part of the "Tax2" taxonomy?
Problem 1:
is_tax() accepts taxonomy names as a first argument. But you put $taxonomy_exist parameter which is boolean. (true/false). Of course it should not work and always return zero. Because there is not any taxonomy called "true" or "false".
Problem 2: Are you sure that tax1, tax2 and tax3 are taxnomies? may be they are terms? if they are terms then i will add a solution code as an update to this answer. If they are taxonomies, then i don't see any logic there. Because taxonomies are not objects to be related to posts. Taxonomy is name of categorization, terms are member elements of those taxonomies.

Fetch custom field data for a specific blog post

I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What's the proper way to output the content of the custom field?
The easiest way to do this is this:
<?php echo get_post_meta($post->ID, 'your_meta_key', true); ?>
On your post or page editor, you can go to "Screen Options" in the top right corner and check the box to display "Custom Fields". This will allow you to see the meta keys available. Just copy the name of the meta key into your get_post_meta call in the spot above where it says "your_meta_key". Don't change the $post->ID as that is global.
Taken from that page linked
<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
so you'd need to access it through the $meta_values return object.
Like so:
<?php
print_r($meta_values);
print 'The ID is: ' . $meta_values->ID;
?>
get_post_meta(1, 'Currently Reading',true); will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:
<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a print or echo like so:
echo $custom;
Something to note, try using a value $post_id for the first argument. This will grab the current post id.

WordPress wpAlchemy sort repeating fields values

I don't even know where to begin with this, so I'm just going to throw it out there. Say there is a custom post type and in it there is a wpAlchemy powered meta box with repeating fields, where in a person can add multiple attachments.Say you don't expect or trust the user to sort the attachments themselves so you want to return the array of attachments in alphabetical order by the value title. How would you do this with the following code? (This code is within the loop of a single page)
<?php
global $attachment_repeat;
$meta = $attachment_repeat->the_meta();
?>
<?php if($attachment_repeat->have_value('docs')):?>
<ul class="attachments">
<?php while( $attachment_repeat->have_fields('docs') ) : ?>
<li>
<!--url --><!--title --><?php $attachment_repeat->the_value('title'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
First you should get the main array ... of "docs" $arr = $attachment_repeat->the_value('docs'); if you var_dump($arr) you will see an associative array .. you can then sort using PHP array sort functions .. maybe something like this:
usort( $arr, "custom_sort" );
function custom_sort($a,$b) {
return strcmp( $a['title'], $b['title'] );
}

Wordpress: Adding the category as text - not link

I want to add a ribbon on my posts on the front page, which shows the posts category.
I can add it as text like this (I use the Imbalance theme by WPShower):
<?php imbalance2_posted_in(); ?>
But how can I just write the Category Name, without markup so I can use it in classes and such?
Thank you in advance.
You can get the category of every post with get_the_category(). Below is shown how to get the category of the current post.
global $post;
$category = get_the_category( $post->ID ); //OR SOME OTHER ID, DEPENDING ON WHAT YOU WANT
$category_name = $category->name; //GETS THE ORIGINAL NAME, INCLUDING WHITESPACES
$category_slug = $category->slug; //GETS THE SLUG, WHICH WILL BE BETTER TO USE IN CLASSNAMES
EDIT
<?php
global $post;
$category = get_the_category( $post->ID );
?>
<div class="box <?php echo($category->slug); ?>"></div>
You can use post_class() to generate a few class names including one for each category.
If you want to do it manually, you can get information on the categories using get_the_category() and put the class names together yourself.

Wordpress - Add comma to all but last item, remove underscore

I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>

Resources