사용 가능한 여러 값이있는 경우 쉼표로 구분 된 제품 속성 용어
Jan 13 2021
WooCommerce에서는 현재 상점 페이지의 일부 제품 속성을 반영하는 기능을 구축하고 있습니다. 여러 속성 값을 사용할 수있는 경우 쉼표로 구분하고 싶지만 방법을 모르겠습니다.
내 코드 :
add_action('woocommerce_after_shop_loop_item_title', 'TitleVariations', 10);
function TitleVariations()
{
global $product; $colormonth = $product->get_attribute('color-month'); $finish = $product->get_attribute('finish'); $design = $product->get_attribute('design'); echo '<span class="variation-display">'; echo __($colormonth, 'woocommerce');
echo __($finish, 'woocommerce'); echo __($crossdesign, 'woocommerce');
echo '</span>';
}
답변
2 LoicTheAztec Jan 13 2021 at 22:50
WC_Product 메소드 get_attribute()
는 둘 이상의 값이있을 때 쉼표로 구분 된 값 문자열을 제공합니다. 또한 각기 다른 속성이 목록에 하나의 용어를 가지고 있는지 확인해야합니다.
제품 속성 라벨 이름을 얻으려면 wc_attribute_label()
제품 속성 기능을 사용할 수 있습니다 .
1). 라벨 이름과 용어 값 (한 줄에 각각 다른 속성)이있는 각 제품 속성을 가져 오려면 대신 다음을 사용합니다.
이 코드 는 사용자 지정 제품 속성도 처리합니다 .
add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
global $product; // Here define your product attribute names (slugs) $attribute_names = array('color-month', 'finish', 'design');
$attributes = array(); // Initializing // Loop Through product attributes array foreach( $attribute_names as $attribute_name ) { if( taxonomy_exists( 'pa_' . $attribute_name ) ) {
$attribute = 'pa_' . $attribute_name; // Custom taxonomy
} else {
$attribute = $attribute_name; // Custom attribute (not a taxonomy)
}
$values_str = $product->get_attribute($attribute); if ( $values_str ) {
$attributes[] = '<strong>' . wc_attribute_label($attribute) . ':</strong> ' . $values_str; } } // Output product attribute label / values pairs (one by line) if( ! empty( $attributes ) ) {
echo '<span class="variation-display">' . implode( '<br>', $attributes ) . '</span>';
}
}
2). 그러나 모든 제품 속성 용어를 쉼표로 구분 된 문자열 로 가져 오려는 경우 코드는 Woocommerce 아카이브 페이지의 제품 제목 아래에 특정 제품 속성 표시 와 유사 합니다 .
따라서 코드 :
add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
global $product;
$color_month = $product->get_attribute('color-month');
$finish = $product->get_attribute('finish');
$design = $product->get_attribute('design');
$attributes = array(); // Initializing if ( $color_month ) {
$attributes[] = $color_month;
}
if ( $finish ) { $attributes[] = $finish; } if ( $design ) {
$attributes[] = $design;
}
// Output product attribute values
if( ! empty( $attributes ) ) { echo '<span class="variation-display">' . implode( ', ', $attributes ) . '</span>';
}
}
코드는 활성 자식 테마 (또는 활성 테마)의 functions.php 파일에 들어갑니다. 작동합니다.
3 u_mulder Jan 13 2021 at 22:14
배열에 값을 수집 한 다음 implode
이 배열 :
$values = [ __($colormonth, 'woocommerce'),
__($finish, 'woocommerce'), __($crossdesign, 'woocommerce'),
];
// if some values returned by `__()` are empty strings,
// you can filter your array so as to remove them
$values = array_filter($values);
echo '<span class="variation-display">';
echo implode(', ', $values);
echo '</span>';