제품 ID에 대한 PHP 변수와 함께 wc_get_product() 사용
WooCommerce에서 제품에 대한 커스텀 랜딩 페이지를 만들고 있는데 랜딩 페이지에 표시하기 위해 무엇보다 제품 가격을 알고 싶습니다.
각 랜딩 페이지에는 WP 관리자가 랜딩 페이지뿐만 아니라 제품 ID에 대한 내용을 추가할 수 있는 커스텀 필드가 있으며, 이 필드는 제품 가격, 체크아웃 URL 등을 생성하는 데 사용됩니다.
못 찾겠어wc_get_product();
내 커스텀 필드나 변수와 함께 작업할 수 있습니다.스트레이트 아이디를 사용해야만 작동합니다.PHP에서 변수가 어떻게 동작하는지 이해가 안 되는 부분이 있는 것 같습니다.제 암호는 이렇습니다.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
갱신하다
또는 를 사용하면 다음 오류가 발생합니다.
Fatal error: Call to a member function get_regular_price() on a non-object in ...
최근 의견과 관련된 업데이트입니다.두 가지 탐색 방법:
1) 을 사용하여 제품 객체를 취득하는 대신 (오류를 회피) 다음을 수행합니다.
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) 그렇지 않으면 다음과 같은 방법으로 제품 가격(또는 제품 메타 데이터)을 취득하는 기능을 사용해 보십시오.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
이번에는 어느 한쪽의 솔루션과 함께 가격이 표시됩니다.
업데이트: $course를 변환해야 할 수도 있습니다.정수 변수에 대한 ID.
내부 변수(2)는 다음과 같이 사용해야 합니다.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
이제 작동해야 합니다.
이것을 시험해 볼 수 있습니다.
$courseID = the_field('course_id');
$product = get_product( $courseID );
저는 @Loic The Aztec이 그의 답변으로 제공한 가능한 솔루션 경로를 살펴본 후 답을 알아냈습니다.이것들 중 어느 것도 효과가 없었기 때문에 나는 다른 무언가가 일어났다고 생각했다.
고급 커스텀 필드를 사용하여 백엔드에 커스텀 필드를 추가하고 있으며 ACF를 사용하고 있었습니다.the_field()
제 변수를 만들기 위해서요.이 함수는 필드를 표시하도록 설계되어 있기 때문에 잘못된 사용법입니다(기본적으로 php의 에코를 사용하고 있습니다).이러한 커스텀 필드를 사용하려면 , ACF 를 사용할 필요가 있습니다.get_field()
즉, 값을 저장하고, 값을 에코하고, 값과 상호 작용하기 위해 사용합니다.
$코스 설정으로 전환한 적이 있습니다.아이디...
$courseID = get_field('course_id');
모든 것이 잘 되었다.내 코드는 작동했고, @Loic The Aztec의 모든 코드 접근법도 작동했습니다.
언급URL : https://stackoverflow.com/questions/41730193/using-wc-get-product-with-a-php-variable-for-product-id
'source' 카테고리의 다른 글
스프링 보안 LDAP 및 리멤버 미 (0) | 2023.04.02 |
---|---|
재생 프레임워크 JsObject에서 Json 배열 구문 분석 (0) | 2023.04.02 |
현재 사용자가 wordpress에서 관리자인지 확인합니다. (0) | 2023.04.02 |
Wordpress에서 XSLT를 사용한 XML 구문 분석 (0) | 2023.04.02 |
WordPress post 추가/업데이트 후 훅과 포스트 메타 삽입 후 후 후크? (0) | 2023.04.02 |