Jump to content

[Solved] Need to add customer data on product page


Rhapsody

Recommended Posts

I have modified the ps_customer table to include some custom fields that are captured during customer registration. Here are some things I would like to do using this custom data:

 

1. Create a custom URL in the product description....

One custom field in the ps_customer table is named ussailnum and contains a text string.

 

I would like to embed this field as part of a URL that is displayed on the product page. When I create the product description in the BO, the url entered would be similar to the following where ##ussailingnum## in the URL would pick up the value of the custom field if the person was logged in, and be blank if the person was not:

 

http://www1.ussailin...d=##ussailnum##

 

I know that the product.tpl file in my custom theme needs to be modified and I assume an override for ProductController.php

 

2. Pull data from ps_customer table to populate custom text fields....

In some products I include customization field text boxes to collect data that also may already be present as data fields in the ps_customer table. This data may be different so the customer should be able to type over and replace it in the customization text boxes, if a customer is logged in. This would be saved with the product ordered and have no effect on updates to the ps_customer table.

 

If this is too difficult, at a minimum I would like to display certain customer data if the customer was logged in. Similar to the URL approach above, the product description would display typed text, and pull the data from the ps_customer table if the person was logged in, otherwise it would only display the text with blanks for the data since the customer was not logged in.

 

For an extension to this approach, was thinking that if the customer was logged in, the entire section between a specified tag in the product description could be displayed with the pulled data. Otherwise, the section between the tags would be ignored and not displayed. This would be something like the <custom_display> tag entered in the product description to determine if the text between the tags was displayed.

 

<custom_display>

Skipper's First & Last Name: ##firstname## ##lastname##

Boat Name: ##boat##

</custom_display>

 

Can this be done?

 

Edit: I'm using Prestashop 1.4.9.0

Edited by Rhapsody (see edit history)
Link to comment
Share on other sites

hi there, how have you been.

 

For item 1: This should be feasible with just an override to the Product Controller. I don't think you would even need to change the template. Just have the controller perform a string replace on the product description.

 

For item 2: I quickly looked at the controller and template and the theme will display the value if it exists. So in the controller before placing the "customizationFields" into smarty, we would just inject the customer information as the default value. The customer could change it if they wish, or leave it alone. But in either case, the customer would have to click the save button, unless you have already applied the customization so this information is saved with the add to cart action.

 

I think the challenge for the customizationFields is mapping the information in the customer table to the custom field. How will you correlate them?

Link to comment
Share on other sites

bellini13,

 

Great to hear from you again. Thanks for the tips. I'll play with the first item over the next week and see if I can make that work. Once I master that, I'll move on to the 2nd item, I'll give some more thought on the approach for item 2. I'll post here when I have a solution that works for each item.

Link to comment
Share on other sites

Starting to work this at the simplest level first. Lets just display the new custom field ussailnum from the ps_customer table to start with. I'm not even adding the logic to check if the user is logged in yet - once I prove I can display the custom field on the product page, I'll add that.

 

I created an override file for ProductController.php with the addition of the custom variable needed to be defined as taken from the ps_customer table

<?php
/*
* 2007-2012 PrestaShop
* Rhapsody created from 1.4.9.0 of ProductController.php
*
* Includes custom fields for display on products
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2012 PrestaShop SA
*  @version  Release: $Revision: 16943 $
*  @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
class ProductController extends ProductControllerCore
{
protected $product;
public $php_self = 'product.php';
protected $canonicalURL;
/** @var string ussailnum - Rhapsody add for US Sailing number*/
public  $ussailnum;

Then in the public function process () I added the following to get the ussailnum assuming the id_customer is used as the pointer in the ps_customer table. This is added after the 3 lines shown above my added code

if (Pack::isPack((int)$this->product->id) && !Pack::isInStock((int)$this->product->id))
 $this->product->quantity = 0;
$id_customer = (isset(self::$cookie->id_customer) && self::$cookie->id_customer) ? (int)(self::$cookie->id_customer) : 0;
$id_group = $id_customer ? (int)(Customer::getDefaultGroupId($id_customer)) : _PS_DEFAULT_CUSTOMER_GROUP_;
$id_country = (int)($id_customer ? Customer::getCurrentCountry($id_customer) : _PS_COUNTRY_DEFAULT_);

 // Rhapsody added get ussailing number for customer.

  $customer=$customer->id_customer;
  $ussailnum=$customer->ussailnum;

 

I stripped out all the other functions in the override except for the following two that I believe will be needed when I ultimately rewrite the ussailing number in the URL

public function textRecord(Product $product, Cart $cart)

public function formTargetFormat()

 

-----------------------------------------------------------------------------

