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]' );
}
Related
I used the code here: Adding custom field to product category to add custom fields to my woocommerce category pages.
The used code includes usage to retrieve the data:
echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);
Now, I'm not sure how I would pull the contents of each category's 'wh_meta_desc' into the 'woocommerce_before_main_content' hook space on the front-end of each relevant category page?
For example here: https://themirrorman.uk/category/wall-mirrors/
I have the equivalent code that would work to achieve this if I had a custom field named 'wh_meta_title' stored within a PRODUCT page. It would take the contents of 'wh_meta_desc' and place that content into the 'woocommerce_before_main_content' hook space on its product page:
function add_before_main_content() {
global $post;
$product_id = $post->ID;
$productCatMetaTitle = get_post_meta($product_id,'wh_meta_title',true);
if(!$productCatMetaTitle) return;
echo ''.$productCatMetaTitle.'';
}
add_action('woocommerce_before_main_content','add_before_main_content');
But how do I adapt this code so that it pulls through 'wh_meta_title' from its newly created CATEGORY page custom field? And then display its contents on the front-end within the 'woocommerce_before_main_content' hook space?
Would my code look something like this?
/** WooCommerce product category custom field - woocommerce_before_main_content - content **/
function add_before_main_content() {
global $post; //Is '$post' correct to apply this function to product category pages?
$term_id = $term->term_id;
$productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
if(!$productCatMetaTitle) return;
echo ''.$productCatMetaTitle.'';
}
}
add_action('woocommerce_before_main_content','add_before_main_content');
Thank you.
UPDATE:
I've now found a suitable solution here: http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/
Works a treat:
Or visit to see it in action now: https://themirrorman.uk/category/wall-mirrors/
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
I am trying to add content after the page title and customize the style of page where all products are listed in woocommerce, but I have no idea where is the file that I can copy to my custom theme, I have been able to customize the products in the loop but don't know where to customize this?
On inspect element it is the h1 element with a class page-title:
<h1 class="page-title">Nettbutikk</h1>
woocommerce/templates/single-product/title.php is the file you are looking for. You can copy single-product/title.php to yourchildthemefolder/woocommerce/single-product/title.php
Here is a link to their github of that file so you can see it for yourself. Let me know if this helped! :)
https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/title.php
To edit the woocommerce product list page, the file woocommerce/archive-product.php needs to be edited.
Woocommerce Listing products page add content after product title using given hook.
add_action( 'woocommerce_after_shop_loop_item_title', 'after_shop_loop_item_title', 1 );
Add this code in functions.php of current active theme.
add_action( 'woocommerce_after_shop_loop_item_title', 'after_shop_loop_item_title', 1 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$text = "<p> (";
foreach ($terms as $term) {
$text .= $term->name;
}
$text .= ") </p>";
echo $text;
}
Reference by: https://businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/
Trying to achieve something that should be simple, but I've tried 3 approaches with multiple code variations and I just can't make it work. I'm trying to create a button that will appear in place of the "ADD TO CART" button on single product pages when the item is out of stock. Clicking the button will fire a popup contact form.
Is creating an add action in functions the right way to go, or should I replace the normal button with an if statement? I've tried both, so help with coding either would be greatly appreciated.
You can either hook into woocommerce_loop_add_to_cart_args using a filter in your functions.php or edit the template file directly by pulling it into your theme. Either way will require a bit of PHP.
If doing it in your functions.php, it would look something like this (untested but should send you down the right path):
<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_out_of_stock_button' );
function my_out_of_stock_button( $args ){
global $product;
if( $product && !$product->is_in_stock() ){
return 'Contact us';
}
return $args;
}
I don't know what your button code should actually look like or what other information you need to capture, but this is how you could override the "Add to Cart" button and replace it if out of stock.
UPDATE
LoicTheAztec brought up a great point - the filter provided only affects the button on the archive, category, tag overview pages - not the individual product pages. There are no hooks for the individual product page buttons BUT you can copy the templates to your theme and override them.
You'll want to look at the files in templates/single-product/add-to-cart. Use a similar if statement as above:
#simple.php
<?php if ( $product->is_in_stock() ) : ?>
// Standard WooCommerce code
<?php else: ?>
// Your button code
<?php endif; ?>
Just add below code in functions.php file of your enabled theme reference
add_action('woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1);
// for shop page
function themelocation_change_outofstock_to_contact_us() {
global $product;
if (!$product->is_in_stock()) {
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
//change the link to your contact us page
echo ' Contact Us ';
}
}
// for single page
add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
// Change In Stock Text
if ($_product->is_in_stock()) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if (!$_product->is_in_stock()) {
$availability['availability'] = __(' Contact Us ', 'woocommerce');
}
return $availability;
}
I was looking for a way to show a contact button on bespoke products and
#ahwychkchih solution works great. One issue I had though is that schema markup will show as out of stock for those products which is not the case for beskpoke products is just they can't be purchased straight away so I've added this to force in_stock markup for my products. I'm aware that this solution would affect all products so you can always add a product id filter if needed
// Force In Stock schema markup
function fix_my_product_offers_schema ($markup_offer, $product) {
if (!$product->is_in_stock()) {
$markup_offer['availability'] = 'https://schema.org/InStock';
}
return $markup_offer;
}
add_filter('woocommerce_structured_data_product_offer', 'fix_my_product_offers_schema', 1, 2);
The code bellow, is to show a custom field I created to customize Woocommerce product category pages.. This code makes the custom field appear "after" the list of products. I need to make this code appear BEFORE the list of products... any hint on what I have to change in this bit of php code to make the custom field show before?
<?php
// Display details on product category archive pages
add_action( 'woocommerce_after_shop_loop', 'wpm_product_cat_archive_add_meta' );
function wpm_product_cat_archive_add_meta() {
$t_id = get_queried_object()->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta_content = $term_meta['custom_term_meta'];
if ( $term_meta_content != '' ) {
echo '<div class="woo-sc-box normal rounded full">';
echo apply_filters( 'the_content', $term_meta_content );
echo '</div>';
}
}
Thank you, I would really like to understand what makes the code appear after and not before, is the filter? in the last lines?
I found this bit of code at http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/
use below action to display custom fields before list of products
add_action('woocommerce_before_shop_loop','wpm_product_cat_archive_add_meta');