【问题标题】:ACF repeater based on product quantity generating checkout fields in Woocommerce基于产品数量的 ACF 转发器在 Woocommerce 中生成结帐字段
【发布时间】:2019-03-14 20:41:43
【问题描述】:

我正在使用 woocommerce-advanced-checkout-fields 插件,并在计费部分添加了一个转发器字段,如下所示

如上图所示,中继器字段“姓名/电子邮件”适用于产品“腰带”

现在当我去商店购买产品并按如下方式制作数量 3 时,中继器字段显示 3 次,一切都很好。

现在,当我下订单时,登录页面不包含我输入的值,如下所示

此外,这些值未显示在订单管理部分中,如下所示。

我相信我已经清楚地阐述了这个问题。需要您的建议来解决这个问题

【问题讨论】:

  • 希望它是一个高级插件,为什么你不能就这个问题联系支持?
  • 我可以在存储这些自定义转发器字段的插件中看到此代码 WC()->customer->set_meta_data WC()->customer->set_meta_data( array( array( 'id' = > $key, 'key' => $key, 'value' => $value ) ) );但是这些值在 post_meta 表中找不到
  • WC()->customer->set_meta_data 将值存储在 User 元数据中,但 NOT 存储在 post 元数据中…所以这似乎与您的问题无关。
  • 这可以在没有您的插件的情况下仅使用自定义代码来完成......但是当购物车中有多个商品时(例如 item1(数量 2)+ item2(数量 3)呢?
  • 您能从浏览器调试器中检查产品详细信息 HTML 吗?检查是否有一些带有通讯员姓名的输入在这里?也许它被什么东西隐藏了。您是否编辑过 woocommerce 模板?是买来的模板吗?

标签: php mysql wordpress woocommerce custom-fields


【解决方案1】:

自从您开始赏金以来,您没有回复任何评论。没有人可以在不知道您正在使用什么设置的情况下解决您的问题,以及订单的这个附加字段的数据库相关的帖子元数据(注册的元键)是什么......

要根据特定产品的商品数量不使用任何插件,为特定的自定义结帐结算字段获取中继器:

1) 产品附加设置 (激活此功能的复选框)

// Display a custom setting option on product edit pages
add_action('woocommerce_product_options_general_product_data', 'add_product_repeater_checkbox_option');
function add_product_repeater_checkbox_option(){
    echo '<div class="product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'          => '_qty_repeater',
        'label'       => __('Qty repeater', 'woocommerce'),
        'description' => __('Enable quantity repeater for additional "Name" and "Email" billing checkout fields', 'woocommerce'),
        'desc_tip'    => 'true'
    ));

    echo '</div>';
}

// Save the custom setting option value from product edit pages
add_action( 'woocommerce_admin_process_product_object', 'save_product_repeater_checkbox_option', 100, 1 );
function save_product_repeater_checkbox_option( $product ) {
    $qty_repeater = isset( $_POST['_qty_repeater'] ) ? 'yes' : 'no';
    $product->update_meta_data( '_qty_repeater', $qty_repeater );
}

2) 在结帐时添加/保存重复的附加字段 (并标记订单)

add_filter('woocommerce_billing_fields', 'additional_billing_checkout_fields', 50, 1 );
function additional_billing_checkout_fields( $billing_fields ) {
    foreach(WC()->cart->get_cart() as $cart_item ){
        // Check if the "Quanty repeater option is set for the current item
        if( $cart_item['data']->get_meta('_qty_repeater') === 'yes' && is_checkout() && $cart_item['quantity'] > 1 ) {

            // Quantity repeater
            for( $i = 1, $j = 2; $i < $cart_item['quantity']; $i++, $j++ ){

                // Name fields
                $billing_fields['billing_name_person'.$j] = array(
                    'type'        => 'text',
                    'label'       => __("Name", "woocommerce") . ' ' . $j,
                    'class'       => array('form-row-first'),
                    'required'    => true,
                    'clear'       => false,
                );

                // Email fields
                $billing_fields['billing_email_person'.$j] = array(
                    'type'        => 'email',
                    'label'       => __("Email", "woocommerce") . ' ' . $j,
                    'class'       => array('form-row-last'),
                    'required'    => true,
                    'clear'       => true,
                );
            }
            break; // Only for one item
        }
    }
    return $billing_fields;
}

