在Woocommerce编辑订单页面中显示可编辑的自定义字段值

时间:2018-01-23 20:34:47

标签: php wordpress woocommerce checkout orders

我不是开发人员,但不知何故设法将Woocommerce自定义字段添加到结帐和订购编辑页面。有类似的问题,但我找不到正确的解决方案。

自定义字段在管理订单编辑页面中可见,但它们不会显示值,也不会添加到订单电子邮件中。

我错过了什么?

请在最后看截图。

以下是汇总的所有代码:

<asp:UpdatePanel ID="atlaspnlpost" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Panel ID="pnlPost" runat="server" Style="width: 855px" GroupingText="Accounting use only">
                    <table class="MainSectionBody" style="width: 855px">
                        <tr style="height: 30px">
                            <td style="width: 165px">
                                <asp:Label ID="Label4" runat="server" Text="Accounting Date" Width="160px"></asp:Label>
                            </td>
                            <td style="width: 200px">
                                <asp:TextBox ID="txtAcctDate" runat="server" Width="200px" TabIndex="13" Text="01/18/2018"></asp:TextBox>&nbsp;
                            </td>
                            <td style="width: 85px">
                                <a runat="server" id="lnkAcctDate" style="cursor: hand">
                                    <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/calendar_small.gif" TabIndex="98">
                                    </asp:Image></a>
                                <asp:CompareValidator ID="cvAccDate" runat="server" ErrorMessage="Accounting Date is Required"
                                    Operator="DataTypeCheck" SetFocusOnError="True" Type="Date" ControlToValidate="txtAcctDate">*</asp:CompareValidator>
                            </td>
                            <td style="width: 405px">
                                <asp:CheckBox ID="chkApprove" Text="Approved By Accounting" runat="server" TabIndex="14" />
                            </td>
                        </tr>
                        <tr style="height: 30px">
                            <td style="width: 165px">
                                <asp:Label ID="lblApprover" runat="server" Text="Approver Name" Width="160px"></asp:Label>
                            </td>
                            <td style="width: 200px">
                                <asp:TextBox ID="txtApprover" runat="server" Width="200px" SkinID="ReadOnlyTextBox"
                                    ReadOnly="True" TabIndex="99"></asp:TextBox>
                            </td>
                            <td style="width: 85px">
                                <td style="width: 405px">
                                    <%--<asp:Button ID="btnPost" runat="server" Text="Post" Width="76px" TabIndex="15" />--%>
                                </td>
                        </tr>
                         <tr style="height: 30px">
                            <td style="width: 165px">
                                <asp:Label ID="lblDescription" runat="server" Text="Description" Width="160px"></asp:Label>
                            </td>
                            <td style="width: 200px">
                                <asp:TextBox ID="txtdescription" runat="server" Width="200px"  TabIndex="99" 
                                    TextMode="MultiLine"></asp:TextBox>
                            </td>
                            <td style="width: 85px">
                                <td style="width: 405px">
                                  <%-- <asp:Button ID="btnPost" runat="server" Text="Post" Width="76px" TabIndex="15" />--%>
                                </td>
                        </tr>
                        <tr style="height: 30px">
                            <td style="width: 165px">
                                <asp:Label ID="lblFinanceexplanation" runat="server" Text="FinanceExpalnation" Width="160px"></asp:Label>
                            </td>
                            <td style="width: 200px">
                                <asp:TextBox ID="txtFinanceExplanation" runat="server" Width="200px"  TabIndex="99" 
                                    TextMode="MultiLine"></asp:TextBox>
                            </td>
                            <td style="width: 85px">
                                <td style="width: 405px">
                                   <asp:Button ID="btnPost" runat="server" Text="Post" Width="76px" TabIndex="15" />
                                </td>
                        </tr>
                    </table>
                </asp:Panel>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
                <asp:AsyncPostBackTrigger ControlID="btnRefresh" EventName="Click" />
                 <asp:AsyncPostBackTrigger ControlID="ddlYear" EventName="SelectedIndexChanged" />

            </Triggers>
        </asp:UpdatePanel>

订单编辑页面的屏幕截图

web add-in framework

2 个答案:

答案 0 :(得分:1)

我已经测试了您的代码,但只有一些小错误。你很接近让它按预期工作。所以你需要在以下代码中进行一些更改:

1)删除了&#39;值&#39;因为这个钩子函数适用于结账并且不需要值(当你试图从非现有订单中获取它们时更少)。

  

