【问题标题】:Replace Add to cart button on Woocommerce Single Product Pages for a product category替换产品类别的 Woocommerce 单一产品页面上的“添加到购物车”按钮
【发布时间】:2018-08-08 05:13:59
【问题描述】:

我正在尝试添加一个指向联系页面的自定义链接按钮 - 在第一个 if 条件下,在按钮上显示带有自定义 URL 的“联系我们”文本而不是“添加到购物篮”按钮。

如何做到这一点?到目前为止,这是我的代码。它显示属于“64”类别的每个产品的自定义按钮文本。这正是我想要的。但是如何将该按钮更改功能从购物车按钮添加到自定义链接按钮?我想如果必须更改此购物车按钮功能。如何?

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );

function woo_custom_cart_button_text() {
    global $product;
    $cat_id = 64;

    $product->get_category_ids();
    if ( in_array( $cat_id, $product->get_category_ids() ) ) {
        return __( 'Contact us', 'woocommerce' );
    }
    else {
        return __( 'Add to Basket', 'woocommerce' );
    }
}

【问题讨论】:

  • 那么什么不起作用?期望什么?顺便说一句,为什么不编辑单个产品页面(很可能是 single.php )并在那里添加按钮?为什么要使用过滤器?
  • @cjmling 我只有 10 个产品,所以对服务器来说不是很大的负担。这样我就可以避免可能的更新重置。
  • 而不是将产品放入购物车 id 的购买按钮,而是使用带有自定义链接的按钮

标签: php wordpress button woocommerce product


【解决方案1】:

对于您的产品类别 ID 64,以下代码将用单个产品页面中的自定义按钮和存档页面上的产品链接按钮替换“添加到购物车”按钮:

// The custom replacement button function
function custom_product_button(){
    // HERE your custom button text and link
    $button_text = __( "Custom text", "woocommerce" );
    $button_link = '#';
    
    // Display button
    echo '<a class="button" href="'.$button_link.'">' . $button_text . '</a>';
}

// Replacing the single product button add to cart by a custom button for a specific product category
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;
    
    // Only for product category ID 64
    if( has_term( '64', 'product_cat', $product->get_id() ) ){

        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
        }
    }
}

// Replacing the button add to cart by a link to the product in Shop and archives pages for as specific product category
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Only for product category ID 64
    if( has_term( '64', 'product_cat', $product->get_id() ) ){
        $button_text = __( "View product", "woocommerce" );
        $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }

    return $button;
}

代码进入您的活动子主题(或主题)的 function.php 文件中。

经过测试并且有效。

【讨论】:

  • LoicTheAztec,谢谢。但似乎代码什么也没做。我正在检查它,试图找出缺少的东西。
  • @Wed 我已经在不同的安装上重新测试了我的代码并且它可以工作。它适用于自第 3 版以来的所有 Woocommerce 版本(未在 3 之前测试过的版本)。因此,问题可能来自您的主题(对 woocommerce 挂钩进行更改)、某些插件或您拥有的自定义设置……您还必须确保您的目标产品类别是 ID 64……
  • 我正在重新检查functions.php的其余部分,看看是否有冲突。
  • 很好的答案!它完成了工作。不仅可以更改产品存档页面上的按钮,还可以更改单个产品页面上的按钮。
【解决方案2】:

在钩子中添加过滤器优先级

add_filter('woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text',50);

【讨论】:

  • 它不仅仅是一个文本。我想添加一个功能将购买按钮替换为自定义 URL 按钮
猜你喜欢
  • 2018-03-05
  • 2015-01-15
  • 1970-01-01
  • 2014-12-02
  • 2019-12-13
  • 2021-07-11
  • 2021-06-09
  • 2020-11-15
  • 2020-03-22
相关资源
最近更新 更多