Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach Woocommerce Products #53

Open
Uniswap-v2-Frontend opened this issue Mar 13, 2018 · 1 comment
Open

Attach Woocommerce Products #53

Uniswap-v2-Frontend opened this issue Mar 13, 2018 · 1 comment

Comments

@Uniswap-v2-Frontend
Copy link

Hi,

Is it possible to attach woocommerce products with a custom post type? If yes, can someone please tell me as I am new to CMB2. Thanks in advance.

@monecchi
Copy link

Yeap! It is totally possible. Just set up the field to query for the product post type. You can inspect the example field setup just to make sure you're heading in the right direction.

e.g.

$prefix = '_myprefix';
...
$my_field->add_field( array(
    'name'    => esc_html_( 'Related Product', 'cmb2' ),
    'desc'    => esc_html_( 'Drag products from the left column to the right column to attach them to this post.', 'cmb2' ),
    'id'      => $prefix . 'product_attached_posts',
    'type'    => 'custom_attached_posts',
    'options' => array(
        'show_thumbnails' => true, // Show thumbnails on the left
        'filter_boxes'    => true, // Show a text box for filtering the results
        'query_args'      => array(
        'posts_per_page' => 10,
        'post_type'      => 'product',
        'product_category'  => 'videos'
        ), 
    ),
) );
...

Then

$attached_products = get_post_meta( get_the_ID(), '_myprefix_product_attached_posts', true );

This will return an array of attached post IDs. You can loop through those post IDs like the following example:

foreach ( $attached_products as $attached_product ) {
  $_product = get_post( $attached_product );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants