Hi ––
I have a custom post type called Listings. Each Listing has a price. The user submits a form that includes all the Listing information (including Price).
My client would like to charge based on the price of the item listed (<$500 is free, $500-$9,999 is $9 per listing, $10K+ is $19 per listing). I have created those as WooCommerce products. Additionally, they would like to have an optional "Featured" fee on top of the standard fee –– which I have created as a separate WooCommerce product ($10).
I think this isn't possible, but that's why I'm asking!
I can configure a conditional field based on the price that works for one of the products (when the inputted price is $750, I can show a radio button with a custom value that is the $500-$9,999 listing product ID) and that works. I just can't seem to figure out how to do this for the different products and keep it a mandatory field –– obviously we don't want customers unselecting an option that prevents them from checking out.
And, on top of that, add a "Featured" fee which is an optional checkbox? I'm not sure we can accomplish both at the same time, but any pointers or solutions? I'm comfortable working with some custom PHP to make this happen too.
My thought would be to create a script that points to the correct checkout based on the inputted price and the Featured listing checked/unchecked, but I'm not super familiar with the Hooks API yet so any pointers would be appreciated.
Hello,
Please elaborate the questions with more details:
I just can't seem to figure out how to do this for the different products and keep it a mandatory field
Where and how do you setup the conditional field? in the Toolset post form for creating new "Listing" post?
If it is, after user submit the form, in server side, you can use filter hook cred_commerce_add_product_to_cart to change the product in cart automatically, see our document:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart
Hi, well, sort of! This is definitely the right direction. Thanks for the hook recommendation.
First, is it possible to pass a specific field as the argument in the cred_commerce_add_product_to_cart? It'd be much easier to just grab the "Price" field and have if/elseif statements return different products. How is the "$post_id" generated? Or could I even use cred_commerce_form_action and use the "$form_data" to create an argument and function to accomplish this?
If that's not possible, to answer:
Where and how do you setup the conditional field? in the Toolset post form for creating new "Listing" post?
The conditional field would be inside the Post Form for creating a new "Listing" post. I'm still unsure exactly how to structure it, but currently there are two different Radio buttons in the Listing Information Field Group, each with conditions to show whether the price is above $500-$9999 or $10,000+.
Again, I would prefer just to write something that grabs the Price field and adds the correct product to the cart and remove the need for any conditional fields.
You can use PHP POST variable to get the user's input, and use it in your custom PHP codes
hidden link
if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URLs, and where I can edit your PHP codes, private message box enabled.
Thanks for the details, I have done below modifications in your website:
1) Add two product posts:
hidden link
- Private Listing ($10,000+) (Featured)
- Private Listing ($500+) (Featured)
2) Dashboard-> Toolset-> Settings-> Custom Code, add an item "conditional_product", with below codes:
add_filter( 'cred_commerce_add_product_to_cart', 'conditional_product', 10, 3 );
function conditional_product( $product_id, $form_id, $post_id ) {
if ( $form_id == 1060 ) { // post form "Add a Listing"
$price = get_post_meta($post_id, 'wpcf-price', true);
$featured = get_post_meta($post_id, 'wpcf-featured', true);
if($price >= 500 && $price <= 9999){
$product_id = 1233; // Private Listing ($500+)
if($featured == 1255){
$product_id = 1291; // Private Listing ($500+) (Featured)
}
}
if($price > 9999){
$product_id = 1234; // Private Listing ($10,000+)
if($featured == 1255){
$product_id = 1290; // Private Listing ($10,000+) (Featured)
}
}
}
return $product_id;
}
Please test again, check if it is what you want, thanks
My issue is resolved now. Thank you!