WooCommerce 教程:添加到購物車在新標籤頁打開網址

WooCommerce 教程:添加到購物車在新標籤頁打開網址

大多數情況下,在 WooCommerce 中使用外部產品時,您可能希望不僅將 “添加到購物車” 更改為其他內容,而且還可以在新的標籤/窗口中打開鏈接。這是我如何做到的!

PHP 片段:更改添加到購物車 URL 在一個新的窗口中打開


// Product Loop Add to cart target blank

add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );

function add_target_blank( $link, $product ){

// I simply added target="_blank" in the line below
$link = sprintf( '<a href="%s" target="_blank" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->product_type ),
        esc_html( $product->add_to_cart_text() )
    );
return $link;
}

如何添加此代碼?

1 、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到子主題的 style.css 文件底部)修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。

2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的【自定義】,複製代碼添加到自定義 css 中。

此代碼是否可用?

如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用戶也希望您的參與。

文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者風間

在哪裡跌倒,就在哪裡趴着。