WooCommerce Hook into Category Info - php

I've been doing research for a couple of hours and going nowhere. Perhaps It's that I don't even know what I'm searching for.
I have a WooCommerce site running on Flatsome's latest version (Theme.)
I'm simply trying to get the category link for each item in the loop and make a CTA button to open the category. This is a widget on the homepage on flatsome.
function ill_category_button() {
$link = "#";
echo '<div class="add-to-cart-button">Open Collection</div>';
}
add_action('woocommerce_after_subcategory_title', 'ill_category_button');

The product category is passed to your function as a parameter, which you can then use to grab the link using get_term_link().
function ill_category_button( $category ) {
$link = get_term_link( $category->term_id);
echo '<div class="add-to-cart-button">Open Collection</div>';
}
add_action('woocommerce_after_subcategory_title', 'ill_category_button');

Related

How to use PHP variable with a Wordpress Shortcode of AAWP Plugin

I use Wordpress with the GeneratePress Theme. A feature of this theme called "Elements" let you write php code which will be executed on a specific hook. These Hooks can be found here: https://docs.generatepress.com/article/hooks-visual-guide/
I use the AAWP plugin to show an amazon bestseller list. I want to get the Wordpress category and place it into the bestseller parameter of the aawp shortcode to get Products for the respective categories.
I already read posts here about using php variables in wordpress shortcodes but it seems that something other than the syntax is the problem.
I have this code
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp grid="3" '.$bestseller.' items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
The amazon list doesnt appear on my page. It seems that the "bestseller" parameter is neccesarry in order to get shown.
The variable doesnt really get recognized.
When I remove the variable and replace it with a hardcoded bestseller parameter its working fine.
When I use this code:
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
It seems that the bestseller value doesnt get read but the "empty" bestseller parameter is enough to show the amazon list, so it just shows random products.
How can I make this work? I just want to place a variable as a value into the "bestseller" parameter. Should be the easiest thing but it doesnt work in the scope of this GeneratePress Elements Hook whith the AAWP shortcode
You're possibly not getting what you believe you need back from get_the_category(). Assuming you're able to get the category id you can use get_cat_name() function to return the category name.
<?php
$cat_name = get_cat_name($category_id);
if ( ! empty($cat_name) ) {
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$cat_name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>

Woocommerce - Get Attribute Thumbnail and Display

There's a number of posts on here attempting to solve this however, I haven't been able to find a solution that works.
I'm attempting to get the URL of the thumbnail image, associated with the product attribute term of the product displayed on a single-product page, and then display the thumbnail below the product meta, with a URL to the term's archive.
The hook that I'm using is as follows:
add_action('woocommerce_product_meta_end','product_brand_image');
Then I'm defining the function used in the hook:
function product_brand_image() {
}
The attribute name is "Brand", and the slug is "brand".
I've managed to get as far as getting the term ID using the following:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
}
I've prnt'd this and it returns the term ID, which I've confirmed to be correct.
I'm struggling to now use this to get the associated term thumbnail URL, and term archive slug to be able to insert the thumbnail and then link it to the archive.
Looking to solve this programmatically without using another plugin.
UPDATE:
Ok, so using #Barrie Dawson comment as a guide, I am now up to this point:
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
}
}
I have found that attribute term thumbnails are stored in table wp_posts. The 'product_search_image_id' field links to the ID field in wp_posts table. The URL of the thumbnail associated to the term is then listed under the 'guid' column in this same table. I need some assistance with the PHP to extract this.
<?php
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('thumbnail_id', $termMeta)) {
$termThumbnail = get_the_post_thumbnail_url($termMeta['thumbnail_id']);
}
}
you can also request available sizes see: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
And here is the final solution I arrived at:
add_action('woocommerce_product_meta_end','product_brand_image');
function product_brand_image() {
global $product;
$attributes = $product->get_attributes();
$attr_id = $attributes['pa_brand']['options'][0];
$term = get_term($attr_id);
$termSlug = $term->slug;
$termMeta = get_term_meta($attr_id);
if (is_array($termMeta) && array_key_exists('product_search_image_id', $termMeta)) {
$post_id = $termMeta['product_search_image_id'][0];
$postData = get_post($post_id);
$prod_image_url = $postData->guid;
echo '<span class="product_brand_image"><img src="'.$prod_image_url.'" /></span>';
}
}
This displays the thumbnail of the term of the product attribute below the product meta, and links it to the archive page for that term. In this instance, it links to a shop page (archive) with all products associated to a particular 'brand'. See screenshot below.
Screenshot

Query author meta for all posts on archive page

