source

wo commerce in word press return 항상 단순한 제품 유형

nicesource 2023. 10. 1. 19:32
반응형

wo commerce in word press return 항상 단순한 제품 유형

그룹화된 제품의 종류를 구하려고 하지만 WC_Product_Factory를 사용하면 우커머스가 빈 상태로 돌아오거나 항상 "간단" 상태로 돌아옵니다.

사용할 때:

$the_product = new WC_Product(2886);
echo $the_product->product_type;

빈 채로 반환합니다.

WC_Product_Factory 사용 시:

$the_product = new WC_Product(2886);
$the_product_factory = new WC_Product_Factory();
$the_product = $the_product_factory->get_product($the_product);
echo $the_product->product_type;

항상 "simple"를 반환합니다.

다음을 사용하려고 합니다.

$the_product = wc_get_product(2886);
echo $the_product->product_type;

그러나 이것은 "Call to member function get_product()"라는 오류를 반환합니다.."

내 코드는 안에 있습니다.

add_action( 'plugins_loaded', function(){
    $the_product = new WC_Product(2886);
    echo $the_product->product_type;

    $the_product = new WC_Product(2886);
    $the_product_factory = new WC_Product_Factory();
    $the_product = $the_product_factory->get_product($the_product);
    echo $the_product->product_type;

    $the_product = wc_get_product(2886);
    echo $the_product->product_type;

    die();
});

도와주셔서 감사합니다!

해결책 또는 실수를 발견했습니다. plugins_loaded to init:

add_action( 'init', function(){
    $the_product = new WC_Product(2886);
    echo $the_product->product_type;

    $the_product = new WC_Product(2886);
    $the_product_factory = new WC_Product_Factory();
    $the_product = $the_product_factory->get_product($the_product);
    echo $the_product->product_type;

    $the_product = wc_get_product(2886);
    echo $the_product->product_type;
    die();
});

그리고 이 작품!

이 url https://wordpress.stackexchange.com/questions/120055/woocommerce-create-new-product-and-add-to-cart-on-form-submit 이 아이디어를 저에게 주었습니다.

해피코딩! :)

이 코드를 사용하여 사용자 지정 제품 유형을 가져올 수 있습니다.

$terms = get_the_terms($product_id, 'product_type');
$product_type = !empty($terms) ? sanitize_title(current($terms)->name) : 'simple';

언급URL : https://stackoverflow.com/questions/28355770/woocommerce-in-wordpress-return-always-simple-as-product-type

반응형