简体   繁体   English

将字体真棒图标添加到 Woocommerce 3 中的自定义添加到购物车按钮

[英]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 … to fa-10x最大(乘法器)fa-2xfa-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.

相关问题 如何用购物车图标和购物车图标以及自定义文本替换Woocommerce的“添加到购物车”按钮? - How to replace Woocommerce “Add to Cart” Button with a Cart Icon and Cart Icon along with custom text? 将WooCommerce上的添加到购物车按钮替换为自定义按钮 - Replace add to cart button with a custom button on WooCommerce 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce Woocommerce 添加到自定义产品的购物车按钮 - Woocommerce Add to cart button for custom product 在WooCommerce的“添加到购物车”按钮旁边添加自定义按钮 - Add Custom Button next to “ADD TO CART” button of WooCommerce 如果在ajax上购物车不为空,则隐藏自定义按钮在Woocommerce中添加到购物车 - Hide a custom button if cart is not empty on ajax add to cart in Woocommerce 隐藏添加到购物车按钮,并在Woocommerce中添加自定义内容 - Hide add to cart button and add custom content after it in Woocommerce 我如何用图标替换“添加到购物车”文本(添加到购物车按钮woocommerce) - How can i replace text “Add to cart” with an icon (Add to cart button woocommerce) 基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮 - Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type woocommerce-删除“添加到购物车”按钮 - woocommerce - Remove “Add to cart” button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM