I am trying to upgrade my SEO on my webshop. Anyone knows php could fix this puzzle. How can i fit this in each other?
How does this: is_product_category( 'shirts' ) fit in this:
function woa_content_before_shop() {
echo "<p>Insert your Content here...</p>";
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
WordPress conditional tags like is_product_category() can be used to change the displayed content based on the matched condition.
In this case you can use them to change the printed text based on category. You can exploit them in this way:
function woa_content_before_shop()
{
if ( is_product_category( 'shirts' ) )
{
echo "<p>Here are shirts!</p>";
}
else if ( is_product_category( 'games' ) )
{
echo "<p>Here are games!</p>";
}
else
{
...
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
Use this code. Also set your descriptions in Products > Categories.
function woa_content_before_shop() {
if ( is_product_category() ) {
global $wp_query;
$id = $wp_query->get_queried_object_id();
$desc = term_description( $id, 'product_cat' );
if ( !empty( $desc ) ) {
$output = '<p>' . esc_html( $desc ) . '</p>';
echo $output;
}
}
}
add_action( 'woocommerce_before_shop_loop', 'woa_content_before_shop');
Related
I would like to add a custom content before the WooCommerce shop loop if a specific filter is active.
The example url is this: /shop/?filter_brand=nike
So far I have tried with this:
add_action( 'woocommerce_before_shop_loop', 'amc_add_content_before_loop' );
function amc_add_content_before_loop() {
if ( is_product_category('nike') ) {
echo esc_html__("Custom content", "amc");
}
}
But the content is not shown on the page.
EDIT
With Vincenzo's answer now it works.
I am now trying to use variables and add a shortcode. I tried with this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop2' );
function add_custom_content_before_shop_loop2() {
$terms = get_terms( 'pa_brand' );
foreach ( $terms as $term ) {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == $term->name ) {
echo do_shortcode('[block id="brand-'.$term->name.'"]');
}
}
}
So what it supposed to do is, if a filter $term->name is active, a custom shortcode with the $term->name should be echoed.
But it's not working.
Considering the url you posted: /shop/?filter_brand=nike, you can do it like this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop' );
function add_custom_content_before_shop_loop() {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == 'nike' ) {
// add custom content
}
}
The code has been tested and works. Add it to your active theme's functions.php file.
EDIT
To get the pa_brand attribute name you are using $term->name instead of $term->slug.
$term is an instance of the WP_Term class. The structure of the WP_Term object can be found here.
Try with this:
add_action( 'woocommerce_before_shop_loop', 'add_custom_content_before_shop_loop2' );
function add_custom_content_before_shop_loop2() {
$terms = get_terms( 'pa_brand' );
foreach ( $terms as $term ) {
if ( is_shop() && isset( $_GET['filter_brand'] ) && $_GET['filter_brand'] == $term->slug ) {
echo do_shortcode('[block id="brand-'.$term->slug.'"]');
}
}
}
I've created a do_action('after_product_tabs) hook that I hope to programmatically echo an image under specific product categories, but I'm running into a bug. I'm able to echo out a div as a test for the action hook, but my conditional logic is breaking. Any help would be very much appreciated:
Here's the function:
function after_product_tabs() {
do_action('after_product_tabs'); //creates the action hook
};
function display_prewired_image() {
echo '<div class="test" style="clear: both;">'.$home_url.'</div>'; // This prints!
if( has_term( 'strat_pickups', 'product_cat' ) ) {
echo' <p>DEBUG ME</p>'; // This breaks
}
}
add_action('after_product_tabs', 'display_prewired_image');
I've also Tried:
1.) Passing in Global Variable:
function display_prewired_image() {
global $product;
$prod_id = $product->get_id();
if( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
echo' <p>DEBUG ME</p>';
}
}
2.) Changing the conditional logic from product categories to is_single(), which works, so I know my problem is within the logic of strat_pickups:
function display_prewired_image() {
global $product;
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$prod_id = $product->get_id();
if( is_single('354') ) {
echo '<h1 style="red!important; clear: both;">DEBUG ME</h1>';
}
} else {
echo 'No product found!';
}
}
add_action( 'after_product_tabs', 'display_prewired_image' );
For more info, I'm customizing the tabs.php template in Woocommerce. Would that not have access to the $product variable?
Try this instead, global $product is missing
function display_prewired_image() {
global $product;
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$prod_id = $product->get_id();
if ( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
echo 'DEBUG ME';
} else {
echo 'Category not found!';
}
} else {
echo 'No product found!';
}
}
//add_action( 'after_product_tabs', 'display_prewired_image' );
add_action( 'woocommerce_before_add_to_cart_form', 'display_prewired_image' );
add_action( 'woocommerce_after_single_product', 'display_prewired_image' );
thank you for your help. This was a bang your head against the wall moment. The product category was not strat_pickups, it was strat-pickups.
I've created a bunch of shortcodes that are either used within posts or echo'd into product pages using wordpress hooks.
I'm trying to create a conditional function so it echos a specific shortcode dependent on the product category. It would need to echo into the 'woocommerce_after_single_product_summary'.
This is the draft so far, can someone give some advice on how to complete this please?
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
if ( is_product_category( 'xx-slug' ) ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( is_product_category( 'xy-slug' ) ) {
echo do_shortcode("[xy-product-notice]");
}
}
Hope this help you.
add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );
function add_warranty_notice() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag' );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
if ( $product_cat_name == 'parent_one' ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( $product_cat_name == 'parent_two' ) {
echo do_shortcode("[xy-product-notice]");
}else{
}
}
I want to display sale bubble in WooCommerce only for logged in users.
I have a function which hides sale-bubble for unlogged users but if I log in there is showing only value "1" instead of sale-bubble.
I know why, because I am returning a true, but I cant figure out how to return sale-bubble instead of true..
WooCommerce
add_filter('woocommerce_sale_flash', 'woo_custom_hide_sales_flash');
function woo_custom_hide_sales_flash()
{
if ( is_user_logged_in() ) {
return true;
}
return false;
}
You are not using this filter hook in the right way. Try the following:
add_filter( 'woocommerce_sale_flash', 'filter_sales_flash_callback', 100, 3 );
function filter_sales_flash_callback( $output_html, $post, $product )
{
if ( ! is_user_logged_in() ) {
$output_html = false;
}
return $output_html;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
function sales_badge() {
global $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'simple' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
if ( $max_percentage > 0 ) { ?>
<span class="prinjal-sale-badge"><?php echo round($max_percentage) . "%"; ?></span>
<?php
}
}
// use shortcode instead of action and then use shortcode anywhere you want to ouptut it
add_shortcode( 'custom_sales_badge', 'sales_badge' );
my problem solve using this code you can place any ware you want to display sale badge
function sales_markup() {
if(!is_admin()) {
if(is_user_logged_in()) {
// Instead of outputting add the markup that you want to show
$output = '<div class="Sales_markup_here">
</div>';
return $output;
}
}
}
// use shortcode instead of action and then use shortcode anywhere you want to ouptut it
add_shortcode( 'sales_markup', 'sales_markup' );
Use the shortcode where you want to output the bubble. You can add the css in the global css file.
I am trying to have it so for specific product attribute that contain "agave" text is displayed in the short description.
I have tried a few snippets of code but none seem to work. I have no problem getting them to work with Categories but I just want it for certain attributes of the products - Agave
function filter_woocommerce_short_description( $post_excerpt ) {
global $post;
if ( has_term( "agave", "categories", $post->ID ) ) {
$post_excerpt .= "<br/>" . "Text Here";
}
return $post_excerpt;
};
add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1 );
I expect the text to show up under the certain attributes (Agave) but they do not
I have tried to use this
add_filter('woocommerce_short_description',
'filter_woocommerce_short_description',10, 1 );
function filter_woocommerce_short_description( $short_description ) {
global $product;
$string_values = $product->get_attribute('agave');
if ( strpos($string_values, 'agave') !== false ) {
$short_description .= '<br>' . __("Testing This Out - AGAVE");
}
return $short_description;
}
For a specific product attribute "agave" you will use something a bit different:
add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1 );
function filter_woocommerce_short_description( $short_description ) {
global $product;
$string_values = $product->get_attribute('agave');
if ( ! empty($string_values) ) {
$short_description .= '<br>' . __("Text Here");
}
return $short_description;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Now if "agave" is a term of a product attribute, you should need to set the product attribute name in $product->get_attribute('attribute-name');
and replace the condition by something like:
if ( strpos($string_values, 'agave') !== false ) {
Note: The taxonomy for product category is product_cat, but not categories…
add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1 );
function filter_woocommerce_short_description( $short_description ) {
global $product;
$string_values = $product->get_attribute('agave');
$agave = $attributes["agave"];
if ( $agave ) {
$short_description .= '<br>' . __("Testing This Out - AGAVE");
}
return $short_description;
}