2017-04-21 66 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+

相关问题