Variable for Custom Field on Checkout

  • Posts: 14
  • Thank you received: 0
11 years 2 months ago #149452

-- HikaShop version -- : 2.2.3
-- Joomla version -- : 2.5.19
-- PHP version -- : 5.3.27

Hello HikaShop,

I'm hoping that you can assist me with a problem I need to solve. I'm trying to set up HikaShop so that when a customer purchases a product they will enter in their billing info, as well as submit a number they already have. I would like that number to be used to query an SQL database table and return a code back, based on the customer provided number and the product selected.

I have created a custom field in the "order" Table with a Column Name of "serialnumber". This field shows up on the checkout page for the customer to enter in their number as I wanted it to.

I have also created the PHP code to query the SQL database. The function takes two variables, the number entered by the customer and the product purchased, and returns a key code. Could you tell me what variable holds the input given by the customer in the custom field? I also don't know how to pass the purchased product to the function as a variable or number that will reference a column in the database table, could you help me do that?

Finally, I'm not sure if I can add the PHP code to a display>view for it to work properly, or would I have to create a HikaShop plugin using something like onAfterOrderConfirm(&$order,&$methods,$method_id)?

Thank you for your help!

Please Log in or Create an account to join the conversation.

  • Posts: 83933
  • Thank you received: 13588
  • MODERATOR
11 years 2 months ago #149456

Hi,

The custom order fields data before the order creation is stored in the user session.
You can retrieve that data like that:
$app = JFactory::getApplication();
$data = $app->getUserState( 'com_hikashop.checkout_fields');
var_dump($data);

To load the products in the cart you could use that code:
$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
echo $product->product_name;
}

You could add that in a view override or in a plugin. Which one depends on what you want to do with the returned code and that I don't know :)

The following user(s) said Thank You: acrossan

Please Log in or Create an account to join the conversation.

  • Posts: 14
  • Thank you received: 0
11 years 2 months ago #149592

Hi nicolas,

I really appreciate your help!

I'd like to display the returned code from the database table to the customer in the "order creation notification" email, or display it on screen after the purchase is complete. Could I include my PHP code in a view override for something like this? Which view would be best if I'd like the code to run after the purchase. The customer is essentially purchasing this returned code so I'd like it to be provided after the transaction.

I tried the code you provided to pull out the custom field entered by the customer but I can't seem to get it to work. Maybe I'm not putting it in the right spot. I tried putting it in end.php just to test and see if it worked, but it only returned NULL. I think I might be looking for this customer provided custom field after the order is created, since they enter it on the same page they enter their credit card information. Does this change the code for looking up what they entered in that custom field?

I also tried:
$app = JFactory::getApplication();
$fields = $app->getUserState('com_hikashop.checkout_fields');
echo $fields->serialnumber;

"serialnumber" is my custom field column name, but this didn't return anything at all.

The PHP you provided to pull the product name worked perfectly! I can pass that to the function.

Thanks again for your help.

Last edit: 11 years 2 months ago by acrossan.

Please Log in or Create an account to join the conversation.

  • Posts: 83933
  • Thank you received: 13588
  • MODERATOR
11 years 2 months ago #149753

Hi,

It depends where you put the code. The code for the custom order fields will only work before the order is created. After that, the data in the user session is wiped and then you would need to use such code to load the order and get the value of the custom order field:
$orderClass = hikashop_get('class.order');
$order = $orderClass->loadFullOrder($order_id, false, false);
echo $order->serialnumber;

If you want to add it in the order creation notification, then you could simply edit that email via the System>Emails menu and add your code in there.

Please Log in or Create an account to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum