저는 전문가는 아니지만 Woocommerce 제품 페이지에서 "out of stoke"제품의 가격 스트로크를 변경해야합니다.
//Change price to 'sold'
add_filter('woocommerce_product_get_price','change_price_regular_member', woocommerce_currency_symbols, 10, 2 );
function change_price_regular_member( $price, $product)
{
if (!$product->is_in_stock())
$price = "SOLD";
return $price;
}
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product;
if (!$product->is_in_stock() ) {
switch( $currency ) {
case 'USD': $currency_symbol = '';
break;
}}
return $currency_symbol;
}
그리고 그것은 작동하지 않습니다
오류 목록
Your PHP code changes were rolled back due to an error on line 66 of file wp-content/themes/pro-child/functions.php. Please fix and try saving again.
Uncaught Error: Call to a member function is_in_stock() on null in wp-content/themes/pro-child/functions.php:66
Stack trace:
#0 wp-includes/class-wp-hook.php(287): change_existing_currency_symbol('$', 'USD')
#1 wp-includes/plugin.php(212): WP_Hook->apply_filters('$', Array)
#2 wp-content/plugins/woocommerce/includes/wc-core-functions.php(854): apply_filters('woocommerce_cur...', '$', 'USD')
#3 wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-price-filter.php(45): get_woocommerce_currency_symbol()
#4 wp-includes/class-wp-widget-factory.php(61): WC_Widget_Price_Filter->__construct()
#5 wp-includes/widgets.php(115): WP_Widget_Factory->register('WC_Widget_Price...')
#6 wp-content/plugins/woocommerce/includes/wc-
도와주세요? 뭐가 문제 야?
대신 woocommerce_get_price_html
다음과 같이 후크를 사용하십시오 ( 포맷 된 표시된 가격 및 통화를 대체합니다) .
add_filter('woocommerce_get_price_html', 'change_sold_out_product_price_html', 100, 2 );
function change_sold_out_product_price_html( $price_html, $product ) {
if ( ! $product->is_in_stock() ) {
$price_html = __("SOLD", "woocommerce");
}
return $price_html;
}
코드는 활성 자식 테마 (또는 활성 테마)의 functions.php 파일에 들어갑니다. 더 잘 작동합니다.
댓글과 관련된 추가 :
두 번째 코드 조각의 경우 대신 다음을 시도하십시오.
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
$product = wc_get_product( get_the_ID() );
if ( is_a( $product, 'WC_Product' ) && $currency === 'USD' && ! $product->is_in_stock() ) {
$currency_symbol = '';
}
return $currency_symbol;
}
작동합니다. 제품 재고가 없을 때 모든 통화에서 작동하도록 && $currency === 'USD'
하려면 IF
명세서 에서 제거 하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다