在WooCommerce上将“添加到购物车"按钮替换为自定义按钮 [英] Replace add to cart button with a custom button on WooCommerce

查看:53
本文介绍了在WooCommerce上将“添加到购物车"按钮替换为自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将特定产品类别的添加到购物车"按钮替换为 简单产品" .

I would like to replace the 'Add to cart' button for a specific product category and when product type isa a 'simple product'.

我想在可以查看所有产品(商店和存档页面)的页面上以及在单个产品页面上进行此操作.

I would like to do that on the page where I can see all the products (shop and archive pages) and in single product pages.

下面的代码只是将我的添加到购物车"按钮隐藏在我的产品类别的所有帖子中:

The code bellow just hide my add-to cart buttons from all post of my product category:

function western_custom_buy_buttons(){

   $product = get_product();

   if ( has_term( 'categ1', 'product_cat') ){

       // removing the purchase buttons

       remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );

       remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

       remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );

       remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );

       remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );

       remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );

   }

}

add_action( 'wp', 'western_custom_buy_buttons' );

我该如何实现?请帮忙.

How can I achieve that? Any help please.

推荐答案

这是一个完整的工作解决方案,它将通过自定义 了解详情" 按钮.

Here is a complete working solution that will replace all add-to-cart buttons for your defined product category and simple products only, by a custom "read more" button.

此代码已经过测试,可在2.6.x到3.0+的WooCommerce版本上使用:

This code is tested and works on WooCommerce versions from 2.6.x to 3.0+:

// Replacing add-to-cart button in shop pages and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link',
'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );

function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {

    // WooCommerce compatibility
    if ( method_exists( $product, 'get_id' ) ) {
        $product_id = $product->get_id();
    } else {
        $product_id = $product->id;
    }

    if ( has_term( 'categ1', 'product_cat', $product_id ) && $product->is_type( 'simple') ) {
        // Set HERE your button link
        $link = get_permalink($product_id);
        $html = '<a href="'.$link.'" class="button alt add_to_cart_button">'.__("Read More", "woocommerce").'</a>';
    }
    return $html;
}

// Outputing a custom button in Single product pages (you need to set the button link)
function single_product_custom_button( ) {

    global $product;

    // WooCommerce compatibility
    if ( method_exists( $product, 'get_id' ) ) {
        $product_id = $product->get_id();
    } else {
        $product_id = $product->id;
    }

    if ( has_term( 'categ1', 'product_cat', $product_id ) ) {
        // Set HERE your button link
        $link = '#';
        echo '<a href="'.$link.'" class="button alt add_to_cart_button">'.__("Read More", "woocommerce").'</a>';
    }
}

// Replacing add-to-cart button in Single product pages
add_action( 'woocommerce_single_product_summary', 'removing_addtocart_buttons', 1 );
function removing_addtocart_buttons()
{
    global $product;

    // WooCommerce compatibility
    if ( method_exists( $product, 'get_id' ) ) {
        $product_id = $product->get_id();
    } else {
        $product_id = $product->id;
    }

    if ( has_term( 'categ1', 'product_cat', $product_id ) )
    {
        #### Removing the add-to-cart button ####

        ## Simple products
        remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );

        ## Other products types
        // remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
        // remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
        // remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
        // remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        // remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );


        #### Adding a custom replacement button ####

        ## Simple products
        add_action( 'woocommerce_simple_add_to_cart', 'single_product_custom_button', 30 );

        ## Other products types
        // add_action( 'woocommerce_grouped_add_to_cart', 'single_product_custom_button', 30 );
        // add_action( 'woocommerce_variable_add_to_cart', 'single_product_custom_button', 30 );
        // add_action( 'woocommerce_external_add_to_cart', 'single_product_custom_button', 30 );
        // add_action( 'woocommerce_single_product_summary', 'single_product_custom_button', 30 );
        // add_action( 'woocommerce_single_variation', 'single_product_custom_button', 20 );
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

这篇关于在WooCommerce上将“添加到购物车"按钮替换为自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