Skip Navigation

[Resolved] Create new product form

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm trying to replicate the same process of a previous form with another post type, But does not work.

Solution:

This is a custom codes problem, and client has found the solution here:

https://toolset.com/forums/topic/create-new-product-form/#post-1154821

Relevant Documentation:

This support ticket is created 5 years, 5 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 13 replies, has 2 voices.

Last updated by domenicoS 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1152914

Hi,

I'm trying to replicate the same process of a previous form with another post type.

I've create a new post type type "offer":hidden link

I've add a cred in the offer layout to add a product (as for the tour): hidden link

I've also update the function adding the form id 31952.

But does not work.

It does not update the price and the summury

#1152957

Hi,

This is a custom PHP codes problem, according to our support policy, we don't provide custom codes support.

In your case, I suggest you try to make the PHP codes easily, for example in your theme file "functions.php", line 185~196

	$form_ids = array(
		24662,
		25474, 
		31952
	);
	if( !isset($cart_item['cred_meta']['cred_form_id'], $cart_item['cred_meta']['cred_post_id']) ) { // if it is from Toolset form 
		return $name;
	}
	$cred_form_id = $cart_item['cred_meta']['cred_form_id'];
	if( !in_array( $cred_form_id,  $form_ids) ) { //if it is from specific form
		return $name;
	}

This should be able to update each item's summary

Similar as above, line 741 ~751

    $form_ids = array(24662, 25474,31952); //here setup form IDs
    foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {
        
		if( !isset($cart_item['cred_meta']['cred_form_id'], $cart_item['cred_meta']['cred_post_id']) ) {
			continue;
		}
		$cred_form_id = $cart_item['cred_meta']['cred_form_id'];
		
        if(!in_array($cred_form_id, $form_ids)){
            continue;
        }

As you can see, we can check only one conditional at one time, this will be easy to debug the PHP codes

For the total price in Cart page problem, there should be something wrong in your PHP codes, I have tried to add a line to output total price, it display 0 value, see line 843:

//var_dump($price);

You can change it to:

var_dump($price);

And test again, you should be able to see the result:
int(0)

I suggest you debug your PHP codes manually, line by line.

#1153153

The problem is that all work fine with form 24662(hidden link) but not with 31952 (hidden link).

I don't think is a code problem

The 24662 form is displayed in a product and 31952 form is displayed in a cusyom post type offer, this is the only diffrence

#1153542
cart-page.JPG

No, the action hook "woocommerce_before_calculate_totals" does work for Toolset form "31952", I have edited your theme file "functions.php", line 752~754, add below line:

		else if(is_page(20050)){ // if it is cart page
			echo 'This post is submitted by form ID: ' . $cart_item['cred_meta']['cred_form_id'];
		}

You can test it in front-end:
hidden link

Submit the form "31952", and it will redirect you to the cart page:
You should be able to see the result:

This post is submitted by form ID: 31952

See screenshot: cart-page

It is your custom codes setup the price value into 0, but it is out the range of Toolset support, you will need to debug your custom codes manually one by one.

#1153779

Ok Luo,

I find where is the problem. Is linked to product_id.

For example in this string:

$price_calculation = get_post_meta($product_id, 'wpcf-price-calculation', true);

If I set var_dump for product_id I see correctly the id of the custom post type offer (31880) hidden link

But if I set var_dump for $price_calculation that is a custom field associated to the custom post type offer I don't se any value. But as you can see in the post it is set on 2 "Prezzo a persona".

The same for all the string linke this (where the value is linked to product_id):

$adult_price = get_post_meta($product_id, 'wpcf-adult-price', true);

Why it works with form 24662 but not with 31952?

#1154010

Hi Luo, I found the problem, but I need your help to fix it in the right way.

In particular in this string:

$ product_id = $ cart_item ['data'] -> get_id ();

Infact I'm working on two different types of posts: one is a woocommerce product (tour) and the other one is a custom post type (offer).

In case of a tour (woocommerce product) it work fine and price and other meta are correct.
In case of an offer (cutom post) the product id is not correct, because the meta was stored in the post and not in the product.

I think a possible solution is to set a condition to catch the right id.
Something like this:

$ product_id = $ cart_item ['data'] -> get_id ();
if ($ product_id = 31890) {$ product_id = // the post id where the cred is displayed;}

In few words I need to set different product id if it is an offer, and should be the id of the post where the form is displayed.

How can I catch this id?

I do not know if you have better solution ..

#1154412

As I mentioned above, this is a custom codes problem, according to our support policy, we don't provide custom codes support.
https://toolset.com/toolset-support-policy/

as a work around, you can edit your post form "31952":
in section "Product to buy when submitting the form", choose the specific product you want.

You can also check it with our Toolset contractors:
https://toolset.com/contractors/

#1154413

Hi Luo,

Can you please only provide an example to how to catch the id of the post where the CRED is displayed.
Is simply this the issue.

I've the id of the CRED but simplyne ed the id of the post where it is displayed

#1154432

OK, I will try to help you to locate the problem, but it is in low priority, and will update here if there is anything found

#1154564

Here are what I found, in the problem page:
hidden link
It is a post of custom post type "offer", it is not a product post, which product is do you want to put into the cart?
Is there any connection between "offer" post and "product" post?

In your website relationship setting:
hidden link

There isn't any direct relationship between "offer" and "product", can you check it?

#1154586

Yes, it is a custom post offer.
the product linked to the offer is "Pacchetto Offerta".

I use this form to add buy the product: hidden link

Yes you are right, I've not set any relationship "offer-product" at the moment

#1154602

I think there is some misunderstand, as I mentioned above:
problem page: hidden link
It is a post of custom post type "offer", it is not a product post, you can not put it into the Woocommerce cart, you can only put the product post into cart.

For the quesiton:

Can you please only provide an example to how to catch the id of the post where the CRED is displayed.

I don't think it is possible, you might consider to connect the "Product" and "offer" with some relationship.

#1154610

Of course.

The process is:

1) I've a custom post offer, with some custom field like price etc.

2) I've a CRED form. With this form I create an other custom post type (as you suggest me in a previous ticket about tour) "Ordini Tour". In this form I've activate the command "Charge payment with this form" and associate the product "Pacchetti offerte"

3) this form is diplayed in all "offer" custom post

3) When a user fill in the form he is bring to the checkout page and can buy the product "Pacchetti soggiorni".

The only issue is change se cart price and the cart summury. I've done it with custom post in my file function.php

We have done the same thing with "Tour" and with "Preventivi" custom post and they works perfectly.

In this case (Offer) the problem is only to catch the right post_id

Is possible to do because as I said we have done the same thing with other post. It is only difficult to explain

#1154821

Hi, I found this solution

1) Set a relationship Offer-product

2) Put thi field in the form to create a new "offer" post type.

[cred_field field='@offer-product.parent' class='form-control' output='bootstrap' select_text='--- non impostato ---' value="[wpv-post-id id='31890']"]

So the relationship with my product "Pacchetti offerte"is automatically set.

3) Add this code to my file

		$offer_id = toolset_get_related_post( $product_id, 'offer-product',  'child');
		if ($product_id = 31890 ) {$product_id =$offer_id ;}

In this way it takes the data-field from the custom post offer.

It works fine.

Thank you

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.