X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   It is possible? hoping that someone can help me (https://forum.x-cart.com/showthread.php?t=36373)

aquaimaging 12-26-2007 09:47 PM

It is possible? hoping that someone can help me
 
http://i91.photobucket.com/albums/k293/pindeho2005/howagain.gif

it is possible if i hit the Size 3.5 x 4 folded business card my quantity box will automatically change it to 1000?

thanks

balinor 12-27-2007 04:29 AM

Re: It is possible? hoping that someone can help me
 
You will probably need to have this custom coded.

aquaimaging 12-27-2007 03:37 PM

Re: It is possible? hoping that someone can help me
 
I dont know how to code :( can anyone help me how? pls

balinor 12-27-2007 07:25 PM

Re: It is possible? hoping that someone can help me
 
That's what I mean, you'll need to hire someone to do this for you.

levo 12-27-2007 08:01 PM

Re: It is possible? hoping that someone can help me
 
Quote:

Originally Posted by aquaimaging
http://i91.photobucket.com/albums/k293/pindeho2005/howagain.gif

it is possible if i hit the Size 3.5 x 4 folded business card my quantity box will automatically change it to 1000?

thanks


Hi. Your post reminded to look into this - although you do have the ability to write some custom javascript for a given product option group via the product admin tools, the code you write is only executed when one actually submits the form to add the product to one's cart. What would be more useful - such as in your case - is to be able to add some custom javascript that is executed by the onchange() event. This is what the stock x-cart does, actually - it calls a function called check_options().

In any case, I'm nearly done with a mod that allows you to add some custom javascript that gets called by check_options(). I just added a textarea for that code beneath the existing one on the product options admin page and added a field to the product_options_js db table to store it. It's functional now, I just need to write up the steps and will try to get that posted tonight. I had to modify a bit more than I would have liked, but I'll go ahead and post what I've got asap.

Nate

aquaimaging 12-27-2007 08:04 PM

Re: It is possible? hoping that someone can help me
 
ok thank i will wait for that..

levo 12-28-2007 09:51 PM

Re: It is possible? hoping that someone can help me
 
Hi again,

Well, here is what I have so far. I'm still considering what else might be useful along these lines, perhaps exending it to handle other events, but for now hopefully it will be of use to some folks out there.

NOTE: This has been tested only on X-Cart 4.1.9

Step 1: Alteration to DB; we need a field to store our javascript code. Use your preferred mysql client to execute the following SQL:

Code:

ALTER TABLE xcart_product_options_js ADD custom_javascript_code TEXT;

Step 2: Modify the following PHP files:

a) $xcart_dir/modules/Product_Options/product_options.php

after ::
Code:

                #
                # Update Validation script (Javascript)
                #
                if ($geid && $fields['js']) {
                        while ($pid = func_ge_each($geid, 1)) {


change the two lines in the subsequent code that begin with 'func_array2insert' such that our new js code will be properly stored in the db ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
                                func_array2insert("product_options_js", array("productid" => $pid, "javascript_code" => $js_code, "custom_javascript_code" => $custom_js_code), true);
                        }
                } else {
                        func_array2insert("product_options_js", array("productid" => $productid, "javascript_code" => $js_code, "custom_javascript_code" => $custom_js_code), true);
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


then, directly after ::
Code:

# Get the product options list
$product_options = func_get_product_classes($productid);
$product_options_ex = func_get_product_exceptions($productid);
$product_options_js = func_get_product_js_code($productid);


add the following line that retrieves our custom javascript from the db for a given product and stores it in the variable '$custom_product_options_js' ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
$custom_product_options_js = func_get_custom_product_js_code($productid);
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


then directly after the following ::
Code:

if (!empty($product_options_js))
    $smarty->assign("product_options_js", $product_options_js);


add the following to pass that variable off to our smarty object ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
if (!empty($custom_product_options_js))
    $smarty->assign("custom_product_options_js", $custom_product_options_js);
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


b) $xcart_dir/modules/Product_Options/func.php

after ::
Code:

#
# Get product JS code
#
function func_get_product_js_code($productid) {
        global $sql_tbl;

        return func_query_first_cell("SELECT javascript_code FROM $sql_tbl[product_options_js] WHERE productid = '$productid'");
}


add the following ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
function func_get_custom_product_js_code($productid) {
        global $sql_tbl;

        return func_query_first_cell("SELECT custom_javascript_code FROM $sql_tbl[product_options_js] WHERE productid = '$productid'");
}
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


c) $xcart_dir/modules/Product_Options/customer_options.php

