WooCommerce Prdoucts Image Gallery With Metabox image Fields

Support General WooCommerce Prdoucts Image Gallery With Metabox image Fields

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #7896
    brkardbrkard
    Participant

    Hi

    I want to use metabox plugin for adding woocommerce products...

    I need to add woo product image gallery with metabox image fields.

    WooCommerce uses _product_image_gallery custom field to store the image gallery. It stores those images/attachments IDs separated by comma. Ex: 32,34,3874 in that single custom field.

    So i need to save the image IDs to the WooCommerce's _product_image_gallery meta key when i publish/update product. Also i need to do this with front-end form submission extension.

    I can do this with another plugin with a snippet. Here i share the code for that:

    /**
    * Update the custom field when the form submits
    *
    * @param type $post_id
    */
    function wpuf_woo_product_gallery( $post_id ) {
    if ( isset( $_POST['wpuf_files']['_product_image'] ) ) {
    
    $images = get_post_meta($post_id, '_product_image' );
    update_post_meta( $post_id, '_product_image_gallery', implode(',', $images) );
    }
    }
    
    add_action( 'wpuf_add_post_after_insert', 'wpuf_woo_product_gallery' );
    add_action( 'wpuf_edit_post_after_update', 'wpuf_woo_product_gallery' );

    Can do this with metabox ? Can you share a snippet for doing this with metabox if possible ? Sorry that i am not a developer.

    Thanks

    #7904
    Anh TranAnh Tran
    Keymaster

    Yes, it's possible. You need to:

    - Register a field with id _product_image_gallery
    - Filter to the value when saving to the database
    - Filter to the value when outputting the field

    This is the sample code. Please try.

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
    	$meta_boxes[] = [
    		'title'      => 'Save gallery as CSV',
    		'post_types' => 'product',
    		'fields'     => [
    			[
    				'type' => 'image_advanced',
    				'id'   => '_product_image_gallery',
    			],
    		],
    	];
    	return $meta_boxes;
    } );
    
    add_filter( 'rwmb__product_image_gallery_value', function ( $value ) {
    	$value = implode( ',', $value );
    	return $value;
    } );
    add_filter( 'rwmb__product_image_gallery_field_meta', function ( $meta ) {
    	if ( is_string( $meta ) ) {
    		$meta = explode( ',', $meta );
    	}
    	return $meta;
    } );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.