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

时间:2019-10-30 15:23:17

标签: wordpress advanced-custom-fields acfpro

根据herehere,使用以下代码将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错误,但重复多次,但我单击了“添加行”:

  

具有无效的表单控件   name ='acf_variation [37] [field_5db9a1de2bbe5] [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中。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

渲染字段后添加脚本:

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 );