【问题标题】:WooCommerce: change cart button text in loop to iconWooCommerce:将循环中的购物车按钮文本更改为图标
【发布时间】:2021-01-17 14:43:15
【问题描述】:

我想将存档页面上的购物车按钮文本更改为图标。

我找到了一个可以做到这一点的 sn-p。但这也改变了购物车按钮的链接:

add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_add_to_cart_button', 10, 2 );
function ts_replace_add_to_cart_button( $button, $product ) {
    if (is_product_category() || is_shop()) {
        $button_text = __("View Product", "woocommerce");
        $button_link = $product->get_permalink();
        $button = '<a href="' . $button_link . '">' . $button_text . '</a>';
    return $button;
    }
}

有没有办法只改变按钮的文本?

我知道,我可以更改模板文件/loop/add-to-cart.php。但我需要一个基于函数的解决方案。

【问题讨论】:

  • 你有输出的元素结构吗?
  • 你的意思是HTML? &lt;a href="?add-to-cart=7827" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="7827" data-product_sku="12800007827" aria-label="Name" rel="nofollow"&gt;Add to cart&lt;/a&gt;

标签: php wordpress woocommerce cart hook-woocommerce


【解决方案1】:

这应该可以解决问题。您可以调整条件语句。现在它涵盖了任何taxonomy.php 页面,这是产品的存档页面(Woocommerce),以及常规的archive.php 页面。

<?php
/**
* do_action( 'wp_customize_add_to_cart_archive' )
* This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
* @link https://developer.wordpress.org/reference/hooks/wp_loaded/
*/
add_action( 'wp_customize_add_to_cart_archive', function() {
  function wp_customize_add_to_cart_archive( $subject ) {
    if( ! is_admin() && is_archive() || is_tax() ) {
      $search = [
        '/>Add to cart</', // ... >< with the brackets, to be sure to target the right Home word
        // ... etc.
      ];
      $replace = [
        '><i class="fas fa-play-circle"></i><', // ... Our replacement for Add to cart using a font awesome icon
        // ... etc.
      ];
      $subject = preg_replace( $search, $replace, $subject );
      return $subject;
    };
  };
  ob_start( 'wp_customize_add_to_cart_archive' );
} ); ?>

了解详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-10
    • 2016-03-20
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多