doubi9255 2017-10-19 22: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-19 23: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 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
  • ¥15 JNA 方法调用.dll异常
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部