// Mark Order as having this additional fields data values
add_action('woocommerce_checkout_create_order', 'save_additional_billing_checkout_fields', 20, 2);
function save_additional_billing_checkout_fields( $order, $data ) {
    foreach( $order->get_items() as $item ){
        $product = $item->get_product();
        // Mark the order as containing additional fields
        if( $product->get_meta('_qty_repeater') === 'yes' && $item->get_quantity() > 1 ) {
            $item->update_meta_data( '_qty_repeater', '1' );
            break; // Stop the loop
        }
    }
}

3) 随处显示额外的结算字段相关数据 (管理订单、订单视图、电子邮件)

// Display additional billing fields values
add_action('woocommerce_order_details_after_order_table', 'display_additional_billing_fields_values' ); // Order received and view
add_action( 'woocommerce_email_after_order_table', 'display_additional_billing_fields_values' ); // Email notifications
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_additional_billing_fields_values' ); // Admin edit Order
function display_additional_billing_fields_values( $order ) {

    if( $order->get_meta('_qty_repeater') ) {
        // Only for email notifications
        if( ! ( is_wc_endpoint_url() || is_checkout() || is_admin() ) ){
            echo '<style>
            table.customer-details {width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373; border: 1px solid #e4e4e4; margin-bottom:40px;}
            table.customer-details td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
                padding: 12px; padding-bottom: 4px;}
            </style>';
        }
        // Others
        else {
            echo '<style> table.customer-details, table.customer-details td { border: none; } </style>';
        }

        echo '<h2>' . __( 'Customer details', 'woocommerce' ) . '</h2>';
        echo '<div><table class="customer-details" cellspacing="0">';

        // Loop through order items
        foreach( $order->get_items() as $item ){
            $product = $item->get_product();
            if( $product->get_meta('_qty_repeater') === 'yes' ) {
                // Loop through item quantity
                for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
                    // Name
                    echo '<tr><td><strong>' . __("Name", "woocommerce") . ' ' . $j;
                    echo ': </strong>' . $order->get_meta('_billing_name_person'.$j) . '</td>';
                    // Email
                    echo '<td><strong>' . __("Email", "woocommerce") . ' ' . $j;
                    echo ': </strong>' . $order->get_meta('_billing_email_person'.$j) . '</td></tr>';
                }
                break;
            }
        }
        echo '</table></div>';
    }
}

4) 使其他帐单字段可编辑 (管理员)

add_filter( 'woocommerce_admin_billing_fields' , 'additional_admin_editable_billing_fields' );
function additional_admin_editable_billing_fields( $fields ) {
    global $pagenow, $post;

    if( $pagenow != 'post.php' ) return $fields;

    $order = wc_get_order($post->ID);

    if( $order->get_meta('_qty_repeater') ) {
        // Loop through order items
        foreach( $order->get_items() as $item ){
            $product = $item->get_product();
            if( $product->get_meta('_qty_repeater') === 'yes' ) {
                // Loop through item quantity
                for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
                    $fields['name_person'.$j] = array(
                        'label'         => __("Name", "woocommerce") . ' ' . $j,
                        'show'          => false,
                        'wrapper_class' => 'first',
                    );
                    $fields['email_person'.$j] = array(
                        'label'         => __("Email", "woocommerce") . ' ' . $j,
                        'show'          => false,
                        'wrapper_class' => 'last',
                    );
                }
                break;
            }
        }
    }
    return $fields;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

  • LoicTheAztec,你真是个天才!非常感谢。我只有一个新手问题:我可以改变什么让repeater显示的字段数减1。因为一个人的详细信息已经在Billing details中,所以我只需要其他人的详细信息,其数量=total_cart_items - 1.
  • 我做了一个小改动:for( $i = 1, $j = 2; -> 只有名字 2 和名字 3。我做的对吗?
  • @Alex 您的更改是正确的……我进行了更多更改测试相关项目数量是否大于 1 以启用此功能。所以步骤 2、3 和 4 被更新。 save_additional_billing_checkout_fields() 函数也有一个疏忽……
  • @FrankBailey 是的,我知道,我讨厌自己:]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多