2017-01-13 16 views
6

Dodałem pól niestandardowych do woocommerce/Dodaj kartę produktu/General stosując ten doskonały poradnik http://www.remicorson.com/mastering-woocommerce-products-custom-fields/woocommerce Oblicz i zapisz cena

Pola niestandardowe (wysokość, szerokość, mnożnik) są zapisywane w bazie danych OK. Chcę obliczyć cenę na podstawie pól niestandardowych i zapisać cenę w bazie danych jako normalną cenę. Problem polega na tym, że cena nie jest zapisywana.
Oto mój kod z functions.php moim dzieckiem tematu:

/* CUSTOM FIELDS: Add custom fields to Product/General pane in Woocommerce 
    from http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ */ 

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

function woo_add_custom_general_fields() 
{ 
    global $woocommerce, $post; 
    echo '<div class="options_group">'; 

    // Product Height 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_height', 
      'label' => __('Product Height (inches)', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter the product height in inches here.', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Width 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_width', 
      'label' => __('Product Width (inches)', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter the product width in inches here.', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Area Multiplier 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_area_mult', 
      'label' => __('Product Area Multiplier', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter Product Area Multiplier. ', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Area Price: added this field to check if this value is being calculated 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_area_price', 
      'label' => __('Product Area Price', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Product Area Price. Calculated automatically', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    echo '</div>'; 
} 

function woo_add_custom_general_fields_save($post_id) 
{ 
    $woocommerce_product_height = $_POST['_product_height']; 
    $woocommerce_product_width = $_POST['_product_width']; 
    $woocommerce_product_area_mult = $_POST['_product_area_mult']; 
    $woocommerce_product_area_price = $_POST['_product_area_price']; 

    // save height, width, multiplier 
    if (!empty($woocommerce_product_height)) 
     update_post_meta($post_id, '_product_height', esc_attr($woocommerce_product_height)); 

    if (!empty($woocommerce_product_width)) 
     update_post_meta($post_id, '_product_width', esc_attr($woocommerce_product_width)); 

    if (!empty($woocommerce_product_area_mult)) 
     update_post_meta($post_id, '_product_area_mult', esc_attr($woocommerce_product_area_mult)); 


    // calculate and save _product_area_price, _regular_price, price as Height*Width*Mult 
    if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width) && !empty($woocommerce_product_area_mult)) 
     $woocommerce_product_area_price = $woocommerce_product_height * $woocommerce_product_width * $woocommerce_product_area_mult; 

    if (!empty($woocommerce_product_area_price)) 
     update_post_meta($post_id, '_product_area_price', esc_attr($woocommerce_product_area_price)); 

    if (!empty($woocommerce_product_area_price)) 
    { 
     update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price)); 
     update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price)); 
    } 
} 

Wszystko działa z wyjątkiem aktualizacji pola cenowe w bazie danych. Moje niestandardowe pola są aktualizowane przy użyciu tej samej składni. Potwierdziłem, że istnieje zmienna $woocommerce_product_area_price, która aktualizuje pole niestandardowe, ale ta sama zmienna nie aktualizuje pól _regular_price ani _price. Więc te linie nie pracują, choć sama zmienna będzie zaktualizować pole _product_area_price:

if (!empty($woocommerce_product_area_price)) 
{ 
    update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price)); 
    update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price)); 
} 

Może istnieje jakiś składnia lub sprawdź mi brakuje do aktualizacji cenę w woocommerce?

+0

Czy sprawdziłeś w DB, że cena się oszczędza, czy nie? czy problem polega na tym, że nie znajduje odzwierciedlenia w interfejsie? –

+0

Nie zapisywanie w bazie danych, Raunak. – Norv

Odpowiedz

2

Co jest przyczyną problemu z ceną wyświetlaną w interfejsie użytkownika. Musisz zaktualizować klucz _price, jeśli chcesz odzwierciedlić w interfejsie _regular_price.

Spróbuj kod:

if (!empty($area_price)){ 
    update_post_meta($post_id, '_regular_price', esc_attr($area_price)); 
    update_post_meta($post_id, '_price', esc_attr($area_price)); //<-- Add this line. 
} 

UPDATED

Wystarczy dodać wyższy priorytet do woocommerce_process_product_meta hak akcja „to będziesz rade.

add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 99); 

Przetestowałem to i działa dobrze.
Mam nadzieję, że to pomoże!

+0

@ user3547460: czy możesz zaktualizować swoje pytanie jako hak, którego używasz do 'woo_add_custom_general_fields_save'. –

+0

Dołączyłem kompletny fragment kodu, aby pokazać, że hak to woocommerce_process_product_meta – Norv

+0

bardzo dziękuję Raunak, uratowałeś mojego mistrza dnia! – Norv