How to Use ACF Fields for WooCommerce Product Photos. See https://cfxdesign.com/how-to-use-acf-fields-for-woocommerce-product-photos
<?php | |
function load_existing_product_gallery_into_acf_gallery_field($field) { | |
global $post; | |
$field['value'] = explode(',', get_post_meta($post->ID, '_product_image_gallery', true)); | |
return $field; | |
} | |
add_filter('acf/load_field/name=product_gallery', 'load_existing_product_gallery_into_acf_gallery_field'); |
<?php | |
function load_existing_product_photo_into_acf_image_field($field) { | |
global $post; | |
$field['value'] = get_post_meta($post->ID, '_thumbnail_id', true); | |
return $field; | |
} | |
add_filter('acf/load_field/name=product_photo', 'load_existing_product_photo_into_acf_image_field'); |
<?php | |
function remove_woocommerce_product_image_meta_boxes() { | |
remove_meta_box('postimagediv', 'product', 'side'); | |
remove_meta_box('woocommerce-product-images', 'product', 'side'); | |
} | |
add_action('add_meta_boxes', 'remove_woocommerce_product_image_meta_boxes', 40); |
<?php | |
function use_acf_fields_for_product_photos($post_id) { | |
if('product' != get_post_type($post_id)) { | |
return false; | |
} | |
$product_photo = get_field('product_photo', $post_id); | |
if($product_photo && !empty($product_photo['ID'])) { | |
update_post_meta($post_id, '_thumbnail_id', $product_photo['ID']); | |
} | |
$product_gallery = get_field('product_gallery', $post_id); | |
if($product_gallery) { | |
$product_gallery_ids_string = implode(',', wp_list_pluck($product_gallery, 'ID')); | |
update_post_meta($post_id, '_product_image_gallery', $product_gallery_ids_string); | |
} | |
} | |
add_action('acf/save_post', 'use_acf_fields_for_product_photos', 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment