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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?