用WooCommerce上的自定义按钮替换添加到购物车按钮

时间:2017-05-11 11:52:33

标签: php wordpress woocommerce categories product

如果产品类型为 'simple product' ,我想替换特定产品类别的“添加到购物车”按钮。

我想在我可以看到所有产品(商店和存档页面)和单个产品页面的页面上这样做。

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

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' );

我怎样才能实现这一目标?请帮忙。

1 个答案:

答案 0 :(得分:5)

这是一个完整的解决方案,它将通过自定义 "read more" 按钮替换您定义的产品类别和简单产品的所有“按购物车”按钮。

此代码已经过测试,适用于从2.6.x到3.0 +的WooCommerce版本:

// 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文件中,或者放在任何插件文件中。