2017-08-09 244 views
1

我想隐藏添加到购物车按钮并显示自定义文本而不是按钮。删除或隐藏WooCommerce添加到购物车按钮

我想下面的钩去掉按钮:

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

回答

0

。如果您想完全禁用添加到购物车按钮,请将此代码添加到您主题的functions.php文件中。

add_filter('woocommerce_is_purchasable', false); 

。要添加到购物车按钮后添加一些HTML内容,请尝试此代码。

add_action('woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func'); 

function add_content_after_addtocart_button_func() { 
    echo '<p>Hi, I'm the text after Add to cart Button.</p>'; 
} 
1

这里是你正在寻找(我认为)的方式。

的第一个函数将取代店页面添加到购物车按钮,通过与他们的单品页面正常的按钮,如下图所示:

enter image description here

第二个功能将取代加载到-Cart按钮(和数量),通过自定义的文字与此:

enter image description here

这里是代码:

// Shop and archives pages: we replace the button add to cart by a link to the product 
add_filter('woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2); 
function custom_text_replace_button($button, $product ) { 
    $button_text = __("View product", "woocommerce"); 
    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>'; 
} 

// replacing add to cart button and quantities by a custom text 
add_action('woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0); 
function replacing_template_single_add_to_cart() { 

    // Removing add to cart button and quantities 
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

    // The text replacement 
    add_action('woocommerce_single_product_summary', function(){ 

     // set below your custom text 
     $text = __("My custom text goes here", "woocommerce"); 

     // Temporary style CSS 
     $style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"'; 

     // Output your custom text 
     echo '<p class="custom-text" '.$style_css.'>'.$text.'</a>'; 
    }, 30); 
} 

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

测试和工程

相关问题