2013-01-13 77 views
1

我寫了創建以下的自定義結賬字段woocommerce插件:複製定製woocommerce領域爲默認字段

billing_street_name 
billing_house_number 
billing_house_number_suffix 

shipping_street_name 
shipping_house_number 
shipping_house_number_suffix 

我也添加到了後臺管理頁面,但因爲我不能掛接到get_formatted_billing_address & get_formatted_shipping_address(其均用於顯示writepanel-order_data.php和shop_order.php地址)我想將它們複製到默認這樣billing_address_1 & shipping_address_1:

billing_address_1 = billing_street_name + billing_house_number + b illing_house_number_suffix

我試着用以下的(初步)代碼來做到這一點:

add_action('woocommerce_process_checkout_field_billing_address_1', array(&$this, 'combine_street_number_suffix')); 

public function combine_street_number_suffix() { 
$key = $_POST['billing_street_name'] . ' ' . $_POST['billing_house_number']; 

return $key; 
} 

,但不工作 - 我不認爲$ _ POST變量被都傳遞?

這裏的鉤子是如何在課堂上-WC-checkout.php創建:

// Hook to allow modification of value 
$this->posted[ $key ] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]); 

回答

1

修復了這個使用 'woocommerce_checkout_update_order_meta' 鉤:

add_action('woocommerce_checkout_update_order_meta', array(&$this, 'combine_street_number_suffix')); 

public function combine_street_number_suffix ($order_id) { 
    // check for suffix 
    if ($_POST['billing_house_number_suffix']){ 
     $billing_house_number = $_POST['billing_house_number'] . '-' . $_POST['billing_house_number_suffix']; 
    } else { 
     $billing_house_number = $_POST['billing_house_number']; 
    } 

    // concatenate street & house number & copy to 'billing_address_1' 
    $billing_address_1 = $_POST['billing_street_name'] . ' ' . $billing_house_number; 
    update_post_meta($order_id, '_billing_address_1', $billing_address_1); 

    // check if 'ship to billing address' is checked 
    if ($_POST['shiptobilling']) { 
     // use billing address 
     update_post_meta($order_id, '_shipping_address_1', $billing_address_1); 
    } else { 
     if ($_POST['shipping_house_number_suffix']){ 
      $shipping_house_number = $_POST['shipping_house_number'] . '-' . $_POST['shipping_house_number_suffix']; 
     } else { 
      $shipping_house_number = $_POST['shipping_house_number']; 
     } 

     // concatenate street & house number & copy to 'shipping_address_1' 
     $shipping_address_1 = $_POST['shipping_street_name'] . ' ' . $shipping_house_number; 
     update_post_meta($order_id, '_shipping_address_1', $shipping_address_1);   
    } 


    return; 
} 

我不認爲這是代碼雖然非常優雅(後綴檢查部分具體),所以如果有人有提示改善它 - 非常歡迎!