【问题标题】:Add the product ID after the cart item name in Woocommerce cart page在 Woocommerce 购物车页面中的购物车商品名称后添加产品 ID
【发布时间】:2018-06-03 16:17:38
【问题描述】:

我希望在 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


    【解决方案1】:

    您的挂钩函数中缺少一些参数,您需要进行一些更改才能以这种方式获取产品 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 文件中或任何插件文件中。

    经过测试并且有效。

    【讨论】:

    • 这正是我想要的。谢谢!
    • 你应该去掉“echo”之后的第一个单引号——它会破坏函数。
    • @LoicTheAztec 谢谢你,如何将其应用于特定产品而不是所有产品?
    【解决方案2】:

    试试这个,

    覆盖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>
    

    希望对你有帮助。

    【讨论】:

    • 这确实有效,但我很想通过 functions.php 路由来实现这一点。
    猜你喜欢
    • 2018-07-04
    • 2017-04-27
    • 2015-05-03
    • 2018-04-10
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多