如何在特定产品类别 [WooCommerce] 的 "Proceed to checkout" 按钮上添加自定义 URL

How to add a custom URL on the "Proceed to checkout", button for a specific product category [WooCommerce]

我正在尝试在特定产品类别的 "Proceed to checkout" 按钮上添加自定义 URL。

我已经使用以下代码成功添加了自定义 URL,但是现在我想根据购物车中的产品显示不同的 URL。

add_filter('woocommerce_get_checkout_url', 'dj_redirect_checkout');

function dj_redirect_checkout($url) {
     global $woocommerce;
     if(is_cart()){
          $checkout_url = 'https://example.com';
     }
     else { 
     //other url or leave it blank.
     }
     return  $checkout_url; 
}

例如,我的 WooCommerce 网站上有两个类别(Tradeline 1 和 Tradeline 2)。如果其他类别在购物车中,我想在 "Proceed to checkout" 按钮上放置一个不同的 URL。

如有任何帮助,我们将不胜感激。

非常感谢。

您应该遍历购物车项目并检查是否有任何项目具有您想要定位的条款之一。您应该使用 "Tradeline 1"、"Tradeline 2" 的 slug,可能是 "tradeline-1".

这是一个快速 POC:

$cat_in_cart = false;

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product = $cart_item['data'];

    if ( has_term( 'tradeline-1', 'product_cat', $product->get_id() ) ) {
        $cat_in_cart = true;
        break;
    }
}

if( $cat_in_cart == true ) // do stuff

有用参考:https://businessbloomer.com/woocommerce-check-product-category-cart/

干杯!