电子邮件地址上的WooCommerce自定义计费字段

时间:2018-09-20 作者:Mauricio Gtz

我想将使用WooCommerce Checkout Field Editor插件添加的自定义帐单字段添加到发送给客户的电子邮件中:

我添加了自定义num\\u int字段:enter image description here

并尝试将其添加到woocommerce\\u order\\u formatted\\u billing\\u address筛选器中:这是我使用的代码:

add_filter(\'woocommerce_order_formatted_billing_address\', array($this, \'woo_custom_order_formatted_billing_address\'), 10, 2);

function woo_custom_order_formatted_billing_address( $address , $WC_Order )
{

    $address = array(
        \'first_name\'    => \'Nombre: \' . $WC_Order->billing_first_name . \' \' . $address[\'last_name\'],
        \'address_1\'     => \'Dirección: \' . $WC_Order->billing_address_1,
        \'address_2\'     => \'Colonia: \' . $WC_Order->billing_address_2,
        \'city\'          => \'Ciudad: \' . $WC_Order->billing_city,
        \'state\'         => \'Estado: \' . $WC_Order->billing_state,
        \'postcode\'      => \'Código Postal: \' . $WC_Order->billing_postcode,

        //this is the custom field i want to add
        \'num_int\'       => \'Num. Interior: \' . $WC_Order->billing_num_int,

    );

    return $address;

}
但邮件上不会显示:

enter image description here

1 个回复
最合适的回答,由SO网友:Mauricio Gtz 整理而成

我最终使用以下代码实现了它:

我使用一个php类来创建一个有组织的工作流,所以不必介意public 关于函数。。。

首先,我像这样注册我的过滤器:

public function registerFilters()
{
    add_filter( \'woocommerce_default_address_fields\', array($this, \'woo_new_default_address_fields\'), 10, 2);
    add_filter(\'woocommerce_order_formatted_billing_address\', array($this, \'woo_custom_order_formatted_billing_address\'), 10, 2);
    add_filter(\'woocommerce_formatted_address_replacements\', array($this, \'myFunction\'), 10, 2);
    add_filter(\'woocommerce_localisation_address_formats\', array($this, \'mySecondFunction\'), 10, 2);

    return $this;
}
使用此功能,我可以添加/注册自定义字段,使其在默认情况下成为自定义字段,这样它将在插件部分以紫色字母显示(如页面顶部的屏幕截图所示):

public function woo_new_default_address_fields($fields)
{
    $fields[\'colonia\'] = array(

        \'label\' => __( \'Colonia\', \'woocommerce\' ),
        \'class\' => array( \'form-row-wide\' ),

    );
    $fields[\'num_int\'] = array(

        \'label\' => __( \'Num. Interior\', \'woocommerce\' ),
        \'class\' => array( \'form-row-wide\' ),

    );
    $fields[\'num_ext\'] = array(

        \'label\' => __( \'Num. Exterior\', \'woocommerce\' ),
        \'class\' => array( \'form-row-wide\' ),

    );
    $fields[\'municipio\'] = array(

        \'label\' => __( \'Municipio\', \'woocommerce\' ),
        \'class\' => array( \'form-row-wide\' ),

    );
    $fields[\'reference\'] = array(

        \'label\' => __( \'Referencias\', \'woocommerce\' ),
        \'class\' => array( \'form-row-wide\' ),

    );

    $order = array(
        "first_name",
        "last_name",
        "colonia",
        "num_int",
        "num_ext",
        "municipio",
        "reference",
        "company",
        "country",
        "address_1",
        "address_2",
        "city",
        "state",
        "postcode"
    );

    foreach($order as $field) {

        $ordered_fields[$field] = $fields[$field];

    }

    $fields = $ordered_fields;

    return $fields;
}
然后我将它们添加到电子邮件的格式化帐单地址:

public function woo_custom_order_formatted_billing_address( $address , $WC_Order )
{

    $address = array(
        \'first_name\'    => \'Nombre: \' . $WC_Order->billing_first_name . \' \' . $address[\'last_name\'],
        \'address_1\'     => \'Dirección: \' . $WC_Order->billing_address_1,
        \'colonia\'     => \'Colonia: \' . $WC_Order->billing_colonia,
        \'num_ext\'       => \'Num. Exterior: \' . $WC_Order->billing_num_ext,
        \'num_int\'       => \'Num. Interior: \' . $WC_Order->billing_num_int,
        \'reference\'     => \'Referencias: \' . $WC_Order->billing_reference,
        \'city\'          => \'Ciudad: \' . $WC_Order->billing_city,
        \'state\'         => \'Estado: \' . $WC_Order->billing_state,
        \'municipio\'     => \'Municipio: \' . $WC_Order->billing_municipio,
        \'postcode\'      => \'Código Postal: \' . $WC_Order->billing_postcode,


    );

    return $address;

}
在此,我将我的自定义字段替换/添加到地址中,以便在格式化的帐单地址上使用这些字段:

public function myFunction( $address, $args )
{
    $address[\'{colonia}\'] = $args[\'colonia\'];
    $address[\'{num_int}\'] = $args[\'num_int\'];
    $address[\'{num_ext}\'] = $args[\'num_ext\'];
    $address[\'{municipio}\'] = $args[\'municipio\'];
    $address[\'{reference}\'] = $args[\'reference\'];
    return $address;
}
然后,我将替换/添加它们,并说明其显示方式:

public function mySecondFunction($formats)
{
    foreach($formats as $key => $value) :
        $formats[$key] .= "\\n\\n{colonia}";
        $formats[$key] .= "\\n\\n{num_int}";
        $formats[$key] .= "\\n\\n{num_ext}";
        $formats[$key] .= "\\n\\n{municipio}";
        $formats[$key] .= "\\n\\n{reference}";
    endforeach;

    return $formats;
}
我希望你们都能理解我,我不是专家:)

结束

相关推荐

如何在选项表格中保存wp_EDITOR html内容

wp_editor( $default_content, $editor_id,array(\'textarea_name\' => $option_name,\'media_buttons\' => false,\'editor_height\' => 350,\'teeny\' => true) ); submit_button(\'Save\', \'primary\'); 我想创建一个邮件模板,管理员可以在其中更改内容并将快捷码放在他想要的地