When a user registers on my Wordress site, a custom post (Athlete) is automatically created, with the user being assigned as the author. The custom post essentially acts as a profile page.
On their profile page, users fill out a bunch or info, and a total_score is calculated and saved as user meta. If they do not complete all of the forms - they won't have a total_score as it is calculated on submission.
I have created a custom archive page for the posts (athletes), and used Settings > Reading > Posts Page to set it as the default posts archive.
On the post preview template (created and looped using Ele Custom Skins and Elementor) I have added an element called #total_score_circle- seen in the screenshot below.
I would like to hide #total_score_circle on the post preview layout if there is no total_score in the author meta for that post.
The below code currently hides the #total_score_circle across all post previews, and not just the ones where total_score doesn't exist in the author meta. So my query is clearly off.
Any help would be greatly appreciated.
function total_score_display(){
if (is_home()){
global $post;
$author_id=$post->post_author;
$total_score = get_the_author_meta('total_score', $author_id);
if(empty($total_score)) : ?>
<style type="text/css">
#total_score_circle {
display: none !important;
}
</style>
<?php endif;
}
}
add_action( 'wp_head', 'total_score_display', 10, 1 );
The frontend has been created with Elementor Pro, and I have used Elementor Custom Skin to create the loop that displays the search results.
Thanks to Ruvee's guidance, I managed to solve my problem through implementing a shortcode on the actual custom skin page using Elementor and the below PHP.
Essentially I created a shortcode that displayed the value with a custom CSS class .total_score_circle and then used another shortcode to run the if/else statement.
If total_score exists, return do_shortcode(), if not return a separate, irrelevant CSS class.
I'm sure it's not the elegant way to do it, but worked a treat with Elementor.
// Create shortcode to show total_score
add_shortcode('total_score_sc', 'total_score_sc');
function total_score_sc ($atts) {
$total_score = get_the_author_meta( 'total_score');
$total_score_class = '<div class="total_score_circle" >';
$total_score_class .= $total_score;
$total_score_class .= '</div>';
return $total_score_class;
}
// Create shortcode to replace above shortcode if total_score not present
add_shortcode('final_total_score_sc', 'final_total_score_sc');
function final_total_score_sc() {
global $post;
$author_id = get_post_meta( get_queried_object_id(), 'author_id' );
$total_score = get_the_author_meta( 'total_score', $author_id );
$sR = '<div class="total_score_circle_empty" >';
$sR .= '</div>';
if(empty($total_score))
return $sR;
else
return do_shortcode( '[total_score_sc]' );
}

Adding an image page banner to shop page

I am using WordPress 4.9.6.
I have set the shop page to be the home-page.
How do I add a page banner to the shop page. I would like to add it just above the breadcrumb trail.
I have tried adding this to the following page archive-product.php
if (is_shop()) {
$args = array('taxonomy' => 'product_cat');
$product_categories = get_categories( $args );
$term_id = $product_categories[0]->term_id;
$content = get_term_meta($term_id, 'cat_meta');
if(isset($content[0]['cat_header'])){
echo do_shortcode($content[0]['cat_header']);
}
}
Unfortunately, not able to add any image to the page.
You can achieve using 2 methods.
1) Add your static image directly at the beginning of archive-product.php
echo "<img src='{YOUR_IMAGE_PATH}'>";
2) Add filter in your theme's functions.php file.
add_action ('woocommerce_archive_description' , 'shop_banner',99);
function shop_banner() {
echo '<img src="{YOUR_IMAGE_PATH}" >';
}
I'm not so sure if I understand exactly what you want. But this is what I understand so far.
If you want to display an Static image banner above the breadcrumbs in your Shop Page.
You could use the woocommerce_before_main_content action.
function BannerShop(){
if(is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
add_action( 'woocommerce_before_main_content', 'BannerShop', 10 );
Here i show the before and after. BTW I don't know what theme are you using so it may be displayed different.
Before
https://i.stack.imgur.com/Mv2YK.jpg
After https://i.stack.imgur.com/nTfCa.jpg

Conditional if statement for a particular WP page

I am using Woo Commerce plugin and I want to display extra text for a specific product page.
The product ID seen in my body is:
single single-product postid-2624
So I tried the following code but it didn't work:
<?php
function ip_more_content() {
if ( is_single('2624'); ) {
echo 'show something';
}
else {
echo '';
}
}
add_action( 'woocommerce_product_thumbnails' , 'ip_more_content', 9 );
?>
How can I make WP do something based on a specific product id?
I guess that 'woocommerce_product_thumbnails' is running inside the loop, so you cab grab item id by simply using get_the_ID() function. So, edit your conditional like this:
if ( get_the_ID() == 2624 ) {
And it should work it out.

Categories