How to implement "product gallery" feature of woocommerce into a custom post type
I'm wondering if there is a way to borrow the "product gallery" feature of woocommerce and add it to a custom post type in wordpress.
I know i can go through the woocommerce source codes and search and find its nuts and bolts and pieces in the woocommerce plugin folder, put them together and customize them for a specific custom post type. I know these are some of the corresponding files in the woocommerce plugin folder:
-
For its javascript:
- meta-boxes-product.js
- single-product.js
-
For rendering its html template:
-
wc-template-functions.php > There is
wc_get_gallery_image_htmlfunction -
class-wc-admin-meta-boxes.php
-
add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' );
-
- class-wc-meta-box-product-images.php
- etc.
-
wc-template-functions.php > There is
BUT
this is prone to errors and bugs. I'm wondering if there is a
better
,
easier
,
more elegant
,
less buggy
way to implement this feature to a custom post type. Maybe there is a
hook
and/or a
class
somewhere that you guys know about and i'm missing here.
Just to clarify my question, i actually have a custom post type it's called "projects". I need a gallery for this custom post type which accepts new images for my custom post type, so i thought i could use the "product gallery" feature from woocommerce instead of writing a gallery from scratch for my custom post type.
Thank you.
Solutions
Not tested but if you could get the
product_id
inside your CPT file try this code:
<?php
$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display the image URL
echo $Original_image_url = wp_get_attachment_url( $attachment_id );
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
?>
This should work!