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'); 
} 

これはチェックアウトのカスタムフィールドであるため、

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'])); 
    } 
} 
01:カスタムフィールドを注文する新しいフィールドを保存するために woocommerce_checkout_update_order_metaアクションフックを使用することができます

使用管理発注版のページにカスタムフィールドの値を表示する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以上で動作します

関連する問題