简体   繁体   English

将自定义字段图像移至产品库 Woocommerce

[英]Moving custom field images to product Gallery Woocommerce

I've several custom image fields (ACF) from an old configuration, and would like to move those images in the Product Gallery (Woocommerce), now I've converted all the datas into a Product post type.我有几个来自旧配置的自定义图像字段 (ACF),并且想在产品库 (Woocommerce) 中移动这些图像,现在我已将所有数据转换为产品帖子类型。 I tried to set this function (found in a similar post), but nothing happens, and no errors returned neither:我试图设置这个 function (在类似的帖子中找到),但没有任何反应,也没有错误返回:

function upload_all_images_to_product($product_id, $image_id_array) {
    //define the array with custom fields images
    $image_1 = get_field('images'); // should returns image IDs
    $image_2 = get_field('images-2');
    $image_3 = get_field('images-3');
    $image_4 = get_field('images-4');
    $image_5 = get_field('images-5');
    $image_6 = get_field('images-6');

    $image_id_array = array($image_1, $image_2, $image_3, $image_4, $image_5, $image_6);

    //take the first image in the array and set that as the featured image
    set_post_thumbnail($product_id, $image_id_array[0]);

    //if there is more than 1 image - add the rest to product gallery
    if(sizeof($image_id_array) > 1) { 
        array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
        update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
    }
}

Could someone help or explain me what's wrong?有人可以帮助或解释我出了什么问题吗?

Depending on where you run this function, you should define the $product_id argument in ACF get_field() function.根据您运行此 function 的位置,您应该在 ACF get_field() function 中定义$product_id参数。

Questions to clarify: How do you run this function?需要澄清的问题:你如何运行这个 function? are you using a hook?你在用钩子吗?

Update : Hooked the function in woocommerce_process_product_meta , so when a product is created or updated, it will trigger the code.更新:在 woocommerce_process_product_meta 中woocommerce_process_product_meta ,因此当创建或更新产品时,它将触发代码。

Also your code can be simplified, optimized and compacted as follow:您的代码也可以被简化、优化和压缩,如下所示:

add_action( 'woocommerce_process_product_meta', 'save_my_custom_settings' );
function upload_all_images_to_product( $product_id, $image_ids = array(); ) {
    // Loop from 1 to 6
    for ( $i = 1; $i <= 6; $i++ ) {
        $field_key = 'images'.( $i == 1 ? '' : '-'.$i );

        // Check that the custom field exists
        if( $field_value = get_field( $field_key, $product_id ) )
            $image_ids[] = $field_value; // Set each ACF field value in the array
    }

    if( ! empty($image_ids) ) { 
        // Take the first image (removing it from the array) and set it as the featured image
        set_post_thumbnail( $product_id, array_shift($image_ids) );
    }

    if( ! empty($image_ids) ) {     
        // Set the remaining array images ids as a coma separated string for gallery images
        update_post_meta( $product_id, '_product_image_gallery', implode(',', $image_ids) ); 
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 untested it could work.未经测试它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM