[英]Add font awesome icon to custom add to cart button in Woocommerce 3
I changed the style of my Add To Cart with the help of LoicTheAztec,在 LoicTheAztec 的帮助下,我更改了添加到购物车的样式,
but how to add a font awesome icon in front of button text using the followin code但是如何使用以下代码在按钮文本前添加字体真棒图标
// For Woocommerce version 3 and above only
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
if( $product->is_in_stock() ) return $button;
// HERE set your button text (when product is not on stock)
$button_text = __('Not available', 'woocommerce');
// HERE set your button STYLING (when product is not on stock)
$color = "#777"; // Button text color
$background = "#aaa"; // Button background color
// Changing and disbling the button when products are not in stock
$style = 'color:'.$color.';background-color:'.$background.';cursor:not-allowed;';
return sprintf( '<a class="button disabled" style="%s">%s</a>', $style, $button_text );
}
First, if font awesome icons are not enabled in Wordpress for your theme, you might need to add Better Font Awesome plugin.首先,如果 Wordpress 中没有为您的主题启用字体真棒图标,您可能需要添加Better Font Awesome插件。
You can select any Icon code in this fontawesome.com gallery of icons您可以在此fontawesome.com 图标库中选择任何图标代码
Now making a very small change to your code you will be able to add your desired Icon and size:现在对您的代码进行非常小的更改,您将能够添加所需的图标和大小:
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
if( $product->is_in_stock() ) return $button;
// HERE set your button text (when product is not on stock)
$button_text = __('Not available', 'woocommerce');
// HERE set your button STYLING (when product is not on stock)
$color = "#555"; // Button text color
$background = "#aaa"; // Button background color
// HERE set your fontawesome icon code and size
$icon = 'fa-ban';
$size = 'fa-lg'; // large - To disable size use an empty value like $size = '';
// Changing and disbling the button when products are not in stock
$style = 'color:'.$color.';background-color:'.$background.';cursor:not-allowed;';
return sprintf( '<a class="button disabled" style="%s"><i class="fa %s %s"></i> %s</a>', $style, $icon, $size, $button_text );
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。
You will get something like:你会得到类似的东西:
Font awesome possible sizes values:字体真棒可能的大小值:
- smallest:
fa-xs
最小的:fa-xs
- smaller:
fa-sm
较小:fa-sm
- larger:
fa-lg
更大:fa-lg
- largest (multiplicator) :
fa-2x
,fa-3x
… tofa-10x
最大(乘法器) :fa-2x
、fa-3x
……到fa-10x
SOLVED解决了
Add font awesome icon to custom add to cart button in Woocommerce 3 Copy Below Code and paste it to your theme functions.php将字体真棒图标添加到 Woocommerce 3 中的自定义添加到购物车按钮复制下面的代码并将其粘贴到您的主题functions.php
/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton"><i class="fa fa-shopping-bag"></i></a>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.