2017-04-21 95 views
1

WooCommerce 3.0更新對我來說並不友好。我添加了一個自定義必填字段來檢出域名,並且無法確定如何保存它。此代碼添加場仍正常:在WooCommerce中添加自定義結賬區域以訂購

add_action('woocommerce_after_order_notes', 'add_domain_checkout_field'); 

function add_domain_checkout_field($checkout) { 

    echo '<div id="add_domain_checkout_field"><h2>' . __('Domain') . '</h2>'; 

    woocommerce_form_field('sitelink_domain', array(
     'type'   => 'text', 
     'required' => true, 
     'class'   => array('my-field-class form-row-wide'), 
     'label'   => __('Domain where SiteLink will be installed'), 
     'placeholder' => __('Enter your URL'), 
     ), $checkout->get_value('sitelink_domain')); 

    echo '</div>'; 

} 

,我試圖挽救它,像這樣:

add_action('woocommerce_checkout_create_order', 'add_domain_to_order_meta', 10, 2); 
function add_domain_to_order_meta($order, $data) { 
    if (! empty($_POST['sitelink_domain'])) { 
     $order->add_meta_data('ssla_sitelink_url', sanitize_text_field($_POST['sitelink_domain'])); 
    } 
} 

但是元似乎並沒有被添加或保存任何地方。

我知道$_POST變量是存在的,我有錯誤記錄下來看到。

測試一些抓取和錯誤記錄混淆了我進一步:

$sitelink_domain  = $subscription->get_meta_data('ssla_sitelink_url'); 
error_log(print_r( $sitelink_domain, true)); 

//輸出爲:

[21-Apr-2017 01:26:27 UTC] Array 
(
    [0] => stdClass Object 
     (
      [id] => 270086 
      [key] => _ssla_sitelink_url 
      [value] => lololol.com 
     ) 

    [1] => stdClass Object 
     (
      [id] => 270089 
      [key] => _download_permissions_granted 
      [value] => 1 
     ) 

) 

然而,

$sitelink_domain  = $subscription->get_meta('ssla_sitelink_url'); 
error_log('Domain: ' . $sitelink_domain); 

輸出就是:

[21-Apr-2017 01:27:39 UTC] Domain: 
+0

如何初始化'$ subscription'?請記住,即使他們共享大量數據,訂閱訂單和訂單也不盡相同。 – helgatheviking

+1

訂閱通過我正在使用的鉤子傳入,'woocommerce_subscription_status_updated',它看起來像某種原因'add_meta_data()'是在帶有下劃線的密鑰前加上?我嘗試了'$ subscription-> get_meta('_ssla_sitelink_url');'並且它正確地將其拉出... – thatryan

回答

0

您可以添加額外的訂單元數據,如下所示。

add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta',1,2); 
if(!function_exists('add_values_to_order_item_meta')) 
{ 
    function add_values_to_order_item_meta($item_id, $values) 
    { 
     global $woocommerce,$wpdb; 
     $user_custom_values = $values['user_custom_data_value']; 
     if(!empty($user_custom_values)) 
     { 
      wc_add_order_item_meta($item_id,'user_custom_data',$user_custom_values); 
     } 
    } 
} 
+1

目前尚不清楚OP是否需要訂單項元或常規訂單元,但是這種方法會將'user_custom_data_value'添加到每個訂購的產品中。此外,您可以通過刪除不使用的全局變量來清除代碼,並刪除'function_exists'檢查,因爲在您自己定義它時不需要檢查它是否存在。 – helgatheviking

2

首先,你需要驗證時結賬的形式張貼在現場,並要求現場和使用woocommerce_checkout_process行動掛鉤不可選:

add_action('woocommerce_checkout_process', 'domain_checkout_field_process'); 
function domain_checkout_field_process() { 
    // Check if it's set and if it's not set, we add an error. 
    if (! $_POST['sitelink_domain']) 
     wc_add_notice(__('Please enter the domain where the SiteLink will be installed.'), 'error'); 
} 

由於這是一個結賬自定義字段,您可以使用woocommerce_checkout_update_order_meta操作掛鉤來保存新字段以訂購自定義字段:

add_action('woocommerce_checkout_update_order_meta', 'domain_checkout_field_update_order_meta'); 

function domain_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['sitelink_domain'])) { 
     update_post_meta($order_id, 'ssla_sitelink_url', sanitize_text_field($_POST['sitelink_domain'])); 
    } 
} 

使用woocommerce_admin_order_data_after_billing_address行動掛鉤,以顯示管理秩序版頁面上的自定義字段值:

add_action('woocommerce_admin_order_data_after_billing_address', 'domain_checkout_field_display_admin_order_meta', 10, 1); 
function domain_checkout_field_display_admin_order_meta($order){ 
    // Get the custom field value 
    $domain_siteLink = get_post_meta($order->get_id(), 'ssla_sitelink_url', true); 
    // Display the custom field: 
    echo '<p><strong>' . __('Domain SiteLink', 'woocommerce') . ': </strong>' . $domain_siteLink . '</p>'; 
} 

顯示在前端的訂單和電子郵件通知的自定義字段標籤和值:

add_action('woocommerce_order_item_meta_end', 'custom_custom', 10, 3); 
function custom_custom($item_id, $item, $order){ 
    // Get the custom field value 
    $domain_siteLink = get_post_meta($order->get_id(), 'ssla_sitelink_url', true); 
    // Display the custom field: 
    echo '<p><strong>' . __('Domain SiteLink', 'woocommerce') . ': </strong>' . $domain_siteLink . '</p>'; 
} 

這代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也包含在任何插件文件中。

此代碼適用於WooCommerce 3.0+

相關問題