如何在我的购物车和结账时显示简短描述
how to display the short description in my cart and the checkout
我想在我的购物车和结帐中显示我的产品的简短描述而不是它的名称 我去了 cart.php 文件,我找到了一个代码,我想我必须更改它
有人会为我提供解决方案吗?
<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 ) . ' ' );
} 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 ) );
}
更好的解决方案是使用钩子。例如:
add_filter( 'woocommerce_cart_item_name', 'ywp_custom_text_cart_item_name', 10, 3 );
function ywp_custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$item_name .= '<br /><div class="my-custom-text-class">Custom text comes here</div>';
return $item_name;
}
代码进入您的活动 theme/child 主题的 functions.php
文件。已测试并有效。
如果您想在购物车和结帐时显示简短描述,您可以使用 woocommerce_cart_item_name
挂钩。
如果您想显示所有产品 类型的简短描述,您可以使用此代码:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// get short description
$short_description = $product->get_short_description();
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
如果在产品变体的情况下,您想显示父产品(可变产品)的简短描述,您可以使用以下代码:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// if it is a variation it gets the description of the variable product
if ( $product->is_type( 'variation' ) ) {
$parent_id = $product->get_parent_id();
$variable = wc_get_product( $parent_id );
$short_description = $variable->get_short_description();
} else {
// get short description
$short_description = $product->get_short_description();
}
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
它在购物车页面和结帐页面都有效。
代码必须添加到主题的 functions.php 文件中。
我想在我的购物车和结帐中显示我的产品的简短描述而不是它的名称 我去了 cart.php 文件,我找到了一个代码,我想我必须更改它 有人会为我提供解决方案吗?
<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 ) . ' ' );
} 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 ) );
}
更好的解决方案是使用钩子。例如:
add_filter( 'woocommerce_cart_item_name', 'ywp_custom_text_cart_item_name', 10, 3 );
function ywp_custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$item_name .= '<br /><div class="my-custom-text-class">Custom text comes here</div>';
return $item_name;
}
代码进入您的活动 theme/child 主题的 functions.php
文件。已测试并有效。
如果您想在购物车和结帐时显示简短描述,您可以使用 woocommerce_cart_item_name
挂钩。
如果您想显示所有产品 类型的简短描述,您可以使用此代码:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// get short description
$short_description = $product->get_short_description();
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
如果在产品变体的情况下,您想显示父产品(可变产品)的简短描述,您可以使用以下代码:
// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// if it is a variation it gets the description of the variable product
if ( $product->is_type( 'variation' ) ) {
$parent_id = $product->get_parent_id();
$variable = wc_get_product( $parent_id );
$short_description = $variable->get_short_description();
} else {
// get short description
$short_description = $product->get_short_description();
}
// if it exists, it returns the short description as the cart item name
if ( ! empty( $short_description ) ) {
return $short_description;
} else {
return $item_name;
}
return $item_name;
}
它在购物车页面和结帐页面都有效。
代码必须添加到主题的 functions.php 文件中。