it-source

WooCommerce - 속성이 존재하는지 확인한 후 변수 속성 값을 지정합니다.

criticalcode 2023. 2. 8. 17:59
반응형

WooCommerce - 속성이 존재하는지 확인한 후 변수 속성 값을 지정합니다.

slug 'short-title'로 woocommerce 제품의 새로운 속성을 만들었습니다.숍 페이지의 제목을 쇼트 타이틀로 덮어쓰고 싶다는 생각입니다.다음과 같은 루프가 설정되어 있습니다.

if (get_post_meta($product->id, 'short-code', true)) {
$product_name =  (get_post_meta($product->id, 'short-code', true));
} else {
$product_name =  get_the_title();
}
echo '<div class="product-below"><h4 class="product-name">'.$product_name.'</h4>'; 

그러나 제품의 짧은 제목 필드에 값을 입력할 때 제목이 덮어쓰지는 않습니다.get_post_meta와는 다른 기능이 필요할 것 같습니다.

다음 작업을 수행할 수 있습니다.

global $product;
$product_name = $product->get_attribute( 'short-code' );

대신

$product_name = get_post_meta($product->id, 'short-code', true)

그리고 만약 당신에게 효과가 없다면 추가된 속성에 대한 slug도 한번 확인해 주세요.short-code또는short_code

언급URL : https://stackoverflow.com/questions/28955642/woocommerce-check-if-attribute-exists-and-then-give-variable-attribute-value

반응형