after ::
Code:

$product_options = func_get_product_classes($productid, !empty($product_info['is_taxes']));
$product_options_ex = func_get_product_exceptions($productid);
$product_options_js = func_get_product_js_code($productid);


add the following ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
$custom_product_options_js = func_get_custom_product_js_code($productid);
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


then, after ::
Code:

$smarty->assign("err", $err);
$smarty->assign("product_options_count", is_array($product_options) ? count($product_options) : 0);
$smarty->assign("product_options_js", @trim($product_options_js));


add the following ::
Code:

/* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === */
$smarty->assign("custom_product_options_js", @trim($custom_product_options_js));
/* === END NTH PRODUCT OPTS JAVASCRIPT MOD === */


Step 3: Modify and/or create the following Smarty templates:

a) Create the file $xcart_dir/skin1/modules/Product_Options/custom_check_options_js.tpl ::

Code:

{* $Id: custom_check_options_js.tpl,v 1.0.0 2007/12/28 04:14:37 nth Exp $ *}
{* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === *}
<script type="text/javascript" language="JavaScript 1.2">
<!--
function custom_check_options() {ldelim}

    {if $active_modules.Product_Options ne '' && $product_options ne ''}
        {if $custom_product_options_js ne ''}
        {$custom_product_options_js}
        {/if}
    {/if}

    return true;
{rdelim}
-->
</script>
{* === END NTH PRODUCT OPTS JAVASCRIPT MOD === *}


b) $xcart_dir/skin1/modules/Product_Options/product_options.tpl

after ::
Code:

<table cellspacing="0" cellpadding="0" width="100%">
{if $geid ne ''}
<tr>
    <td width="15" class="TableSubHead"><img src="{$ImagesDir}/spacer.gif" width="15" height="1" border="0" /></td>
    <td class="TableSubHead"><b>* {$lng.lbl_note}:</b> {$lng.txt_edit_product_group}</td>
</tr>
{/if}
<tr>
        {if $geid ne ''}<td width="15" class="TableSubHead"><input type="checkbox" value="Y" name="fields[js]" /></td>{/if}
        <td><textarea name="js_code" cols="60" rows="15">{$product_options_js}</textarea></td>
</tr>
<tr>
        {if $geid ne ''}<td width="15" class="TableSubHead">&nbsp;</td>{/if}
        <td>&nbsp;</td>
</tr>


add the following ::
Code:

{* === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === *}
<tr>
        {if $geid ne ''}<td width="15" class="TableSubHead">&nbsp;</td>{/if}
        <td>{$lng.txt_custom_product_options_js_note}</td>
</tr>
<tr>
        {if $geid ne ''}<td width="15" class="TableSubHead"><input type="checkbox" value="Y" name="fields[js]" /></td>{/if}
        <td><textarea name="custom_js_code" cols="60" rows="15">{$custom_product_options_js}</textarea></td>
</tr>
<tr>
        {if $geid ne ''}<td width="15" class="TableSubHead">&nbsp;</td>{/if}
        <td>&nbsp;</td>
</tr>
{* === END NTH PRODUCT OPTS JAVASCRIPT MOD === *}


c) $xcart_dir/skin1/customer/main/product.tpl

near the top of the file, directly after ::
Code:

{include file="form_validation_js.tpl"}

add the following ::
Code:

{* ==== BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD === *}
{include file="modules/Product_Options/custom_check_options_js.tpl"}
{* ==== END NTH PRODUCT OPTS JAVASCRIPT MOD === *}


Step 4: Modify the following Javascript file:

a) $xcart_dir/skin1/modules/Product_Options/func.js

after ::
Code:

