【问题标题】:Disable item link for specific products in WooCommerce cart checkout and orders在 WooCommerce 购物车结帐和订单中禁用特定产品的项目链接
【发布时间】:2020-08-13 13:13:58
【问题描述】:

我想禁用特定可变产品的购物车商品和订单商品名称的链接。

我发现 Disable item name link for specific product in Woocommerce cart checkout and orders 答案代码确实禁用了单个产品上的链接,但我想知道如何为变量更改它?

【问题讨论】:

  • 欢迎堆栈溢出Rita,请告诉您具体做了什么,并指出问题发生的地方,这样您可以更快地得到答案。

标签: php wordpress woocommerce product permalinks


【解决方案1】:

以下内容适用于所有产品类型(简单、可变、变体……),禁用定义的产品ID数组中的项目链接:

// 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 products IDs in the array
    $product_ids = array(37, 40);

    if( array_intersect($product_ids, array($cart_item['product_id'], $cart_item['variation_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 products IDs in the array
    $product_ids = array(37, 40);

    if( array_intersect($product_ids, array($cart_item['product_id'], $cart_item['variation_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 products IDs in the array
    $product_ids = array(37, 40);

    if( array_intersect($product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
        $item_name = $item->get_name();
    }
    return $item_name;
}

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

【讨论】:

  • 完美,现在我知道如何使用它了。非常感谢。
  • @RitaMaglione 因为这个答案似乎回答了你的问题,你可以请accept 回答,谢谢。
猜你喜欢
  • 2019-08-19
  • 2018-07-10
  • 1970-01-01
  • 2021-01-10
  • 2016-09-04
  • 2017-04-27
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多