doubi9255
2017-10-20 06:14 阅读 64
已采纳

禁用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 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;
    }
    
    点赞 评论 复制链接分享

相关推荐