2017-05-12 3 views
3

장바구니의 원래 가격을 변경하려면이 방법을 사용했지만 주사위는 사용하지 않으려 고 시도했습니다. 나는 내가 사용하는 제품이 가변적 인 제품이기 때문에 작동하지 않는다고 생각한다. 제품 ID 141 및 변화 ID입니다 (142)WooCommerce 제품 변형을위한 카트의 원래 가격 변경

function sv_change_product_price_cart($price, $cart_item, $cart_item_key) { 
    if (142 === $cart_item['product_id']) { 
     $price = '$50.00 per Unit<br>(7-8 skewers per Unit)'; 
    } 
    return $price; 
} 
add_filter('woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3); 

감사

를 작동하게하는 방법

답변

2

당신은이 제품의 변화를 작업 할 $cart_item['variation_id']에 의해 $cart_item['product_id']를 교체해야한다 너의 상태로.

// Changing the price (for cart calculation) 
add_filter('woocommerce_before_calculate_totals', 'sv_change_product_price_cart', 10, 1); 
function sv_change_product_price_cart($cart_object) { 

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

    foreach ($cart_object->get_cart() as $cart_item) { 
     if (142 == $cart_item['variation_id']){ 
      // Set your price 
      $price = 50; 

      // WooCommerce versions compatibility 
      if (version_compare(WC_VERSION, '3.0', '<')) { 
       $cart_item['data']->price = $price; // Before WC 3.0 
      } else { 
       $cart_item['data']->set_price($price); // WC 3.0+ 
      } 
     } 
    } 
} 

코드에 간다 : 여기

// Changing the displayed price (with custom label) add_filter('woocommerce_cart_item_price', 'sv_display_product_price_cart', 10, 3); function sv_display_product_price_cart($price, $cart_item, $cart_item_key) { if (142 == $cart_item['variation_id']) { // Displaying price with label $price = '$50.00 per Unit<br>(7-8 skewers per Unit)'; } return $price; } 

이 가격 카트 계산이 변경됩니다 후크 기능입니다 :

이 기능은 계산을 디스플레이를 변경할 수 있지만하지 않을 것이다 활성 자식 테마 (또는 테마) 또는 모든 플러그인 파일의 function.php 파일.

enter image description here

이 코드는 테스트 광고가 Woocommerce 버전의 2.6.x에서 작동하며 3.0

:

그래서 당신은 그것을 얻을 것이다

관련 문제