2016-06-03 3 views
1

맞춤형 조건부 출력을 위해 노력하고 있습니다. 판매 가격이있는 제품 루프가 발견되면 판매 가격 태그에 클래스를 추가합니다. 정상 가격 만 있으면이 클래스를 일반 가격표에 추가합니다.제품 주위의 조건부 맞춤 출력 판매 가격 및 일반 가격

나는이 다른 문서화으로부터 &에보고 한 후 작업을 얻이 수없는 것 : 나는 스팬 클래스 = "양"태그를 변경하려고 정상 가격 필터 &을 사용하고

add_filter('woocommerce_get_price_html', 'custom_price_html', 100, 2); 
function custom_price_html($price, $product){ 
    ob_start(); 
     global $product; 
     if (isset($product->sale_price)) { 
      return str_replace('</del>', '<span class="amount">text</span></del>', $price); 
      return str_replace('</ins>', '<span class="highlight amount">highlight here</span></del>', $price); 
     } 
     else { 
      return str_replace('</ins>', '<span class="highlight amount">highlight here</span>text</del>', $price); 
     } 
} 

을 스팬 클래스 = "금액", 그러나 나는 여전히 동일한 출력을 얻을 수 있습니다.
아이디어가 있으십니까?

add_filter('woocommerce_price_html', 'price_custom_class', 10, 2); 
function price_custom_class($price, $product){ 
    return str_replace('<span class="amount"></span>', '<ins><span class="amount">'.woocommerce_price($product->regular_price ).'</span></ins>', $price); 
} 

답변

2

이 후크 변화는 2 개 변수 ($price$instance) 대신 echo $price 당신의 return $price)을 가진 필터이다. 이 방법으로 사용하려고 할 수 있습니다.

add_filter('woocommerce_sale_price_html','price_custom_class', 10, 2); 
function price_custom_class($price, $product){ 
    if (isset($product->sale_price)) { 
     $price = '<del class="strike">'.woocommerce_price($product->regular_price).'</del> 
     <ins class="highlight">'.woocommerce_price($product->sale_price).'</ins>'; 
    } 
    else 
    { 
     $price = '<ins class="highlight">'.woocommerce_price($product->regular_price).'</ins>'; 
    } 
    return $price; 
} 

이 후크는 일반적으로 판매 가격으로 판매됩니다.

참조 :

add_filter('woocommerce_price_html', 'price_custom_class', 10, 2); 
function price_custom_class($price, $product){ 
    // your code 
    return $price; 
} 

참조 : woocommerce_price_html

1

함수 또는 메서드를 특정 필터 동작에 연결하려면 여기에 필터 후크가 필요합니다.

add_filter('woocommerce_sale_price_html','price_custom_class'); 
+0

예 내가 필터 훅이 올바른 방법이 될 것입니다 알아낼 5 분 전 후 정상 가격에 대한 woocommerce_sale_price_html

, 당신은 woocommerce_price_html 필터 훅이 그것을 통해 가십시오. 둘 다 sale_price & regular_price를 대체 할 것이라고 생각하면 get_price_html이 더 잘 작동할까요? – mark5

+0

예, get_price_html 함수를 사용하여 호출 할 때 바뀐 값이 표시됩니다. –