Last active
August 25, 2021 20:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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