Now in the product.tpl file I've made the following mod to display a label "US Sailing Number:" followed by the number.

 {$HOOK_PRODUCT_TAB}
</ul>
<div id="more_info_sheets" class="sheets align_justify">
{if $product->description}
 <!-- full description -->
{* Rhapsody added ussailnum here *}
  <p class="text">
<label for="ussailnum">{l s='US Sailing number: '}&nbsp </label>  
			{$ussailnum=$customer->ussailnum}
  </p>
{* Rhapsody added ussailnum above *}
 <div id="idTab1" class="rte">{$product->description}</div>

 

This displays the label at the top of the product description area, but no number (text string) pulled from ps_customer table. I am logged in the front office when testing this.

 

I am not a software [spam-filter] so probably don't have the correct syntax in either of these files. Can anyone give me pointers here?

Link to comment
Share on other sites

hi there, you have to take the value from the customer object and place it into smarty.

 

so you can actually remove the public variable you defined at the top

public  $ussailnum;

 

and then in the process function you need to load the customer object

$customer=new Customer((int)$id_customer);

 

and then place that custom variable into smarty for use in the template

self::$smarty->assign('ussailnum', $customer->ussailnum);

 

and then in the template you simply display the vaue using

{$ussailnum}

 

this assumes that you have overridden the Customer object to add that custom variable?

Link to comment
Share on other sites

bellini13 - thanks for the pointers!

 

I got the basic functionality working so it displays the US Sailing number. I will now start doing the URL and string replacement.

 

I had previously overridden the classes/customer.php to add the custom variables and have that functionality working.

Link to comment
Share on other sites

Ok - here is the code I'm trying. In the override for product.php I've placed the code below that I want to do the following:

1. Puts the ussailnum from the Customer table in smarty - I checked this and am able to display $ussailnum on the product page when I am logged in using $ussailnum and put the smarty variable in product.tpl

2. Create a new variable $descr which will be the product description that has the string replaced.

3. Perform a string replacement of ##ussailnum## in Product-description with the $ussailnum from 1 above.

 

 

   $customer=new Customer((int)$id_customer);
   self::$smarty->assign('ussailnum', $customer->ussailnum);
   $descr = str_replace('##ussailnum##', $ussailnum, $product->description);
   self::$smarty->assign('descr', $descr);

 

4. Tried to replace the display of the Product description in product.tpl with the new smarty variable $descr that should contain the modified string per #3 above.

 

This is replaced

 <div id="idTab1" class="rte">{$product->description}</div>

 

by this code in product.tpl

 <div id="idTab1" class="rte">{$descr}</div>

 

It doesn't work - nothing is displayed for $descr. Any pointers?

Link to comment
Share on other sites

this line looks wrong

$descr = str_replace('##ussailnum##', $ussailnum, $product->description);

it should probably be

$descr = str_replace('##ussailnum##', $customer->ussailnum, $product->description);

 

I would add a debug statement in the product.php that displays the value of $descr

something like this is fine and will display at the top of the page

echo "the replaced string is ".$descr."<br>";

Link to comment
Share on other sites

Thanks for the tips.

 

Just to clarify, I made the changes in ProductController.php override, not Product.php as I stated in my post #7.

 

I made the changes in ProductController and inserted 2 test strings.

 

echo "the replaced string is ".$descr."<br>";
echo "other test string is ".$ussailnum."<br>";

 

It doesn't appear that this is passing anything by the test strings. The top of the product display shows:

 

the replaced string is

other test string is

 

However if I put the {$ussailnum} in the product.tpl as a test, it passes the correct value on the product display.

Link to comment
Share on other sites

#1 from my original post is solved thanks to pointers from bellini13!

 

Here is what worked.

1. Created an overide/controllers/ProductController.php that adds the $customer->ussailnum (already defined in the override for classes/customer.php to add the custom variables). This is in the "public function process()"

// Rhapsody added customer variables to put in smarty
$customer=new Customer((int)$id_customer);
self::$smarty->assign('ussailnum', $customer->ussailnum);
// Test lines to check if variables passed to smarty are displayed at top of product
// echo "other test string is ".$customer->ussailnum."<br>";
// echo "other test string is ".$product->description."<br>";
// End of adding customer variables

 

2. Modified the product.tpl file in my custom theme to include the string replacement in the URL that is passed in the product description.

 <!-- full description -->
{* Rhapsody added ussailnum in string replacement here *}
 {if $ussailnum}
  <div id="idTab1" class="rte">{str_replace('##ussailnum##', {$ussailnum}, $product->description)}</div>
 {else}
 <div id="idTab1" class="rte">{str_replace('mid=##ussailnum##', {$ussailnum}, $product->description)}</div>
 {/if}
{* Rhapsody added ussailnum above *}

Edited by Rhapsody (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...