将自定义产品元数据传递到Woocommerce 4中的订单

雨果

我正在尝试将自定义产品元数据传递给订单。我可以将自定义产品元数据保存在管理员中,但是结帐时很难将值保存到订单元数据中。我正在使用Woo4。有人可以帮忙吗?谢谢,谢谢

add_action( 'woocommerce_product_options_general_product_data', 'product_meta_create_email_field' );
function product_meta_create_email_field() {
    $args = array(
        'id'            => 'email_estabelecimento',
        'label'         => __( 'Email do Estabelecimento', 'cfwc' ),
        'class'         => 'cfwc-custom-field',
        'desc_tip'      => true,
        'description'   => __( 'Insere aqui o email do estabelecimento.', 'ctwc' ),
    );
    woocommerce_wp_text_input( $args );
}

add_action( 'woocommerce_process_product_meta', 'product_meta_save_email_field' );
function product_meta_save_email_field( $post_id ){
    if( isset( $_POST['email_estabelecimento'] ) )
        update_post_meta( $post_id, 'email_estabelecimento', esc_attr( $_POST['email_estabelecimento'] ) );
}

add_action('woocommerce_checkout_create_order_line_item', 'save_email_as_order_item_meta', 20, 4);
function save_email_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if ( $estabelecimento = $values['data']->get_meta('email_estabelecimento') ) {
        $item->update_meta_data( 'email_estabelecimento', $estabelecimento );
    }
}
7uc1f3r

您很近,在这里和那里稍作调整

// Adding a custom field in the back-end
function product_meta_create_email_field() {
    $args = array(
        'id'            => 'email_estabelecimento',
        'label'         => __( 'Email do Estabelecimento', 'cfwc' ),
        'class'         => 'cfwc-custom-field',
        'desc_tip'      => true,
        'description'   => __( 'Insere aqui o email do estabelecimento.', 'ctwc' ),
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'product_meta_create_email_field', 10, 0 );

// Saving the custom field value
function product_meta_save_email_field( $post_id ) {
    // Get product
    $product = wc_get_product( $post_id );

    // $_POST
    $email_estabelecimento = isset( $_POST['email_estabelecimento'] ) ? $_POST['email_estabelecimento'] : '';

    // Update meta data
    $product->update_meta_data( 'email_estabelecimento', esc_attr( $email_estabelecimento ) );

    // Save
    $product->save();

}
add_action( 'woocommerce_process_product_meta', 'product_meta_save_email_field', 10, 1 );

// Displaying custom fields in the WooCommerce order and email confirmations
function save_email_as_order_item_meta($item, $cart_item_key, $values, $order) {
    $email_estabelecimento = $values['data']->get_meta('email_estabelecimento');

    if ( isset( $email_estabelecimento ) ) {
        $item->update_meta_data( 'email_estabelecimento', $email_estabelecimento );
    }
}
add_action('woocommerce_checkout_create_order_line_item', 'save_email_as_order_item_meta', 20, 4);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将自定义产品元数据传递到Woocommerce 3中的订单

将自定义数据传递到vue-router中的$ router.push()

如何从ui-router中的视图将自定义数据传递到状态?

将自定义数据传递到Symfony / Twig中的表单主题

如何将自定义数据传递到Yii中的EDataTables

将自定义发布的变量从页面传递到Woocommerce订单数据

在Woocommerce 3中将自定义元数据添加到管理订单编辑页面

将自定义数据传递给request_futures中的异常

将自定义数据传递给React中的PrivateRoute组件

将自定义元数据保存到Shipstation的WooCommerce订单

在Woocommerce 3中将购物车项目中的自定义数据传递到Order meta

将自定义用户字段传递给WooCommerce订单元数据

在Woocommerce中将产品的自定义元数据添加到订单项

如何在新订单电子邮件中显示WooCommerce自定义产品元?

在WooCommerce中将订单商品自定义元数据自动添加到订单注释中

保存订购产品变体到订单的自定义元数据

将自定义字段数据保存到Woocommerce产品变体的购物车和订单

将产品自定义字段值保存为WooCommerce中的自定义订单项元

在Woocommerce 3中将自定义结帐字段添加为“订购自定义元数据”

添加到Woocommerce的自定义元数据未在订单项元中显示

在 WooCommerce 中添加产品自定义输入文本作为订单项目数据

将自定义购物车商品值添加到WooCommerce订单商品元数据

将参数/输入数据传递到javascript中的自定义元素

从Woocommerce商店页面中的特定自定义元数据过滤产品

在WooCommerce中更新自定义订单项目元

WooCommerce:将自定义元添加为隐藏订单项元以供内部使用

将产品自定义字段另存为WooCommerce管理员手动订单的自定义订单项元数据

将自定义参数传递到 Azure 数据工厂中的存储过程

Rails / ActiveAdmin - 如何将自定义数据从控制器传递到内容中