Add to cart link and custom product fields.

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 4 months ago #16216

In the product screen there is the Add to Cart button which gives displays the following code:

<a class="hikashop_html_add_to_cart_link" href="http://myshop.com/index.php?option=com_hikashop&ctrl=product&task=updatecart&quantity=1&checkout=1&product_id=887">
Add to cart</a>
Would it be possible to enhance this add to cart link to accept a custom parameter which then gets passed through the checkout process and stored in the cart_product and order_product tables.

The requirement is to be able to associate the ordered product with the context where the Add to Cart button was clicked. This would be used during post order processing.

The link would then look something like this:
<a class="hikashop_html_add_to_cart_link" href="http://myshop.com/index.php?option=com_hikashop&ctrl=product&task=updatecart&quantity=1&checkout=1&product_id=887&tracking_id=123456">
Add to cart</a>

The following user(s) said Thank You: pankaj7645

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
9 years 4 months ago #16220

In the Business edition of HikaShop, you have custom fields of the table "item". You could create one called tracking_id. Then you could have the URL:

http://myshop.com/index.php?option=com_hikashop&ctrl=product&task=updatecart&quantity=1&checkout=1&product_id=887&data[item][tracking_id]=123456

The system would then automatically insert the tracking_id in both tables.

Last edit: 2 years 4 months ago by nicolas.

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 4 months ago #16221

Great, thanks.

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

  • Posts: 9
  • Thank you received: 0
9 years 3 months ago #18105

Am I understanding this correctly. By adding the &[item][custom_item_value_field_name]=Test to the end of the url of the add to cart button it will add the specified value items custom field, and then add that item to the cart?

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
9 years 3 months ago #18109

It's &data[item][custom_item_value_field_name]=Test and yes.

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 3 months ago #19188

(a) Could multiple products be added from an HTML form using arrays like below?
Note that the quantity could be 0 in some cases!

<form action="http://myshop.com/index.php" method="post">
<input type="hidden" name="option" value="com_hikashop" />
<input type="hidden" name="ctrl" value="product" />
<input type="hidden" name="task" value="updatecart" />
<input type="hidden" name="product_id[]" value="887" />
<input type="hidden" name="quantity[]" value="1" />
<input type="hidden" name="data[][item][tracking_id]" value="123" />
<input type="hidden" name="product_id[]" value="887" />
<input type="hidden" name="quantity[]" value="1" />
<input type="hidden" name="data[][item][tracking_id]" value="456" />
<input type="hidden" name="product_id[]" value="887" />
<input type="hidden" name="quantity[]" value="0" />
<input type="hidden" name="data[][item][tracking_id]" value="789" />
<input type="hidden" name="product_id[]" value="887" />
<input type="hidden" name="quantity[]" value="1" />
<input type="hidden" name="data[][item][tracking_id]" value="012" />
<input type="submit" value="Submit" />
</form>

(b) Is there / could there be a Hikashop helper function so that my own code could add products to the cart?
Something like this:
$items = array();
$items['tracking_id'] = '123';
$product_id = 887;
$quantity = 1;
hikashopHelper::addToCart($product_id, $quantity, $items);

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
9 years 3 months ago #19192

a/ It's not possible. You could always add that but it's already quite complex code in the updatecart functions...

b/ Sure. You can use the function update of the file administrator/components/com_hikashop/classes/cart.php

But since the items data is taken directly from the request, you will have to set it dynamically in it... Something like that:
$data=array('item'=>$items);
JRequest::setVar('data',$data);
$class = hikashop::get('class.cart');
$class->update($product_id, $quantity);

It's not tested so you might have to adapt that a bit...

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 2 months ago #19870

Works if just one item is being added:

require_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php');
    $items = array(); 
    $items['tracking_id'] = 123; 
    $data=array('item'=>$items);
    JRequest::setVar('data',$data);
    $class = hikashop::get('class.cart');
    $class->update(23, 1);
Does not work if 2 items are being added (e.g. driven from a foreach() loop):
require_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php');
    $items = array(); 
    $items['tracking_id'] = 123; 
    $data=array('item'=>$items);
    JRequest::setVar('data',$data);
    $class = hikashop::get('class.cart');
    $class->update(23, 1);

    $items = array(); 
    $items['tracking_id'] = 456; 
    $data=array('item'=>$items);
    JRequest::setVar('data',$data);
    $class = hikashop::get('class.cart');
    $class->update(23, 1);
To get it to work with Hikashop v1.5.1 I found I had to comment out the following lines of code:
(a) classes/field.php in getInput() lines 234 and 236 (if(!isset($formData))
(b) classes/cart.php in updateEntry() line 356 $already_done2 = true;

I have not tested the rest of the shop functionality so I don't know what might get broken by these changes.

Is it possible to provide a proper long term fix for this please?
E.G. An optional force parameter passed into $class->update() which achieves the same result.

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
9 years 2 months ago #19873

We tested that and indeed that's the correct modifications to do in order to override the system. That's a good idea as I'm sure other developers will want to use it. We'll add a new force parameter to the update function for next release:
function update($product_id,$quantity=1,$add=0,$type='product',$resetCartWhenUpdate=true,$force=false){

We'll keep it to false by default in order to not alter anything else in HikaShop as the statics in place currently are used to avoid inserting custom item fields data to the options of a product.

The following user(s) said Thank You: mumu11

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 2 months ago #19874

Great, thanks.

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 2 months ago #19923

Above code populates my custom tracking_id column in the jos_hikashop_cart_product table.

However, when I create/complete the order it does not get copied through to the corresponding column in the jos_hikashop_order_product table

As a result the onAfterOrderUpdate() and onAfterOrderCreate() triggers in my plug-in cannot see it.

The custom item has Front-End disabled in its definition (I don't want the customer to see it at checkout).
If I change it to enabled then it works fine.

Is this a bug or design feature?
Could it be changed please so that these items allows get copied across to the jos_hikashop_order_product table.
There might also be additional issues with hiding these values in emails, invoices and customer order listings.

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
9 years 2 months ago #19928

You should keep it published on the front end and use CSS to hide it.
You should be able to hide it everywhere with CSS and/or view edition.

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

  • Posts: 281
  • Thank you received: 4
  • Hikashop Business
9 years 2 months ago #19929

Will do, thanks.

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

  • Posts: 111
  • Thank you received: 4
3 years 3 months ago #270377

How do I add multiple parameters? Would it look like this?
&data[item][custom_item_value_field_name]=Test&data[item][custom_item_value_field_name2]=Test2

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

  • Posts: 70485
  • Thank you received: 10576
  • MODERATOR
3 years 3 months ago #270382

Hi,

Yes, exactly.

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

Time to create page: 0.104 seconds
Powered by Kunena Forum