这样可以避免隐藏的错误,并且当客户已经在以前的追求中填充了字段时,将显示正确的值...

// Add custom Checkout billing fields
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields', 20, 1);
function add_woocommerce_billing_fields( $billing_fields ) {

    $billing_fields['billing_birthday'] = array(
        'type' => 'tel',
        'label' => __('Datum rojstva'),
        'placeholder' => __('dd/mm/yyyy', 'placeholder'),
        'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
        'class' => array('form-row-first'),
        'required' => true,
        'clear' => true
    );

    $billing_fields['billing_socialno'] = array(
        'type' => 'tel',
        'label' => __('Davčna številka'),
        'placeholder' => _x('8-mestna številka', 'placeholder'),
        'class' => array('form-row-last'),
        'required' => false,
        'clear' => true
    );

    return $billing_fields;
}

// Change field type to tel for woocommerce checkout
add_action( 'woocommerce_after_checkout_form', 'change_checkout_field_input_type');
function change_checkout_field_input_type() {
    echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
    echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";
}

2)此处的密钥错误,因此表示在管理订单编辑页面中字段值未显示的原因。

  

它是'birthday''socialno',而不是'billing_birthday''billing_socialno'

// Setting custom fields Keys/Labels pairs in admin edit order pages and allow edit this fields correctly.
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
    $billing_fields['birthday'] = array( 'label' => __('Datum rojstva', 'woocommerce') );
    $billing_fields['socialno'] = array( 'label' => __('Davčna številka', 'woocommerce') );

    return $billing_fields;
}

3)正确缺少的字段值以显示在订单编辑页面中。

// Get the field values to be displayed in admin Order edit pages
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
function add_woocommerce_order_fields($address, $order ) {
    $address['billing_birthday'] = get_post_meta( $order->get_id(), '_billing_birthday', true );
    $address['billing_socialno'] = get_post_meta( $order->get_id(), '_billing_socialno', true );
    return $address;
}

4)其他未更改的挂钩功能:

//Doda user meta v backend profil
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields) {
    if (isset($fields['billing']['fields'])) {
        $fields['billing']['billing_birthday'] = array(
            'label' => __('Datum rojstva', 'woocommerce'),
            'description' => 'Pa kaj bo končno ratalo memo milo?'
        );
        $fields['billing']['billing_socialno'] = array(
            'label' => __('Davčna številka', 'woocommerce'),
            'description' => ''
        );
    }
    return $fields;
}

add_filter( 'woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);
function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
    if ($type_to_load == 'billing') {
        $customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
        $customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);

    }
    return $customer_data;
}

//add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);
function add_woocommerce_formatted_address_replacements($replace, $args) {
    $replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
    $replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
    return $replace;
}

add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);
function add_woocommerce_localisation_address_formats($formats) {
    $formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
    return $formats;
}

代码进入活动子主题(或活动主题)的function.php文件。

电子邮件通知 - 显示自定义字段(及其标签)。

通过您的活动子主题覆盖Woocommerce模板emails/email-addresses.php

  

可以通过复制来覆盖此模板:
plugin/woocommerce/templates/emails/email-addresses.php
  致yourtheme/woocommerce/emails/email-addresses.php ...
  官方文件:Template structure & Overriding templates via a theme

您将在第34行 之后插入(在结算电话之后)以下内容:

<?php
    // Billing birthday
    $billing_birthday = get_post_meta($order->get_id(), '_billing_birthday', true );
    echo $billing_birthday ? '<br/>'.__('Datum rojstva', 'woocommerce').': '.$billing_birthday : '';

    // Billing socialno
    $billing_socialno = get_post_meta($order->get_id(), '_billing_socialno', true );
    echo $billing_socialno ? '<br/>'.__('Davčna številka', 'woocommerce').': '.$billing_socialno : '';
?>

经过测试和工作。

答案 1 :(得分:0)

如果您想添加自定义字段,则不仅仅是添加新的结算明细,而且无论如何都很简单。

我建议您使用hook df$Average_wind < - storms %>% filter (status = df$status, date >= df$Begin_Date,date<df$End_Date) %>% summarise(avg = mean(wind)) Error in FUN(left) : invalid argument to unary operator ,因此它将是这样的:

woocommerce_admin_order_data_after_billing_address

您还可以查看完整的教程here