/*
        Rebuild page if some options is changed
*/
function check_options() {


add the following ::
Code:

        // === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD ===
        custom_check_options();
        // === BEGIN NTH PRODUCT OPTS JAVASCRIPT MOD ===


Step 5: Add the following language variable - 'txt_custom_product_options_js_note' with the value:

Code:

Below you can write a client-side Javascript code that will be launched when a customer decides to add a product to cart and starts selecting options with which the product should be added. This script will execute when any options' onchange() event handler is triggered, rather than when the onsubmit() handler is triggered, as with the custom javascript textarea above. You need to have skills in Javascript programming to avoid errors on the customer frontend.

Usage: When administering a given product's options, you'll now find an additional textarea directly underneath the one used for custom form validation code on the customer front-end product detail page. The same notes apply - you need some familiarity with javascript to do anything useful, etc... The code gets executed by the same function that the stock x-cart code uses to handle various tasks when a product options' onchange() event is triggered, i.e. check_options().

a simple example (completely contrived - your code will need to refer to your specific product option variables, etc.):

Code:

var optid = document.getElementById('po3');
if (optid.value == '11') {
    document.getElementById('product_avail').value = 5;
    alert('qty modified!');
}


I hope this is useful to someone, and suggestions/improvements are most welcome.

Regards,

Nate

levo 12-29-2007 09:32 AM

Re: It is possible? hoping that someone can help me
 
One more thing,

I did not include the code needed to include the custom javascript code in the import/export admin procedures. If there is interest, or as time permits, I'll add that and then perhaps post it to 'completed mods'. Meanwhile. let me know if there's any particular feature someone needs and I'll see if I can help.

Nate

inebriate 12-30-2007 04:18 PM

Re: It is possible? hoping that someone can help me
 
by the looks of the screen shot, you are using product.tpl...just put a if statement and in a javascript put

Code:

var selitem = document.getElementById('po'+whatevertheidforthe3.5x4selectboxis).value;
var qtybox = document.getElementById('product_avail');

qtybox.options.length = 0;
if (selitem == '3.5 x 4')

qtybox.option[0] = new Option('1000',1000); //populate the qty box with what you want

else

qtybox.option[0] = new Option('10',10);

granted, this only works on javascript users

as for the product options javascript mod, just an idea, it may be easier to just modify the form validation and putting it as an onchange to the select input...should require less modification and support importing/exporting the javascript

levo 12-30-2007 07:37 PM

Re: It is possible? hoping that someone can help me
 
Quote:

Originally Posted by inebriate
by the looks of the screen shot, you are using product.tpl...just put a if statement and in a javascript put

Code:

var selitem = document.getElementById('po'+whatevertheidforthe3.5x4selectboxis).value;
var qtybox = document.getElementById('product_avail');

qtybox.options.length = 0;
if (selitem == '3.5 x 4')

qtybox.option[0] = new Option('1000',1000); //populate the qty box with what you want

else

qtybox.option[0] = new Option('10',10);

granted, this only works on javascript users

as for the product options javascript mod, just an idea, it may be easier to just modify the form validation and putting it as an onchange to the select input...should require less modification and support importing/exporting the javascript


Good point, but if I understand you correctly you'd (at least for the specific product in question) have to choose one or the other (onsubmit or onchange), but not both. Please let me know if that's not the case.

As I mentioned, this mod did require a good bit more changes to the code than I would have liked, but I've got a number of scenarios that I need to handle where I'm going to need the flexibility it offers, and I didn't want to have to special case the form validation every time. In particular, I'm writing a Smarty plugin that hooks into the HTML_AJAX PEAR module (which I am a big fan of) and I'm experimenting a bit using the product options JS mod and the Extra Fields HTML mod that someone else contributed, which opens up a lot of possibilities, but quickly leads to the maintainability issues inherent in most (all?) template-based systems.

As much as I enjoy working with X-Cart, as others have pointed out there is one problem with mod development in particular that really needs to be addressed - the inability to produce a workable patch system for the Smarty templates. We simply have to use the de facto documentation style that amounts to writing a diff by hand - I'm not knocking it, really, it's quite usable, but time-consuming and error-prone no matter how careful one tries to be. I've seen a couple of posts lately that touch on it, and if there are any others willing to try to come up with a usable system, I'm certainly willing to devote some time and effort to the project.

Anyway, sorry to veer so far off course, but I finished the import/export part of the mod, which I've just successfully tested, so after checking it over one more time I'll amend the original product options mod post, so that should no longer be an issue. Thanks for your feedback, I really appreciate it.

Regards,

Nate


All times are GMT -8. The time now is 12:06 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.