2017-12-13 2 views
0

wc_price 필터의 유용성과 관련하여 의심의 여지가 있습니다. [https://docs.woocommerce.com/wc-apidocs/source-function-wc_price.html#489] wc_price 필터를 찾을 수 있습니다.Woocommerce wc_price 필터 사용

function wc_price($price, $args = array()) { 
    extract(apply_filters('wc_price_args', wp_parse_args($args, array(
     'ex_tax_label'  => false, 
     'currency'   => '', 
     'decimal_separator' => wc_get_price_decimal_separator(), 
     'thousand_separator' => wc_get_price_thousand_separator(), 
     'decimals'   => wc_get_price_decimals(), 
     'price_format'  => get_woocommerce_price_format(), 
    )))); 

    $negative  = $price < 0; 
    $price   = apply_filters('raw_woocommerce_price', floatval($negative ? $price * -1 : $price)); 
    $price   = apply_filters('formatted_woocommerce_price', number_format($price, $decimals, $decimal_separator, $thousand_separator), $price, $decimals, $decimal_separator, $thousand_separator); 

    if (apply_filters('woocommerce_price_trim_zeros', false) && $decimals > 0) { 
     $price = wc_trim_zeros($price); 
    } 

    $formatted_price = ($negative ? '-' : '') . sprintf($price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol($currency) . '</span>', $price); 
    $return   = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>'; 

    if ($ex_tax_label && wc_tax_enabled()) { 
     $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
    } 

    return apply_filters('wc_price', $return, $price, $args); 
} 

즉,이 필터는 제품 가격 변경에 사용됩니다.

이제 코드별로 프론트 엔드 가격을 두 배로 늘리고 싶습니다. 그래서 다음 함수를 써라.

add_filter('wc_price', 'double_price', 10, 3); 

function double_price($return, $price, $args){ 
    $price=$price*2; 
    return $price; 

} 

지금 가격은 통화 기호없이 프론트 엔드에 나와있다.

는 내가 지금은 일하고있어이

function double_price($return, $price, $args){ 
    $price=$price*2; 
    return '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>'.$price.'</span>'; 

} 

처럼 다시.

그러나 이것이 올바른 방법이라고 생각하지 않습니다. 누군가이 기능을 올바르게 사용할 수있는 방법을 설명 할 수 있습니까?. 이 필터에서 $args,$price,$retun을 어떻게 사용합니까?

또한 카테고리에 따라 모든 제품 가격을 변경해야하는 경우이 필터에 제품 ID를 어떻게 얻을 수 있습니까? 전 제품 ID를 작성하는 경우 다음 내가

function double_price($return, $price, $args){ 
    $price=$price*2; 
    if(has_term('shirts', 'product_cat' ,$product->ID)) { 
     $price=130; 
    } 

    return '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>'.$price.'</span>'; 

} 

주의 사항 작성합니다 :이 질문은 하위 질문 이 후크 wc_price이 서식 가격 후크가 After using woocommerce_product_get_price hook the checkout page price is incorrect

답변

1

... 당신은 대신 woocommerce_product_get_price를 사용해야가 필터 후크 :

add_filter('woocommerce_product_get_price', 'double_price', 10, 2); 
function double_price($price, $product){ 
    return $price*2; 
} 

테스트를 거쳐 작동하는

코드는 활성 자녀 테마 (또는 테마)의 function.php 파일 또는 모든 플러그인 파일에 있습니다.

이 후크 서식 가격 함수 전에 소성하고 여기 WC_Product 객체 유형에 적용 get_price() 방법에 따라 합성 후크이다.

+0

하지만 woocommerce_product_get_price이 https://docs.woocommerce.com/wc-apidocs/class-WC_Deprecated_Filter_Hooks.html – Harry

+0

을 받고있다 그러나이 코드를 사용하여 오류가있는 이유. 체크 아웃 페이지 가격이 올바르지 않습니다. 예를 들어 제품의 원래 가격은 10입니다. 우리는이 코드로 가격을 두 배로 올립니다. 그래서 제품 가격은 지금 20입니다. 이 제품을 장바구니에 추가하면 장바구니 및 체크 아웃 페이지 가격은 40입니다. 이것은이 곱셈이 두 번에 일어난다는 것을 의미합니다. – Harry

+0

https://stackoverflow.com/questions/47788254/after-using-woocommerce-product-get-price-hook-the-checkout-page-price-is-incorr – Harry