doubi9255 2017-10-20 06:14
浏览 86
已采纳

禁用Woocommerce中按钮的默认/?add-to-cart =动画

I would like to remove the default /?add-to-cart= animation buttons on the Shop Archive page. The default mode on a simple product shows a little spinner after clicking the button before showing a tick and providing a "View Cart" option link. (See here: https://snag.gy/ZreQid.jpg)

I would like the simple product Add to Cart button to redirect to the product page in the same way that the variable product does. (i.e. I want the client to view the product page after clicking the button, and I'll change "Add to Cart" label to "View Product" via the Booster for Woocommerce plugin).

So, I found this code on this thread:

/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */

function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');

/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */

add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('<br>[button link="' . esc_attr($link) . '"]VIEW PRODUCT[/button]');
}

The first step works consistently well. The second step works some of the time. For example, on one site, Step 2 works a charm. On a second site (with the same theme, at the same host, etc. etc.), the button displays like this:

[button link="https://example.com/product/product-name/"]VIEW PRODUCT[/button]

i.e. the button doesn't show; this raw code is on display and it's not clickable. See here: https://snag.gy/FxEPWh.jpg

I hope I've explained it properly.

Can anyone help to explain why the above code is not working consistently across different sites? Is there an easier way to do this?

  • 写回答

1条回答 默认 最新

  • dtvfxzq3802 2017-10-20 07:51
    关注

    Here is the solution

    add_filter('woocommerce_loop_add_to_cart_link','change_simple_shop_add_to_cart',10,2);
    function change_simple_shop_add_to_cart( $html, $product ){
        if( $product->is_type('simple')) {
    
            $html = sprintf( '<a rel="nofollow" href="%s" data-product_id="%s"  class="button">%s</a>',
                    esc_url( get_the_permalink() ),
                    esc_attr( $product->get_id() ),
                    esc_html(  __( 'View Product', 'woocommerce' ) )
            );
        }
        return $html;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题