[+] Publish all posts connected with one Order when using Forms Commerce multiple times to create multiple posts in the same Order

In this case, Forms Commerce is used to submit multiple posts with the same Order. The Form setting indicates that the post should be published when the Order is completed. However, only one post will be published automatically due to a limitation in the Forms Commerce system. Only one post is expected per Order, so only the newest post will be published when the Order is completed.

To automatically publish all posts connected with the Order, a custom code snippet is required:

// publish all posts created when one commerce form is submitted mutliple times in the same order
add_action( 'cred_commerce_after_order_completed', 'publish_all_ordered_posts', 10, 1 );
function publish_all_ordered_posts( $data ) {
  // which order was completed?
  $order_id = $data['transaction_id'];
  // which posts were created in this order?
  $all_order_posts = get_post_meta( $order_id, '_cred_post_id');
  // loop over all the created posts
  if( !is_array( $all_order_posts ) )
    return;
  foreach( $all_order_posts as $all_order_post ) {
    // check the post status
    $this_post_status = get_post_status( $all_order_post );
    // publish any unpublished posts
    if ( $this_post_status != 'publish' ) {
      $args = array(
        'ID' => $all_order_post,
        'post_status' => 'publish'
      );
      wp_update_post( $args );
    }
  }
}

Use repeating image field in CRED New Product form to define Product Gallery images

A client wants to use CRED to create new WC Products. When creating the Product, the Client would like to upload multiple images that will be used to create an image gallery. The solution is to add a repeating image custom field, and use the cred_save_data hook to move those images out of the custom fields and into a product gallery.

Ticket link: https://wp-types.com/forums/topic/cred-repeating-image-fields-gallery-woo-and-non-woo/

/* ----------------------------------------------------------------------- */
// CREATE A PRODUCT IMAGE GALLERY USING A REPEATING IMAGE FIELD IN CRED
// User uploads to a repeating custom image field
// Images are automatically moved into product image gallery
// Custom fields are left blank
// keywords: cred, product, image, gallery, repeating, field

add_action('cred_save_data', 'cred_product_gallery_action',10,2);
function cred_product_gallery_action($post_id, $form_data)
{
    global $wpdb;
    // if a specific form
    if ($form_data['id']==12345)
    {
        if (isset($_POST['wpcf-product-gallery-image-cred']))
        {
           $list_ids = array();
            foreach($_POST['wpcf-product-gallery-image-cred'] as $url) {
              $item_id = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ));
              $list_ids[] = $item_id[0];
            }
            update_post_meta($post_id, '_product_image_gallery', implode($list_ids,','));
            delete_post_meta($post_id, 'wpcf-product-gallery-image-cred');
        }
    }
}

Add support for gallery/popup/lightbox on WooCommerce product pages – Applicable on WC 3.X.X

With WC 3.0 they removed the support of gallery/popup/lightbox on WooCommerce product page.

To Add support for gallery/popup/lightbox on WooCommerce product pages, please try to add following code lines to theme’s functions.php file:

add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );

WooCommerce 3+ featured product filter

Filter to show only “featured” products, compatible with WC 3+.

Ticket URL: https://wp-types.com/forums/topic/toolset-views-not-showing-_featured-in-view-gui-for-woocommerce/

As of WC 3, Featured Products are managed a bit differently. Before, we could use the hidden field “_featured” to filter a View. Now the flag has been migrated to a hidden taxonomy “product_visibility” and has a term name “featured”. Until this is supported in the Views GUI, a filter can be used to add the “featured” product filter to a search:

/* --------------------------------------------- */
// WooCommerce 3+ featured products filter
// Only include "featured" products
add_filter( 'wpv_filter_query', 'featured_products',99,3 );
function featured_products( $query_args,$views_settings, $view_id) {
 
  if ($view_id == 10){
    $query_args = array(
      'post_type'  => 'product',
      'tax_query' => array(
        array(
          'taxonomy'     => 'product_visibility',
          'field'   => 'name',
          'terms' => 'featured',
          'operator' => 'IN'
        ),
      ),
    );
  }
  return $query_args;
}

Be sure to change $view_id == 10 to match your View.

Evaluate if product is bought by user

This shortcode evaluates if a certain (logged in) user has bought a certain (current in loop) product. The WooCommerce function seems buggy and some attention is needed. Check the code first to see if it is still working with the workaround.

functions.php:

add_shortcode('wpv-product-purchased', 'wpv_product_purchased_func');
  
function wpv_product_purchased_func($atts){
$current_user = wp_get_current_user();
$email = $current_user->email;
return (wc_customer_bought_product( $email, $current_user->ID, get_the_ID()));
}

Register this shortcode in Views > Settings:

wpv-product-purchased

Then evaluated as this:

 

 

The function of WooCommerce itself is returning a error, and the ShortCode works as expected, although it returns a numerical value “1” for bought and a empty value “” for not bought, not a boolean “true” or “false” as stated by WooCommerce.

This might be a Bug in their code or misunderstanding on my end reading the Core code of WooCommerce, and it could therefore change in future.