2017-03-31 1 views
0

ich brauche den Produktpreis auf Werte von einigen benutzerdefinierten Feldern basierend aktualisierenaktualisieren benutzerdefiniertes Feld aus anderen benutzerdefinierten Feldern Werte

das ist, wie ich habe zu bauen und die benutzerdefinierten Felder zu speichern, am Ende ich versuche, update _regular_price basierend auf den Werten der Zollfelder, wenn ich das Produkt speichere, aktualisiere, veröffentliche, aber nichts passiert, was fehlt mir?

alle Code geht in function.php jetzt

//BUILD AND SAVE FIELDS 
// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 

function woo_add_custom_general_fields() { 

    global $woocommerce, $post; 

    echo '<div class="options_group">'; 

// Product URL 
woocommerce_wp_checkbox( 
array( 
    'label' => __('Product URL', 'woocommerce'), 
    $post_id = $_GET['post'], 
    $produrl = get_post_meta($post_id, 'productOriginalUrl', true), 
    'description' => '<a href="' . $produrl . '" class="button button-primary button-large" target="_blank">' . __('View Product', 'woocommerce') . '</a>', 
    ) 
); 

    // Number Field 
woocommerce_wp_text_input( 
    array( 
     'id'    => '_price_usd', 
     'label'    => __('Price in USD', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Enter the custom value here.', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
    ) 
); 
woocommerce_wp_text_input( 
    array( 
     'id'    => '_price_tax', 
     'label'    => __('TAX', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Enter the custom value here.', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
    ) 

); 
woocommerce_wp_text_input( 
    array( 
     'id'    => '_price_shipping', 
     'label'    => __('Shipping', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Enter the custom value here.', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
    ) 

); 

// Select 
woocommerce_wp_select( 
array( 
    'id'  => '_shipping_weight', 
    'label' => __('Shipping Weight', 'woocommerce'), 
    'options' => array(
     '3.75' => __('Less Than 0.5 KG', 'woocommerce'), 
     '7.5' => __('0.5 KG', 'woocommerce'), 
     '15' => __('1 KG', 'woocommerce') 
     ) 
    ) 
); 

// Hidden field 
woocommerce_wp_hidden_input(
array( 
    'id' => '_usd_rate', 
    'value' => '18' 
    ) 
); 
    echo '</div>'; 
}; 

// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 
function woo_add_custom_general_fields_save($post_id){ 

    // Number Field 
    $woo_price_usd = $_POST['_price_usd']; 
    if(!empty($woo_price_usd)) 
     update_post_meta($post_id, '_price_usd', esc_attr($woo_price_usd)); 

    $woo_price_tax = $_POST['_price_tax']; 
    if(!empty($woo_price_tax)) 
     update_post_meta($post_id, '_price_tax', esc_attr($woo_price_tax)); 

    $woo_price_shipping = $_POST['_price_shipping']; 
    if(!empty($woo_price_shipping)) 
     update_post_meta($post_id, '_price_shipping', esc_attr($woo_price_shipping)); 

    // Select 
    $woo_shipping_weight = $_POST['_shipping_weight']; 
    if(!empty($woo_shipping_weight)) 
     update_post_meta($post_id, '_shipping_weight', esc_attr($woo_shipping_weight)); 

    // Hidden Field 
    $woo_usd_rate = $_POST['_usd_rate']; 
    if(!empty($woo_usd_rate)) 
     update_post_meta($post_id, '_usd_rate', esc_attr($woo_usd_rate)); 

} 

//UPDATE PRICE REGULAR 
function wpa104760_default_price($post_id, $post) { 


    if (isset($_POST['_regular_price'])) { 
$post_id = $_GET['post']; 
     $woo_price_usd = get_post_meta($post_id, '_price_usd', true); 
     $woo_price_tax = get_post_meta($post_id, '_price_tax', true); 
     $woo_price_shipping = get_post_meta($post_id, '_price_shipping', true); 
     $woo_shipping_weight = get_post_meta($post_id, '_shipping_weight', true); 
     $woo_usd_rate = get_post_meta($post_id, '_usd_rate', true); 

$woo_new_product_price = (($woo_price_usd + (($woo_price_usd*$woo_price_tax)/100) + $woo_price_shipping + $woo_shipping_weight) * $woo_usd_rate); 

update_post_meta($post_id, '_regular_price', $woo_new_product_price); 
     } 

} 
add_action('woocommerce_process_product_meta', 'wpa104760_default_price'); 

Antwort

0

die Antwort einfach war,

es einen Filter benötigt, um die _regular_price und die _sale_price zu aktualisieren

//price new 

function return_custom_price($price, $product) { 
global $post; 
$new_total = get_post_meta($post->ID, '_total_egp', true); 
$price = get_post_meta($post->ID, '_regular_price', true); 
$sale_price = get_post_meta($post->ID, '_sale_price', true); 
$now_price = get_post_meta($post->ID, '_price', true); 

if($price != 0){ 
update_post_meta($post->ID, '_regular_price', $new_total); 
update_post_meta($post->ID, '_sale_price', $new_total); 
update_post_meta($post->ID, '_price', $new_total); 
return $price; 
    } 
} 
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2); 
Verwandte Themen