Adding parent and child tags to a form

jm987jm987 Member




Hello,

I'm working on adding products to a website. However, the way we do things is a bit different compared to the examples, and I'd like a little bit of help. I'm reusing some old code from a currently inactive developer, and I'd like to add a parent code tag to two products so we have a reference for a child product I'd like to add. However, I can't find where I'd add the parent code tag so it would work. The code is below.

<h2 style="text-align: center;">BUY&nbsp;NOW</h2>
<!-- foxycart start -->
<div class="foxycart-table">
<table class="tftable" border="1">
<tbody>
<tr>
<th>
<p><strong>Package&nbsp;<br></strong></p>
</th>
<th>
<p><strong>Description</strong></p>
</th>
<th>&nbsp;</th>
</tr>
<tr>
<td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"><input name="name" type="hidden" value="Monthly Subscription"> <input name="price" type="hidden" value="2000" data-min="2000"> <input name="sub_frequency" type="hidden" value="1m"> <input name="renewal_and_termination" type="hidden" value="The service continues monthly unless and until either party provides 30 days written termination notice."> <input name="code" type="hidden" value="keynote_monthly"> <input name="category" type="hidden" value="Keynote Monthly Subscription"> <input name="2:parent_code" type="hidden" value="keynote_monthly"><p>Option 1</p>
<td>
<p><span class="monthly_fee">Entering for up to one local, national or industry award opportunity per month.</p>
</td>
<td><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></td>
</tr>
<tr>
<td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"><input name="name" type="hidden" value="Monthly Subscription"> <input name="price" type="hidden" value="2500" data-min="2500"> <input name="sub_frequency" type="hidden" value="1m"> <input name="renewal_and_termination" type="hidden" value="The service continues monthly unless and until either party provides 30 days written termination notice."> <input name="code" type="hidden" value="keynote_monthly"> <input name="category" type="hidden" value="Keynote Monthly Subscription"> <input name="2:parent_code" type="hidden" value="keynote_monthly"><p>Option 2</p>
<td>
<p><span class="monthly_fee">Our KeyNote speaking engagement requires a $2,500 upfront, non-refundable services retainer.</p>
</td>
<td><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></td>
</tr>
<tr>
<td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"><input name="name" type="hidden" value="Monthly Subscription"> <input name="price" type="hidden" value="2500" data-min="2500"> <input name="sub_frequency" type="hidden" value="1m"> <input name="renewal_and_termination" type="hidden" value="The service continues monthly unless and until either party provides 30 days written termination notice."> <input name="code" type="hidden" value="keynote_monthly"> <input name="category" type="hidden" value="Keynote Monthly Subscription"> <input name="2:parent_code" type="hidden" value="keynote_monthly"><p>Option 2</p>
<td>
<p><span class="monthly_fee">Our KeyNote speaking engagement requires a $2,500 upfront, non-refundable services retainer.</p>
</td>
<td><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></td>
</tr>
</tbody>
</table>
</div>
<!-- .foxycart-table --> <!-- foxycart end -->
<p>&nbsp;</p>
Comments
  • fc_adamfc_adam FoxyCart Team
    @jm987,

    Thanks for posting. It looks like you've attempted to add the parent code attribute to each of the forms already, is that correct? I see each has the following input included:
    <input name="2:parent_code" type="hidden" value="keynote_monthly">
    For that specific input - you don't need to prepend it with 2: - it can just be name="parent_code" which will associate the parent code with the same product within the form.

    It also looks like the same form is included three times within the code you pasted - and each one is missing the closing tag for the form (like </form>). Is there meant to be three duplicate instances of the add to cart?
  • jm987jm987 Member
    edited June 23
    Thanks Adam!

    Yeah, there's meant to be three seperate forms. However, I'm running into a problem where the second and third forms use form 1's product, even though I've set it to use another product and have added closing tags for the forms. Can you tell what's going on?

    I want it to be set up so that the first one is a subscription, while the other two are not.

    <div class="foxycart-table">
    <table class="tftable" border="1">
    <tbody>
    <tr>
    <th>
    <p><strong>Package&nbsp;<br></strong></p>
    </th>
    <th>
    <p><strong>Description</strong></p>
    </th>
    <th>
    <p><strong>Price</strong></p>
    </th>
    <th>&nbsp;</th>
    </tr>
    <tr>
    <td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"><input name="name" type="hidden" value="Option 1"> <input name="price" type="hidden" value="2500" data-min="2500"> <input name="sub_frequency" type="hidden" value="1m"> <input name="renewal_and_termination" type="hidden" value="The service continues monthly unless and until either party provides 30 days written termination notice."> <input name="code" type="hidden" value="keynote_monthly"> <input name="category" type="hidden" value="Keynote Monthly Subscription"> <input name="2:parent_code" type="hidden" value="keynote_monthly"><p>Option 1</p>
    <td>
    <p><span class="monthly_fee">Proactive pitching to secure up to one local, community,</br> national or industry speaking engagement per month.</p>
    </td>
    <td>
    <p><span class="monthly_fee">$2,500 per month</p>
    </td>
    <td><center><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></center></td>
    </form>
    </tr>
    <tr>
    <td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"><input name="name" type="hidden" value="Cool"> <input name="price" type="hidden" value="2500" data-min="2500"> <input name="code" type="hidden" value="Keynote option 2"> <input name="category" type="hidden" value="Keynote option 2"> <input name="2:parent_code" type="hidden" value="Keynote_option_2"><p>Option 2</p>
    <td>
    <p><span class="monthly_fee">The main package. We will (insert stuff here).</p>
    </td>
    <td>
    <p><span class="monthly_fee">$2,500 upfront</p>
    </td>
    <td><center><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></center></td>
    </form>
    </tr>
    <tr>
    <td><form id="foxyform" accept-charset="utf-8" action="https://axiapr.foxycart.com/cart" method="post"> <input name="price" type="hidden" value="5000" data-min="5000"> <input name="code" type="hidden" value="Keynote_highlight_reel"> <input name="2:child_code" type="hidden" value="Keynote_highlight_reel"><p>Optional add-on:</br> Speaker's highlight reel</p>
    <td>
    <p><span class="monthly_fee">We highly recommend and will edit a short video</br> of highlight clips from your presentations/speeches</p>
    </td>
    <td>
    <p><span class="monthly_fee">$5,000</p>
    </td>
    <td><center><a class="cta_button submitForm foxy-button" id="cta_button_343720_973c7885-1305-4f97-8519-07e5036c6e3a" href="#"> Add to Cart </a></center></td>
    </form>
    </tr>
    </tbody>
    </table>
    </div>
  • fc_adamfc_adam FoxyCart Team
    @jm987,

    Is this page live somewhere we could take a look? From looking at your forms - it looks like they're being submitted in a non-standard way, perhaps using javascript - as the "add to cart" buttons are link elements instead of a submit input or button element. I'm wondering if the javascript that is submitting the form is only submitting the first form on the page.

    Also - within each of your forms, you won't need to prepend any of the input names with 2: - that's only required if you're adding multiple products within a single form to separate the respective product details.
  • jm987jm987 Member
    Hey Adam,

    I've got this done, but I'd like to ask another question. can I set it up so that a child product requires one out of multiple products to be in your cart in order to be purchased?

    In my example, I want it so that Item A can only be purchased if you have EITHER Product B OR Product C in your cart. Is that possible with Foxycart?
  • fc_adamfc_adam FoxyCart Team
    @jm987,

    With our bundled products - children products can only be added to the cart if their parent product is in the cart. You could set both of these products to have the same code, and the child product to have that code set as it's parent_code attribute. If both parents are present in the cart when the child is added, it will added to the last parent that was added. You can see more details on product bundling here: https://wiki.foxycart.com/v/2.0/products#product_grouping_bundling_ensembles

    If you can't (or don't want to) have the parent products share a code - you could also add some custom javascript to your website to run some validations before adding products to the cart. Within that code, you could check if the child product is being attempted to be added, and if it is, check if either of the parents are already in the cart. If they aren't, you can stop the add to cart from happening (and display an error to the customer), otherwise just letting the add to cart succeed. If you'd like to take that approach, we can provide some extra details on that.
  • jm987jm987 Member
    That works fine! Thanks!
Sign In or Register to comment.