0

I am creating an extension for Cartthrob. What I need it to do is not allow the customer to complete their order if there are a certain number of certain products in the cart (there are three products that they can only buy a total of 8 across all three, but two products that they can buy any number of).

I have the extension set up on the cartthrob_update_cart_end to check the items currently in the cart. I was hoping to set a variable (global?) that I could use later on in the checkout template in a conditional. Setting a global variable like this DID NOT work for me:

$this->EE->config->_global_vars['test_var'] = "test var";

When I try to output the variable as {test_var}, it only outputs the variable name (it wasn't parsed). This would most likely be a parse issue, but since this is an extension I thought parsing wouldn't matter.

Some pseudocode I was thinking:

  1. Update cart hook calls my function in ext.limit_items.php.
  2. My function counts up all the products and sets a variable "item_count_passed" to either true or false.
  3. My checkout template loads. Within the template is the following conditional: {if item_count_passed} then show checkout form, else show message.

Is this possible/is there a better way to do this? I am saying this is not specific to Cartthrob since I am just asking how to set a variable in extension code that can be used in a conditional in a template.

Thanks!

Edit: I realize that I am probably looking more for a session variable and not a global variable.

Edit #2: Just to be clear, the root question is: How can I set a variable in an extension that I am writing that would be available in a template, particularly within a conditional?

0

You should be able to use CartThrob custom data for this in your extension:

$this->EE->cartthrob->cart->set_custom_data('item_count_passed', $item_count <= 8);

Then in your template:

{exp:cartthrob:checkout_form}
{if custom_data:item_count_passed}
{if:else}
{/if}
{/exp:cartthrob:checkout_form}

You might also want to call this in the cartthrob_add_to_cart_end hook.

| improve this answer | |
  • Thanks for this tip, it didn't seem to work for me (same issue in that it was not recognizing {custom_data:item_count_passed} as an EE variable and did not parse it). I will indeed need to add this to the add_to_cart_end hook as well. Just out of curiousity, where did you find this info about custom data? I can't seem to find any documentation on developing Cartthrob add ons? Thanks! – 2bsharpdev May 28 '13 at 19:05
  • Try making it a string: $this->EE->cartthrob->cart->set_custom_data('item_count_passed', $item_count <= 8 ? '1' : '0'); – Rob Sanchez May 28 '13 at 19:14
  • Also that custom_data: tag is not a global variable and you'd need to use it inside a cartthrob tag, either checkout_form or update_cart_form or customer_info. – Rob Sanchez May 28 '13 at 19:16
  • This worked perfectly, thank you! On a side note, do you have any recommendations for documentation on building cartthrob add ons? Its not too bad to figure out on the fly, but would be nice to know that features like this exist. – 2bsharpdev May 28 '13 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.