Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cfxd/5a799464fe59ecac1c2d409b49114d4b to your computer and use it in GitHub Desktop.
Save cfxd/5a799464fe59ecac1c2d409b49114d4b to your computer and use it in GitHub Desktop.
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_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