Add an image on Woocommerce payment method title - php

In Woocommerce, I plan on adding images before bank name plugin BACS. at now I'm already input bank name and other setting and already try inputting HTML before bank name but it does not work.

You can easily add an icon (an image) to the payment gateways in checkout page.
But in Woocommerce this icon is located after the title.
To change it before the title you should need to edit the related template checkout/payment-method.php at line 27 from this:
<?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
to this:
<?php echo $gateway->get_icon(); ?> <?php echo $gateway->get_title(); ?>
and save … Please see: How to Override WooCommerce Templates via a Theme …
You will need to upload the image(s) in a folder in your theme as "assets" for example.
For each gateway you can enable a custom image, or return the default one, using this custom function hooked in woocommerce_gateway_icon action hook:
add_filter( 'woocommerce_gateway_icon', 'custom_payment_gateway_icons', 10, 2 );
function custom_payment_gateway_icons( $icon, $gateway_id ){
foreach( WC()->payment_gateways->get_available_payment_gateways() as $gateway )
if( $gateway->id == $gateway_id ){
$title = $gateway->get_title();
break;
}
// The path (subfolder name(s) in the active theme)
$path = get_stylesheet_directory_uri(). '/assets';
// Setting (or not) a custom icon to the payment IDs
if($gateway_id == 'bacs')
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/bacs.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'cheque' )
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cheque.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'cod' )
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cod.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'ppec_paypal' || 'paypal' )
return $icon;
return $icon;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested on WooCommerce 3 and works.
How to get the gateway ID:
Go on WC Settings > Checkout (end of the page) listed in the Gateway ID column

Related

Woocommerce changing title on shop depending on variables

I'm trying changing titles on Wordpress Woocommerce shop page depends on which variation I'm using.
I've found one solution which works perfectly on product pages. I'm stuck now and need to change a little bit the code that will work on shop page too.
Here is the code for the product page:
https://wordpress.org/support/topic/how-to-make-the-title-of-the-product-change-when-choosing-specific-variation/
It works perfectly, you can check the working code here:
http://tattodivi.nhely.hu/product/flower-lily/
I need one more solution which works on Woocommerce shop page too in this link:
http://tattodivi.nhely.hu/
If any of you can help me on this let me know.
Thanks.
The following code might work
jQuery('.products .variable-item').on('click', function(){
if(jQuery(this).parent().hasClass('image-variable-wrapper')){
var title_obj = jQuery(this).parent().parent().parent().parent().parent().find('.woocommerce-loop-product__title');
if(typeof(title_obj.attr('data-title'))=='undefined'){
title_obj.attr('data-title', title_obj.text());
}
title_obj.text(title_obj.data('title')+' - '+jQuery(this).data('value'));
}
});
This snippet will work only if you set default variation beforehand as its in the demo you provide.
//Remove default title
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);
//Add custom title
add_action('woocommerce_shop_loop_item_title','custom_template_loop_product_title',10);
function theme_template_loop_product_title() {
global $product;
if($product->get_type() === 'variable'):
$attributes = $product->get_default_attributes();
if($attributes):
$_title = array();
foreach($attributes as $attribute):
$_title[] .= $attribute.' ';
endforeach;
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() .' '. implode('',$_title).'</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
else:
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';
endif;
else:
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; endif;
}

Display product attributes not set for variation on Woocommerce product variations

I have custom tab on each product variation with some 'variation description' static content and that I have displayed on product page as well in > product summary when I select a variation (like in this link).
And now I need to display the same on shop page in product loop (see my shop page).
OR even better to display specific attributes. I tried this code:
add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
function custom_before_title() {
global $product;
echo '<h4><b>Seizoen:</b>' . $product->get_attribute('pa_seizoen') .'</h4>';
echo '<h4><b>Maat:</b> ' . $product->get_attribute('pa_maat') .'</h4>';
echo '<h4><b>Tijk:</b> ' . $product->get_attribute('pa_tijk-weving') .'</h4>';
echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_fullkraft[') .'</h4>';
echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_vulling') .'</h4>';
}
But whatever I do I get same result, it displays only product attributes that are in the product variations: first two "season and size".
Any help is appreciated.
You are using a plugin (or some customizations) that is displaying on your shop page (or archive pages) each product variations individually.
So you need to get the parent variable product for other product attributes that are not set for variations (and the product description or other required related data):
add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
function custom_before_title() {
global $product;
if( $seizoen = $product->get_attribute('pa_seizoen') ) {
echo '<h4><strong>Seizoen:</strong>' . $seizoen . '</h4>';
}
if( $maat = $product->get_attribute('pa_maat') ) {
echo '<h4><strong>Maat:</strong> ' . $maat . '</h4>';
}
$parent_id = $product->get_parent_id(); // Get the parent Variable Product ID
// Below, all Parent variable product data
if( $parent_id > 0 && $parent_product = wc_get_product($parent_id) ) {
if( $tijk_weving = $parent_product->get_attribute('pa_tijk-weving') ) {
echo '<h4><strong>Tijk:</strong> ' . $tijk_weving . '</h4>';
}
if( $fullkraft = $parent_product->get_attribute('pa_fullkraft') ) {
echo '<h4><strong>Vulkracht:</strong> ' . $fullkraft . '</h4>';
}
if( $vulling = $parent_product->get_attribute('pa_vulling') ) {
echo '<h4><strong>Vulling:</strong> ' . $vulling . '</h4>';
}
// Display the parent variable product description
echo '<p><strong>Beschrijving:</strong> ' . $parent_product->get_description() . '</p>';
// Display the parent variable product short description
echo '<p><strong>Korte beschrijving:</strong> ' . $parent_product->get_short_description() . '</p>';
}
// Display the product variation description
// echo '<p><strong>Beschrijving:</strong> ' . $product->get_description() . '</p>';
// Display the product variation weight
// echo '<p><strong>Gewicht:</strong> ' . wc_format_weight( $product->get_weight() ) . '</p>';
// Display the product variation dimensions
// echo '<p><strong>Dimensies:</strong> ' . $product->get_dimensions() . '</p>';
}
Code goes in functions.php file of your active child theme (or active theme). It should work now.

Display ACF field/image below the product title in WooCommerce archive pages

I'm using Advanced Custom Fields Plugin and I could just display the text field below the product title on the Archive page but I also need to display image as a logo on the same location (under the text field).
This is the code I'm using for the display the text field and it works:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), 'oa1', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="ozel-alanlar">'.$custom_field.'</p>';
}
}
And I also tried to this code to display image field but it doesn't work:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_image_display_below_title', 2 );
function custom_image_display_below_title() {
global $product;
// Get the custom field value
$oa2 = get_post_meta( $product->id, 'oa2', true );
// Display
if ( ! empty( $oa2 ) ) {
echo '<img src="' . $oa2 . '" />';
}
}
This is the result:
And the custom fields I created;
As you are using Advanced custom fields, you need to use get_field() dedicated function instead. The image field need to be set as "Image Url". Now the code will be:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Display ACF text
if( $text = get_field( 'oa1', $product->get_id() ) ) {
echo '<p class="ozel-alanlar">' . $text . '</p>';
}
// Display ACF image
if( $image_url = get_field( 'oa2', $product->get_id() ) ) {
echo '<img src="' . $image_url . '" />';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You will get something like:
ACF image field documentation

Override themes functions

In my theme there is a functions-template.php file with a lot of functions. One of them echoes the category description on the site.
function woocommerce_taxonomy_archive_description() {
if ( is_tax( array( 'product_cat', 'product_tag' ) ) && get_query_var( 'paged' ) == 0 ) {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$description = apply_filters( 'the_content', term_description() );
if ( $image && yit_get_option( 'shop-category-image' ) == 1 ) {
echo '<div class="term-header-image"><img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[1] . '" alt="' . $cat->name . '" /></div>';
}
if ( $description ) {
echo '<div class="term-description">' . $description . '</div>';
}
}
}
I want to echo another variable instead without messing with the files. Is there a way to "override" an existing function?
I've been fiddling a bit with mu-plugins etc but with no success.
I always get the Fatal error: Cannot redeclare woocommerce_taxonomy_archive_description() (previously declared in error when adding the same function in my custom functions file..
Yes it can be overridden from a child theme. You can override the function from your child themes functions.php file.
See more about child theme https://codex.wordpress.org/Child_Themes
WordPress loads child theme first and then the parent theme. So if you create a function with same name in your child theme, then the if condition with ! function_exists will will be false and hence there this function won't be declared.
If you want to override from a plugin then you've to declare the function in earlier execution. Try declaring it in a init hook.
add_action('init', 'theme_func_override', 5);
function theme_func_override(){
function override_func(){
//code goes here
}
}
Update
If the function isn't declared with function_exists() check within a if condition then you can't override it!

Custom Field URL Option for Woocommerce Product Thumbnail

Currently I'm using the Woocommerce plugin for affiliate products. I would like to be able to click on the thumbnail on the main page and go directly to amazon, for example. Currently it's setup so that once clicked it goes to the product detail page on my site. From there you can get to the amazon page. However, fewer clicks the better.
So I found the hook in the content-product.php page. What I did was wrap the whole thing in a URL and used a custom field to add in the URL. Doesn't work as intended. What happens is that the URL goes to amazon only when using one of the sale flash options. When turned off, the URL does not go to amazon, but to the product page on my site. I don't know where else to place the URL wrapper.
So i tried looking for the <a href="<?php the_permalink(); ?>"> that is currently controlling where the thumbnail goes. I traced the function to the woocommerce-template.php file. That's where I hit a dead end. I'm not sure where it is for the thumbnail currently.
Here is my modified code that works partially in the content-product.php page:
<div class="thumbnail-wrapper">
<a href="<?php echo get_post_meta( $post->ID, 'URLThumb', true ); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
</a>
</div>
Here is the thumbnail function that I can't seem to drill down further to find the existing <a href="<?php the_permalink(); ?>"> to change. This is on the woocommerce-template.php page.
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
/**
* Get the product thumbnail for the loop.
*
* #access public
* #subpackage Loop
* #return void
*/
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
File Name: woocommerce.php
File Location: wp-content/themes/'your-theme'/theme/woocommerce.php
Solution: Target external products via query of product type, looping in $product_url when external, and looping in get_permalink() when simple/variable. This code also accounts for opening external products in a new tab.
I'm going to post one version of what the code looked like before, and then another with my additions + modifications. In my theme, the first line of code I pasted exists on line 374 within woocommerce.php (this will differ depending on your theme, and some themes may not have a modified woocommerce.php file. If that is the case, just drag woocommerce.php into your theme directory from the plugin.
Code Before Addition/Modification:
function woocommerce_template_loop_product_thumbnail() {
global $product, $woocommerce_loop;
$i = 0;
$attachments = array();
$attachments[] = get_post_thumbnail_id();
$attachments = array_merge( $attachments, $product->get_gallery_attachment_ids() );
$original_size = wc_get_image_size( 'shop_catalog' );
if ( $woocommerce_loop['view'] == 'masonry_item' ) {
$size = $original_size;
$size['height'] = 0;
YIT_Registry::get_instance()->image->set_size('shop_catalog', $size );
}
switch ( $woocommerce_loop['products_layout'] ) {
case 'zoom':
if( isset( $attachments[1] ) ) {
echo '' . woocommerce_get_product_thumbnail() . '';
echo '<div class="attachments-thumbnail">';
while( $i < 3 ){
if( ! isset( $attachments[ $i ] ) ) break;
$src = wp_get_attachment_image_src( $attachments[ $i ], 'shop_catalog' );
$active = ( $i == 0 ) ? 'active' : '';
echo '<div class="single-attachment-thumbnail ' . $active . '" data-img="' . $src[0] . '">';
yit_image( "id=$attachments[$i]&size=shop_thumbnail&class=image-hover" );
echo '</div>';
$i++;
}
echo '</div>';
}
else {
echo '' . woocommerce_get_product_thumbnail() . '';
}
break;
case 'flip':
if( isset( $attachments[1] ) ) {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
echo '<span class="face back">';
yit_image( "id=$attachments[1]&size=shop_catalog&class=image-hover" );
echo '</span></a>';
}
else {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
}
break;
}
Code After Addition/Modification:
function woocommerce_template_loop_product_thumbnail() {
global $product, $woocommerce_loop;
if(!is_single() ) {
if( $product->is_type( 'external' ) ){
$product_url = $product->get_product_url() . '"target="_blank""';
} else( $producenter code heret_url = get_permalink());
} else ($product_url = get_permalink());
$i = 0;
$attachments = array();
$attachments[] = get_post_thumbnail_id();
$attachments = array_merge( $attachments, $product->get_gallery_attachment_ids() );
$original_size = wc_get_image_size( 'shop_catalog' );
if ( $woocommerce_loop['view'] == 'masonry_item' ) {
$size = $original_size;
$size['height'] = 0;
YIT_Registry::get_instance()->image->set_size('shop_catalog', $size );
}
switch ( $woocommerce_loop['products_layout'] ) {
case 'zoom':
if( isset( $attachments[1] ) ) {
echo '' . woocommerce_get_product_thumbnail() . '';
echo '<div class="attachments-thumbnail">';
while( $i < 3 ){
if( ! isset( $attachments[ $i ] ) ) break;
$src = wp_get_attachment_image_src( $attachments[ $i ], 'shop_catalog' );
$active = ( $i == 0 ) ? 'active' : '';
echo '<div class="single-attachment-thumbnail ' . $active . '" data-img="' . $src[0] . '">';
yit_image( "id=$attachments[$i]&size=shop_thumbnail&class=image-hover" );
echo '</div>';
$i++;
}
echo '</div>';
}
else {
echo '' . woocommerce_get_product_thumbnail() . '';
}
break;
case 'flip':
if( isset( $attachments[1] ) ) {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
echo '<span class="face back">';
yit_image( "id=$attachments[1]&size=shop_catalog&class=image-hover" );
echo '</span></a>';
}
else {
echo '<span class="face">' . woocommerce_get_product_thumbnail() . '</span>';
}
break;
}
Code Added:
if( $product->is_type( 'external' ) ){
$product_url = $product->get_product_url() . '"target="_blank""';
} else( $product_url = get_permalink());
} else ($product_url = get_permalink());
Code Modified:
With the exception of the code that was added above, replace all instances of get_permalink() with $product_url.
Figured out a work around. Since the SalesFlash image was the one being triggered, I just used a blank PNG image to overlay ontop of the product image. Turned all my products into sale items and it works. Not perfect, but I don't need the sale icon anyway.
But if anyone does know of a proper programming solution, I would change it. Thanks.
this on worked for me in the content-product.php without asking the meta data
<div class="thumbnail-wrapper"><a href="<?php echo $product->product_url; ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?> </a>
</div
I did the same in thing in loop/add-to-cart.php for the "Add More"
and "Details" buttons in lines 21 get_permalink() and 57 $link by replacing them respectively like this:
LINE 27
$details = sprintf('%s', get_permalink(), apply_filters('yit_details_button', __( 'Details', 'yit' )), apply_filters('yit_details_button', __( 'Details', 'yit' )) );
REPLACE get_permalink() with $product->product_url
AND IN LINE 57
$add_to_cart = sprintf('%s', apply_filters( 'yit_external_add_to_cart_link_loop', $link, $product ), $label, $label);
REPLACE again $link with $product->product_url .
I had no problems untill now. I was wondering if you Finally found a clear solution so we could do the same thing for thumnails and product images ,without adding a blank image on top of them.Im not much of code so i would appreciate if someone Knows how to put an external link on the product images on the front page.Thank you

Categories