在Woocommerce购物车页面中的购物车项目名称后面添加产品ID

问题描述 投票:2回答:2

我想在woocommerce中加入'woocommerce_cart_item_name'过滤器,并希望在名称后面显示产品ID。到目前为止我正在使用这个...

add_filter( 'woocommerce_cart_item_name', 'justatest' );

function justatest( $productname ) {
    echo $productname;
    // ideally echo name and product id here instead
}

这将返回名称及其周围的链接,但我想在项目名称后面添加产品的实际ID。

如何在Woocommerce购物车页面中的购物车项目名称后添加产品ID?

我知道我不需要先退回,因为那样会让我退出这个功能,但我很好奇我将如何做到这一点。

php wordpress woocommerce cart hook-woocommerce
2个回答
2
投票

您的钩子函数中有一些缺少的参数,您应该进行一些更改以通过这种方式获取产品ID:

add_filter( 'woocommerce_cart_item_name', 'just_a_test', 10, 3 );
function just_a_test( $item_name,  $cart_item,  $cart_item_key ) {
    // Display name and product id here instead
    echo '$item_name.' ('.$cart_item['product_id'].')';
}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。

经过测试和工作。


0
投票

试试这个,

覆盖woocommerce的以下页面模板,

woocommerce/cart/cart.php in your theme

你会发现一个表HTML /代码。

<td class="product-name" data-title="<?php esc_attr_e('Product', 'woocommerce'); ?>"
<?php
if (!$product_permalink) {
   echo apply_filters('woocommerce_cart_item_name', $_product->get_name(). $product_id, $cart_item, $cart_item_key) . '&nbsp;';
} else {
   echo apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($product_permalink), $_product->get_name() . $product_id), $cart_item, $cart_item_key);
}

// Meta data
echo WC()->cart->get_item_data($cart_item);

// Backorder notification
if ($_product->backorders_require_notification() && $_product->is_on_backorder($cart_item['quantity'])) {
   echo '<p class="backorder_notification">' . esc_html__('Available on backorder', 'woocommerce') . '</p>';
}
?>
</td>

在cart.php中添加此部分

<td class="product-name">...</td>

希望这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.