2017-05-10 10 views
2

WooCommerce 3.0 이상을 사용하고 있으며 특정 페이지에서 제품 가격을 설정했습니다.WooCommerce 3.0 이상의 주문에서 동적 카트 항목 가격이 책정되지 않습니다

 $regular_price = get_post_meta($_product->id, '_regular_price', true); 
     $buyback_percentage = get_post_meta($_product->id, '_goldpricelive_buy_back', true); 
     $fixed_amount = get_post_meta($_product->id, '_goldpricelive_fixed_amount', true); 
     $markedup_price = get_post_meta($_product->id, '_goldpricelive_markup', true); 
     $buyback_price = ($regular_price - $fixed_amount)/(1 + $markedup_price/100) * (1-$buyback_percentage/100); 
     $_product->set_price($buyback_price); 

내 장바구니에서 가격이 업데이트되고 있지만 주문 제출을 클릭하면 주문 개체가 설정 한 가격을받지 못하는 것 같습니다. 원산지 제품 가격이 소요됩니다.

이 작업을 어떻게 수행 할 수 있습니까? 당신이 사용자 정의 후크 함수 내에서 woocommerce_before_calculate_totals 액션 후크 설정을 사용해야

감사

get_price() 방법으로 업데이트
+0

어떻게 이러한 모든 코드 라인을 호출합니까? – Reigel

+0

나는 루프를 통해 이것을 호출한다. $ _product = wc_get_product ($ id) – Elland

+0

잘,'$ _product-> set_price ($ buyback_price);'는'$ _product' 바로 지금 바로 가격을 정할 것이다. 그것은 구원을 얻지 못할 것이다. – Reigel

답변

2

...

, 제품 ID 또는 제품 ID의 배열.
그런 다음 각각에 대해 맞춤 계산을 수행하여 장바구니, 결제 및 주문 제출 후 맞춤 가격을 설정할 수 있습니다. 어떤 플러그인 파일도

add_action('woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1); 
function adding_custom_price($cart_obj) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Set below your targeted individual products IDs or arrays of product IDs 
    $target_product_id = 53; 
    $target_product_ids_arr = array(22, 56, 81); 

    foreach ($cart_obj->get_cart() as $cart_item) { 
     // The corresponding product ID 
     $product_id = $cart_item['product_id']; 

     // For a single product ID 
     if($product_id == $target_product_id){ 
      // Custom calculation 
      $price = $cart_item['data']->get_price() + 50; 
      $cart_item['data']->set_price(floatval($price)); 
     } 

     // For an array of product IDs 
     elseif(in_array($product_id, $target_product_ids_arr)){ 
      // Custom calculation 
      $price = $cart_item['data']->get_price() + 30; 
      $cart_item['data']->set_price(floatval($price)); 
     } 
    } 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 : 여기

는 기능 WooCommerce 버전 3.0 이상에서 테스트 코드입니다. 각 카트 항목에 대한 $product_id을 가지고

그런 다음 당신은 쉽게 ...

코드에서처럼 get_post_meta() 함수와 그와 함께 제품 동적 값으로 내 가짜 계산에 고정 값을 대체 할 수 있습니다