Skip Navigation

[Resolved] Product Page as CPT

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 28 replies, has 2 voices.

Last updated by CharlesB2907 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#626611

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck

Sorry for the delay getting back to you, it was a long weekend here in Ireland.

The workflow you are describing goes back to one of my first points that this means duplication. For every painting, ecollage, or collage post that you create, a corresponding product post needs to be created as well, to go through the check-out process.

I'm not sure from your workflow whether you intend to create those duplicate products at the time the art posts are created, or on-the-fly when someone orders one of the artworks (which would require you to first check whether it had already been created by someone else ordering the same product—if that is possible because there are duplicates).

In either case, that is only something that would be possible with custom coding, to generate products based upon the fields of your various artwork posts.

If you wanted to go down that route, probably creating the matching product at the same time an artwork post is created, you would need to use the save_post hook (https://developer.wordpress.org/reference/hooks/save_post/) so that when such posts are created (or updated) you create a corresponding product, with fields that come from the artwork post, and you would need to save the id of the newly created product as a custom field on your artwork post to connect the two.

I'm not sure this is any easier than what we talked about before.

#626673

Hi,

I appreciate your argument regarding duplicate pages. However, I know my clients well and they are going to have an issue of their overall post-type structure as "product", instead of reflecting the nature of their business model.

I am hoping to achieve the following, and any support you can offer in this regard would be greatly appreciated.

1. The "product" post-type page need only be a summery pop-up including the following content: the featured image, type, price and send to cart button. The page would serve as a confirmation summary using the "product" post type.

2. When the site owner adds content to display a painting, ecollage or collage to the site from a front-end form, it would be perfect if the form could allow the user to indicate the content for the summary page as well.

3. Therefore, the "ecollage" post type Profile Page content and the "product" post type Summary Page will be created at the same time, when the artwork is first added to the site.

Does this seem logical and possible to achieve with Toolset apps?

#626681

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck

Yes, okay, that is do-able, except it will require custom code to implement, using the save_post hook as described above.

I can help with that, but I'm just finishing for the day.

I'll look again tomorrow and suggest something you can use as a starting point on your own site.

#626683

Thank you. I look forward to your next post.

#627014

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck

I drafted some code that will automatically generate a product post that copies some of the content from your art post types, assuming for example that you have a custom field of "price" that will be used to set the price of the WooCommerce product.

Here is the sample code. Please note that providing such code falls well outside of our support policy. I have provided it as a starting point for you to adapt to your own site, but if you are not able to do that you'll need to seek the help of a developer (such as one of the Toolset contractors linked to in the sidebar).

/**
 * Auto-generate products from art post types
 */
add_action( 'save_post', 'tssupp_generate_product', 101, 3 );
function tssupp_generate_product( $post_id, $post, $update ){

 $art_types = array( 'painting', 'ecollage', 'collage' );
 $product_field = 'wpcf-' . 'linked-product-id';

 	if ( in_array( $post->post_type, $art_types ) ) {

 		// Preparing standard fields to duplicate on product
 		$fields = array(
 			'post_type'		=>	'product',
 			'post_status'	=>	'publish',
 			'post_title'	=>	$post->post_title,
 			'post_content'	=>	$post->post_content
 		);

 		// is there already a connected product?
 		$product_id = get_post_meta( $post_id, $product_field, true );

 		if ( $product_id ) {
 			// update product
 			array_unshift( $fields, array( 'ID' => $product_id ) );
 			wp_update_post( $fields );
 		} else {
 			// create product
 			$product_id = wp_insert_post( $fields );

 			// set product ID on connected post
 			update_post_meta( $post_id, $product_field, $product_id );
 		}

 		/* Add/update other product fields */

 		// get post meta of art post to duplicate on product
 		$meta = get_post_meta( $post_id, '', true );

 		// featured image
 		update_post_meta( $product_id, '_thumbnail_id', $meta['_thumbnail_id'][0] );

 		// price field from wpcf-price
		update_post_meta( $product_id, '_regular_price', $meta['wpcf-price'][0] );

		// artist field from wpcf-artist
		update_post_meta( $product_id, 'wpcf-artist', $meta['wpcf-artist'][0] );

		// set WC Product fields to defaults
		update_post_meta( $product_id, '_visibility', 'visible' );
		update_post_meta( $product_id, '_stock_status', 'instock');
		update_post_meta( $product_id, 'total_sales', '0' );
		update_post_meta( $product_id, '_downloadable', 'no' );
		update_post_meta( $product_id, '_virtual', 'yes' );
		update_post_meta( $product_id, '_sale_price', '' );
		update_post_meta( $product_id, '_purchase_note', '' );
		update_post_meta( $product_id, '_featured', 'no' );
		update_post_meta( $product_id, '_weight', '' );
		update_post_meta( $product_id, '_length', '' );
		update_post_meta( $product_id, '_width', '' );
		update_post_meta( $product_id, '_height', '' );
		update_post_meta( $product_id, '_sku', '' );
		update_post_meta( $product_id, '_product_attributes', array() );
		update_post_meta( $product_id, '_sale_price_dates_from', '' );
		update_post_meta( $product_id, '_sale_price_dates_to', '' );
		update_post_meta( $product_id, '_price', '' );
		update_post_meta( $product_id, '_sold_individually', '' );
		update_post_meta( $product_id, '_manage_stock', 'no' );
		update_post_meta( $product_id, '_backorders', 'no' );
		update_post_meta( $product_id, '_stock', '' );

 	}
}

On your art post template you will want to add a button to link to this product so it can be bought.

In my example I added a custom field "linked-product-id" to the art posts that holds the product id, so you can create a link to that post using the wpv-post-url shortcode like so:

<a href="[wpv-post-url id='[wpv-post-field name='wpcf-linked-product-id']']">Buy this artwork</a>
#627110

Thank you. This is amazing support. Thank you. Please allow me a day to review this information before my next post.

#627196

I will need to try to accomplish this myself. I posted a customization project on the Toolset Contractors page and sent an invitation to several listed contractors with zero response. I see no viable reason to try posting again.

I'm just curious, when the site owner adds a new ecollage post, for example, using a front-end form to populate the fields that make up the display page for the post, why can there not be additional fields on the same front-end form that populate the fields of the "Product" summary page as well?

Is it not possible to create two display output pages using the same CRED Form?

#627274

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

A CRED form can only publish one post, so it can publish an ecollage post or a product post, but not both.

When you publish an ecollage post the corresponding product post doesn't exist yet, and you could use the cred_save_data hook to generate it in much the same way as I did above, although using the save_post hook means that the product is created (or updated) regardless of whether the ecollage post were published/edited in the front end or back end.

#627438

All artwork regardless of the type is one of a kind, which means there is only one to sell.

This means that when it is sold, the "Product" post-type summary page should 1. auto-delete or 2, update as "Sold" and not allow another instance of that particular item if the "Product" summary is ever triggered post-sell.

When an item is sold, the site owner will want to update the "Detail" page (in this example, an ecollage CPT) by logging into and updating the CRED Form for the post and change the "Location" field of the work to one of the following selector field options:

1. Available with Artist

(the work is for sale)

2. Available at [gallery name]

(if for sale at a gallery, the site owner should be able to select from listed gallery options to indicate the name of the gallery "Location" from the CRED Form. It would be helpful if the site owner can add a new "Location" if the option doesn't already exist as a form field option.

3. In Private Collection

(which means it has been sold)

4. Unknown

(which means that the "Location" of the work is unknown and no further information is available)

Your feedback on the above workflow would be greatly appreciated.

#628401

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck.

The queue is extremely busy at the moment and I am getting through tickets as fast as I can. I'll get back to you as quickly as possible. Sorry for the wait.

#628794

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Chuck

WooCommerce includes stock management, so when you create a product (automatically with the above code) you can set it up so that the available inventory is just a single item. If and when the product is bought the stock will automatically reduce to zero: it will be sold out and unavailable to purchase again.

Read about managing stock with WooCommerce here: https://docs.woocommerce.com/document/managing-products/

In the code I supplied above modify the following lines so that they have the required options set up correctly when the product is created:

        update_post_meta( $product_id, '_sold_individually', 'yes' );
        update_post_meta( $product_id, '_manage_stock', 'yes' );
        update_post_meta( $product_id, '_backorders', 'no' );
        update_post_meta( $product_id, '_stock', '1' );

You can create a CRED edit form that the site owner can use to update the location field for the piece. Make a select custom field with the 4 options you describe.

The difficult one is 2, inasmuch as it is not the same field, and there is no provision to modify custom field options from the front-end.

So, you will need a second custom field that only appears if option 2 is selected in the location field (see https://toolset.com/documentation/user-guides/cred-conditional-display-engine/).

This second field can record the gallery.

The simplest option would be a plain text field where the site owner can enter text for the gallery name/address.

Or—which might not be very intuitive—would be to make this not a custom field but a taxonomy, because CRED does allow you to add additional taxonomy terms from the front-end. (The required markup for this will be inserted if you use the auto-generate form markup button on your form.)

#628867

Hi Nigel,

I'm going to give this a try. Is the initial code you provided to be added to the sites function.php file in the child theme?

#629045

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That's right.

Don't forget to include the changes I suggested if you want to include stock control, and also please read through the code, particularly the first few lines, to double-check that I am using the same post and field slugs as you are on your actual site.

#775269

Hello,

Is there any chance this topic can be re-opened with Nigel or another support agent who may be able to assist me?