【问题标题】:Change URL of “View Cart” button on WooCommerce Pages other than Cart and Checkout更改购物车和结帐以外的 WooCommerce 页面上“查看购物车”按钮的 URL
【发布时间】:2020-08-10 02:37:27
【问题描述】:

我正在应用以下代码来更改商店页面上“查看购物车”按钮的 URL。

// Change View Cart Button URL from /cart to /checkout
add_filter( 'woocommerce_add_to_cart_redirect', 'cart_to_checkout' );
 
function cart_to_checkout( $url ) {
    return wc_get_checkout_url();
}

我需要使用什么过滤器来实现单一产品页面上“Woocommerce 错误消息”中的“查看购物车”按钮的相同效果?

Woocommerce 错误消息”显示如下:“‘产品 X’的最大允许数量为 3(您目前在您的购物车中有 3 个)。 “查看购物车”

所以,我需要更改上述消息中“查看购物车”按钮的 URL。

此外,所有查看购物车按钮都必须指向结帐页面,而不是购物车页面。

谢谢!

【问题讨论】:

  • 这可以满足您的需要吗? apply_filters( 'woocommerce_get_cart_url', wc_get_page_permalink( 'checkout' )) 本质上应该将查看购物车页面更改为始终转到结帐页面
  • 谢谢。但它没有用。

标签: php wordpress woocommerce


【解决方案1】:

因为这个通知是硬编码在WC_Cartadd_to_cart()方法中的(from line 1075 to 1083)

throw new Exception(
    sprintf(
        '<a href="%s" class="button wc-forward">%s</a> %s',
        wc_get_cart_url(),
        __( 'View cart', 'woocommerce' ),
        /* translators: 1: quantity in stock 2: current quantity */
        sprintf( __( 'You cannot add that amount to the cart &mdash; we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) )
    )
);

更改url链接的唯一方法是使用woocommerce_get_cart_url过滤钩located inside the function wc_get_cart_url()(在本通知中使用)如下仅适用于单个产品页面

add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
    // Only on single product pages
    if( is_product() )
        $url = wc_get_checkout_url();
    
    return $url;
}

要将使用wc_get_cart_url()所有网址链接更改为结帐网址,您将改用:

add_filter( 'woocommerce_get_cart_url', 'wc_get_checkout_url' );

补充:要使其在除购物车和结帐页面之外的任何地方都能正常工作,请使用以下内容:

add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
    // Except on cart and checkout pages
    if( ! ( is_cart() || is_checkout() ) )
        $url = wc_get_checkout_url();
    
    return $url;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


相关:Change the "view cart" product overlay button on product loops in Woocommerce

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    相关资源
    最近更新 更多