2016-06-24 7 views
0

Wordpress Woocommerce에서 필요한 조건에 따라 제품 가격을 프로그래밍 방식으로 업데이트했습니다. 다음은 간단한 예입니다. 나는 그것을 표시하고 카트에 그냥 잘 추가했습니다. 내 문제는 사용자가 로그 아웃하고 다시 로그인하면 장바구니가 제품의 전체 가격을 반환하는 것입니다. 가격을 잘못 업데이트하거나 카트가 올바른 할인 가격을 유지할 수있는 더 나은 방법이 있습니다. 여기 Woocommerce 업데이트 가격이 장바구니에 저장되지 않음 세션

내가

add_action('woocommerce_get_price_html','pricechanger'); 
    function pricechanger($price){ 
     $theid = get_the_ID(); 
     $product = wc_get_product($theid); 
     $price = $product->price; 
     $price = //do something to the price here 

     //save the productid/price in session for cart 
     $_SESSION['pd']['$theid'] = $price; 

     add_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2); 
     add_action('init', 'woocommerce_add_to_cart_action', 10); 
     add_action('init', 'woocommerce_checkout_action', 10); 

     return $price; 

    } 

가격은 카트 버튼에 추가로 전환하지 않을 때문에 functions.php이 무엇인지, 내가 세션에 저장 했어. 나는 그 가격을 장바구니에 보내는 곳을 찾지 못했습니다.

add_action('woocommerce_before_calculate_totals', 'woo_add_discount'); 
function woo_add_discount() { 

    if(isset($_SESSION['pd'])) { 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 
     foreach($_SESSION['pd'] as $key => $val) { 
     if($cart_item['data']->id == $key){ 
      $cart_item['data']->set_price($val); 
     } 
     } 
    } 
    } 

} 

도움을 주시면 대단히 감사하겠습니다! 감사합니다.

+0

uggg 내 유일한 해결책은 세션 종료시 장바구니를 죽이는 것 같습니다. 바라건대 누군가가 이것에 대한 해답을 가지고 있습니다! 로그 아웃 한 후 장바구니 데이터/맞춤형 가격을 유지하는 것이 좋습니다. – Dyluck

답변

1

내 결론을 게시하는 것을 잊어 버렸습니다.

당신은 당신의 가격 변화 코드에 대해이 후크를 실행해야합니다

add_action('woocommerce_before_calculate_totals', 'your_function_here'); 
add_action('woocommerce_before_mini_cart', 'your_function_here'); 
function your_function_here(){ 

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

     //do something to your price here 
     $price = 'some calculation here'; 

     //set your price 
     $cart_item['data']->price = floatval($price); 

    } 

} 

희망 누군가가하는 데 도움이!

관련 문제