在 Woocommerce 购物车结账和订单中禁用特定产品的商品名称链接

2023-12-30

我希望禁用购物车中特定产品的产品页面的产品链接。该产品是当购物车小计金额等于特定值时自动添加到购物车的礼品产品。

我知道可以对所有购物车商品执行此操作。但我不太确定如何针对特定项目。


适用于的新答案所有产品类型对于已定义产品 ID 的数组,请在此处:
在 WooCommerce 购物车结账和订单中禁用特定产品的商品链接 https://stackoverflow.com/questions/63395992/disable-item-name-link-for-specific-variable-product-in-woocommerce-cart-checkou/63396318#63396318

Updated: 添加了一个挂钩函数来处理迷你车

要从购物车、结账和订单中删除商品名称链接,请使用以下命令:

// Cart item link
add_filter( 'woocommerce_cart_item_name', 'conditionally_remove_link_from_cart_item_name', 10, 3 );
function conditionally_remove_link_from_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    // HERE set your Free product ID
    $gift_product_id = 37;
    
    if( $gift_product_id == $cart_item['data']->get_id() ) {
        $item_name = $cart_item['data']->get_name();
    }
    return $item_name;
}

// Mini-cart item link
add_filter( 'woocommerce_cart_item_permalink', 'conditionally_remove_cart_item_permalink', 10, 3 );
function conditionally_remove_cart_item_permalink( $permalink, $cart_item, $cart_item_key ) {
    // HERE set your Free product ID
    $gift_product_id = 37;
    
    if( $gift_product_id == $cart_item['data']->get_id() ) {
        $permalink = '';
    }
    return $permalink;
}

// Order item link
add_filter( 'woocommerce_order_item_name', 'conditionally_remove_link_from_order_item_name', 10, 2 );
function conditionally_remove_link_from_order_item_name( $item_name, $item ) {
    // HERE set your Free product ID
    $gift_product_id = 37;

    if( $gift_product_id == $item->get_product_id() ) {
        $item_name = $item->get_name();
    }
    return $item_name;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Woocommerce 购物车结账和订单中禁用特定产品的商品名称链接 的相关文章

随机推荐