Disable item name link for specific product in Woocommerce cart checkout and orders

﹥>﹥吖頭↗ 提交于 2020-08-23 07:59:28

问题


I'm looking to disable the product link to the product page of a specific product in the cart. This product is a gift product automatically added to the cart when the cart subtotal amount equals a particular value.

I know it's possible to do this with all the cart items. But I'm not quite sure on how to target a specific item.


回答1:


New answer that works for all product types for an array of defined products Ids, here:
Disable item link for specific products in WooCommerce cart checkout and orders

Updated: Added a hooked function to handle minicart

To remove the item name link from cart, checkout and orders, use the following:

// 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;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/55408675/disable-item-name-link-for-specific-product-in-woocommerce-cart-checkout-and-ord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!