wordpress - ACF中继器字段上的产品变化

根据此处此处,使用以下代码将ACF添加到各个产品变型中。 除转发器字段外,其他所有内容似乎均显示正确:

// Add product variations ACF rule
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
    $choices['product_variation'] = 'Product Variation';
    return $choices;
}

$GLOBALS['wc_loop_variation_id'] = null;

function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    return (preg_match( '/Variation/i', $field_group['title'] ) == true);
}

add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    $GLOBALS['wc_loop_variation_id'] = $variation_post->ID;

    foreach ( acf_get_field_groups() as $field_group ) {
        if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
            acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
        }
    }

    $GLOBALS['wc_loop_variation_id'] = null;
}, 10, 3 );

add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
        return;
    }

    $_POST['acf'] = $_POST['acf_variation'][$variation_id];

    acf()->input->save_post( $variation_id );
}, 10, 2 );

add_filter( 'acf/prepare_field', function ( $field ) {
    if ( !$GLOBALS['wc_loop_variation_id'] ) {
        return $field;
    }

    $field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );

    return $field;
}, 10, 1);

在转发器字段上添加行时,它不会出现。

然后我在保存产品时收到以下javascript错误,但是重复多次,但是我单击了“添加行”:

名称='acf_variation [37] [field_5db9a1722bbe2] [field_5db9a1de2bbe5] [acfcloneindex] [field_5db9a1ed2bbe6]'的无效表单控件不可聚焦。

当我仅检查一个表行时,带有class="acf-row acf-clone" ACF的样式表显示:

.acf-repeater .acf-row.acf-clone {
    display: none !important;
}

当我添加管理员样式时:

.acf-repeater .acf-row.acf-clone {
    display: table-row !important;
}

显示第一个字段,但其他字段仍未添加(检查时同时显示在DOM中。有什么想法吗?

渲染字段后添加脚本:

function rdv__after__render_field( $field ){
    //add these if you want to apply it on certain fields only 
    //if ($field['key'] != 'field_key') {
    //    return;
    //}
    echo "<script>
            (function($) {
                acf.doAction('append', $('#post'));
            })(jQuery);
          </script>";
}
add_action( 'acf/render_field/type=repeater', 'rdv__after__render_field', 10, 1 );

然后将您的“ woocommerce_save_product_variation”(在https://support.advancedcustomfields.com/forums/topic/acf-on-product-variations-almost-works/上获得此代码)更改为

add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
        return;
    }

    if ( ! empty( $_POST['acf_variation'][$variation_id] ) && is_array( $fields = $_POST['acf_variation'][$variation_id] )  ) {
        foreach ( $fields as $key => $val ) {
            update_field( $key, $val, $variation_id );
        }
    }

}, 10, 2 );

转载请注明来自askonline.tech,本文标题:wordpress - ACF中继器字段上的产品变化


 Top