백엔드 개발 PHP 튜토리얼 WooCommerce 3에서 프로그래밍 방식으로 제품 가격을 어떻게 변경할 수 있나요?

WooCommerce 3에서 프로그래밍 방식으로 제품 가격을 어떻게 변경할 수 있나요?

Dec 08, 2024 am 03:24 AM

How Can I Programmatically Change Product Prices in WooCommerce 3 ?

WooCommerce 3의 후크를 통해 상품 가격 변경

WooCommerce 플랫폼은 상품 가격을 수정할 수 있는 다양한 후크를 제공합니다. add_filter('woocommerce_get_regular_price')와 add_filter('woocommerce_get_price')를 사용해 제시된 솔루션은 단순 상품에는 효과가 있지만, 변형 상품의 경우에는 한계가 있습니다.

변형 상품 가격

변형 상품 가격 조정을 위해 다음과 같이 업데이트된 후크가 있습니다. 권장 사항:

  • 단순, 그룹화 및 외부 제품:

    • wooCommerce_product_get_price
    • wooCommerce_product_get_regular_price
  • 변형:

    • wooCommerce_product_variation_get_regular_price
    • wooCommerce_product_variation_get_price
  • 변수 제품 범위:

    • woocommerce_variation_prices_price
    • woocommerce_variation_prices_regular_price

플러그인 구현

플러그인 내에서 이러한 후크를 구현하려면 다음을 고려하세요.

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2);

add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2);

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3);
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3);

function custom_price($price, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}

function custom_variable_price($price, $variation, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}
로그인 후 복사
로그인 후 복사

테마 구현

테마를 선호하는 경우 기반 접근 방식을 사용하려면 function.php에 다음 코드를 포함하세요. 파일:

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2);

add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2);

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3);
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3);

function custom_price($price, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}

function custom_variable_price($price, $variation, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}
로그인 후 복사
로그인 후 복사

추가 참고 사항 및 개선 사항

  • 가격 위젯: 가격 필터 위젯 최소값 및 최대값을 조작하려면, 다음 후크 사용을 고려해보세요: woocommerce_price_filter_widget_min_amount 및 woocommerce_price_filter_widget_max_amount.
  • 제품 판매 가격: 판매 가격에 대한 특정 후크가 존재합니다: woocommerce_product_get_sale_price, woocommerce_variation_prices_sale_price 및 woocommerce_product_variation_get_sale_price.
  • 가격 캐싱: WooCommerce 3에 캐시된 변형 가격을 고려해야 합니다. 가격 캐싱을 효율적으로 처리하려면 add_filter('woocommerce_get_variation_prices_hash') 필터를 사용하는 것이 좋습니다.

위 내용은 WooCommerce 3에서 프로그래밍 방식으로 제품 가격을 어떻게 변경할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫툴 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

11 최고의 PHP URL 쇼트너 스크립트 (무료 및 프리미엄) 11 최고의 PHP URL 쇼트너 스크립트 (무료 및 프리미엄) Mar 03, 2025 am 10:49 AM

11 최고의 PHP URL 쇼트너 스크립트 (무료 및 프리미엄)

Laravel의 플래시 세션 데이터로 작업합니다 Laravel의 플래시 세션 데이터로 작업합니다 Mar 12, 2025 pm 05:08 PM

Laravel의 플래시 세션 데이터로 작업합니다

6 PHP 개발자가 가져야 할 6 가지 추가 기술 6 PHP 개발자가 가져야 할 6 가지 추가 기술 Feb 28, 2025 am 10:52 AM

6 PHP 개발자가 가져야 할 6 가지 추가 기술

PHP의 배열을 이해하십시오 PHP의 배열을 이해하십시오 Feb 28, 2025 am 10:53 AM

PHP의 배열을 이해하십시오

Laravel Back End : Part 2, React가있는 React 앱 구축 Laravel Back End : Part 2, React가있는 React 앱 구축 Mar 04, 2025 am 09:33 AM

Laravel Back End : Part 2, React가있는 React 앱 구축

Laravel 테스트에서 단순화 된 HTTP 응답 조롱 Laravel 테스트에서 단순화 된 HTTP 응답 조롱 Mar 12, 2025 pm 05:09 PM

Laravel 테스트에서 단순화 된 HTTP 응답 조롱

PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법 PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법 Mar 14, 2025 am 11:42 AM

PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법

Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트 Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트 Mar 13, 2025 pm 12:08 PM

Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트

See all articles