在"我的帐户>下载"页面上的产品标题下显示所选变体的名称



默认情况下,WooCommerce在标题中显示可变产品的属性,我使用以下代码在购物车和结帐页面中显示标题下方的属性:

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

但这在我的帐户页面不起作用,用户看到的是没有属性的完整产品名称。

为了修复它,我使用下面的代码来显示产品标题中的属性:

function show_attributes_outside_title_1( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_2( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );

但是我想在标题(或新列)下面显示属性,这样更容易阅读,并且与您在购物车和结帐页面中看到的设计相同。

问题的开头部分有些混乱。

您说您想在购物车和结帐页面上显示产品标题下的属性,但然后返回__return_false,您打算做相反的事情吗?

解决方案# 1

您可能想要反转检查确保您选择的产品变体属性显示在您的帐户页面下载下的产品名称(正如你上面的评论所证明的):

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_1( $enabled ) {
if ( is_account_page() ) {
$enabled = true;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
function show_attributes_outside_title_2( $enabled ) {
if ( ! is_account_page() ) {
$enabled = false;
}
return $enabled;
}

解决方案# 2

如果你想让问题中的代码保持不变,你可以使用woocommerce_account_downloads_column_download-product钩子,其中download-product是产品名列的id(在/my-account/downloads/页面)。这里是文档。

最后,使用wc_get_formatted_variation函数,您可以获得所选变量的名称. 有关参数的更多信息,请阅读文档。

// shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the produc
$product_name = $download['product_name'];
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $product_name ) . '</a>';
} else {
echo esc_html( $product_name );
}
}

代码已经过测试并正常工作。将其添加到活动主题的functions.php.

我稍微更改了Vincenzo的答案,使其看起来与我在购物车和Checkout页面中看到的属性相同。下面是代码,以防有人需要它:

// Shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the product
$product_name = $download['product_name'];
// define variable
$product_attributes = '';
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name();
$product_attributes = wc_get_formatted_variation( $product, true, true, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $product_name ) . '</a><p>' . esc_html( $product_attributes ) . '</p>';
} else {
echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
}
}
// Shows variation outside title in cart and checkout pages
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

最后两个过滤器替换了我的代码,第一部分解决了下载页面中的问题。

最新更新