0

我在购物车页面中显示带有产品标题的 SKU 值,但现在每个单词 SKU 和产品标题都单独链接。我怎样才能只显示单个链接而不是单独链接。

用于在购物车页面中显示 SKU 值在 Functions.php中添加了此代码

add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);
function add_sku_in_cart( $title, $values, $cart_item_key ) {
$sku = $values['data']->get_sku();
$url = $values['data']->get_permalink( $product->ID );
$final='<a href="'. $url .'">SKU: '. $sku .'</a>';
return $title ?  sprintf("%s  - ", $final) .$title : $final;
}       

示例 现在显示这个

<a href="http://localhost/test/?product=child-product">SKU: asda121 </a> - <a href="http://localhost/test/?product=child-product">Child Product</a>

但我想要那样

<a href="http://localhost/test/?product=child-product">SKU: asda121 - Child Product</a>                               
4

3 回答 3

2

您应该以woocommerce_cart_item_name这种方式使用在过滤器挂钩中挂钩的自定义函数,以在 Sku 和项目名称上获得相同的链接(当 sku 存在时)

add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {

    $product = $cart_item['data'];
    $sku = $product->get_sku();

    // When sku doesn't exist
    if(empty($sku)) return $item_name;

    $product_name = $product->get_name();
    $product_id = $product->get_id();
    $url = $product->get_permalink( $product_id );

    return '<a href="'. $url .'">Sku: ' . $sku . ' - ' .$product_name . '<a>';
}

代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

测试和工作

于 2017-08-09T12:59:24.723 回答
0
add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);

function add_sku_in_cart( $title, $values, $cart_item_key ) {

    $sku = $values['data']->get_sku();
    $url = $values['data']->get_permalink( $product->ID );
    $final='SKU: '. $sku . ($title ? ' - ' . $title : '');
    return '<a href="'. $url .'">' . $final . '</a>';
} 
于 2017-08-09T08:12:17.710 回答
-1
add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);
function add_sku_in_cart(){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names = array();

foreach($items as $item => $values) { 

    // Retrieve WC_Product object from the product-id:
    $_woo_product = wc_get_product( $values['product_id'] );

    // Get SKU from the WC_Product object:
    $product_names[] = $_woo_product->get_sku(); 
 }
} 
于 2017-08-09T08:51:59.627 回答