首页 > 解决方案 > 修改购物车和结帐表中的产品名称

问题描述

TL;博士

我想在购物车和结帐时都更改产品名称,因此会显示属性名称的名称。

就像现在一样:我的产品名称 - 蓝色

如我所愿:我的产品名称 - 颜色:蓝色


细节

我可以在案例中看到,正是这些行打印了它:

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

    do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );

    // Meta data.
    echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.

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

我在这里找到了另一个帖子:Add Product ID after the cart item,建议如下:

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'].')';
}

..但这附加到产品名称。我想修改现有的。


解决方案尝试 1

我在这里找到了解决方法。

这是:

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

理想情况下,我想控制自己,如何显示每个变化。所以这不是一个完整的修复,而是朝着正确方向迈出的一步。

额外问题我自己怎么能找到这两个过滤器?

我在购物车的 WooCommerce 代码中看不到它们

标签: woocommerce

解决方案


推荐阅读