Guide: persParam with oxcmp_basket->addToBasket function

Hello everyone,

I just registered to write a guide on how to get persParams working when you have to use the oxcmp_basket->addToBasket function, because I encountered several problems while solving this task.
In my case I needed to add several customized articles in one step.

My first attempt was to simply use a string as the aPersParam argument, but that had to fail because I missed that it should be an array.
I discovered that it has to be an array by inspecting the database entries made by the default methods (eg. by simply putting in a customized text in details view).
I saw that it had to look something like this:
a:1:{s:7:“details”;s:x:“customized text”}

where x is the length of the customized text.

By trying different arrays in my code I recognized, that the >s:7:“details”< part is the index of the customized text. So I created an array like:
$aPersParam = array();
$aPersParam[‘details’] = $customText;

but that ended up with an empty persParam, even in the database.

By chance I found out, that when I put something in the array beforehand it works:
$aPersParam = array();
$aPersParam[0] = ‘someThing’;
$aPersParam[‘details’] = $customText;

So thats my solution for this. I think it’s very odd, that the first part of the array seems to be ignored. Maybe someone knows what it’s used for?
I also want to note that you can add multiple personalized texts in the array with custom indices but the ‘details’ index has to be set, otherwise oxid won’t recognize that there is a valid persParam.

I hope this is of use for someone else because it took me many frustrating hours to figure this out.

Julian

Thanks a lot Julian, here is my solution for this - but you found the path in the jungle:-)

oxcmp_basket.php

// collecting input
// JZL changed
// $aPersParam = array();
// $aPersParam[0] = isset( $aProductInfo[‘persparam’] )?$aProductInfo[‘persparam’]:null;

Thanks, Jürgen

Nice surprise about this shorter form. Can you tell us what it is you are talking/writing about?
Has any info. about this shorter form been posted before (and i just missed it)?

Hello,

I extend the class ‘oxcmp_basket’ by adding a module ‘HDi2Basket’.
I add multiple items into a cart, each item packs with label, some text. But i didn’t turn on the option ‘Product can be customised []’ at the backend.

Here is the code:


foreach(your condition){
 unset($persparam);
 $persparam = array("details" => "your text here"); 
				
 //add products to basket
 parent::tobasket($id,$am, NULL, $persparam);
}

I have to unset the array $persparam at the beginning since one item/article to be added to the cart has one label. So each loop, if i don’t unset the array, the array will contains a lot of label messages.

But the label added by this trick, it will not show in a cart under the article info, since i don’t turn on the option at the back-end. I poorly have to edit the .tpl.
Please notice the comments: [{* else section to display added label *}]


basketcontents.tpl
 [{if !$editable }]
                                <p class="persparamBox">
                                    [{foreach key=sVar from=$basketitem->getPersParams() item=aParam name=persparams }]
                                        [{if !$smarty.foreach.persparams.first}]<br />[{/if}]
                                        <strong>
                                            [{if $smarty.foreach.persparams.first && $smarty.foreach.persparams.last}]
                                                [{ oxmultilang ident="LABEL" }]
                                            [{else}]
                                                [{ $sVar }] :
                                            [{/if}]
                                        </strong> [{ $aParam }]
                                    [{/foreach}]
                                </p>
                            [{else}]
                                [{if $basketproduct->oxarticles__oxisconfigurable->value}]
                                    [{if $basketitem->getPersParams()}]
                                        <br />
                                        [{foreach key=sVar from=$basketitem->getPersParams() item=aParam name=persparams }]
                                            <p>
                                                <label class="persParamLabel">
                                                    [{if $smarty.foreach.persparams.first && $smarty.foreach.persparams.last}]
                                                        [{ oxmultilang ident="LABEL" }]
                                                    [{else}]
                                                        [{ $sVar }]:
                                                    [{/if}]
                                                </label>
                                                <input class="textbox persParam" type="text" name="aproducts[[{ $basketindex }]][persparam][[{ $sVar }]]" value="[{ $aParam }]">
                                            </p>
                                        [{/foreach }]
                                    [{else}]
                                         <p>[{ oxmultilang ident="LABEL" }] <input class="textbox persParam" type="text" name="aproducts[[{ $basketindex }]][persparam][details]" value=""></p>
                                    [{/if}]
				                  [{* else section to display added label *}]
				                  [{else}]
				                 <p class="persparamBox">
                                    [{foreach key=sVar from=$basketitem->getPersParams() item=aParam name=persparams }]
                                        [{if !$smarty.foreach.persparams.first}]<br />[{/if}]
                                        <strong>
                                            [{if $smarty.foreach.persparams.first && $smarty.foreach.persparams.last}]
                                                [{ oxmultilang ident="LABEL" }]
                                            [{else}]
                                                [{ $sVar }] :
                                            [{/if}]
                                        </strong> [{ $aParam }]
                                    [{/foreach}]
								</p>
                   [{* End else section to display added label *}]	
                                [{/if}]
                            [{/if}]

Hope this trick might be useful.
Thank you.