Forum

Thread tagged as: Question, FoxyCart, Field-Types

Foxycart - adding product option

I understand about adding a field to product.html I have added Weight as a custom field

How do I go about getting product options to be able to modify it ? In the same way that price can be modified for a particular option.

I have added code to options.edit.pre.php and options.edit.post.php

But there seem to be a number of files which need editing, and I want to make sure I get them all.

For example, although the admin page now shows options to set the weight for each option, it doesn't seem to be saved. Or maybe saved, but not retrieved

What else needs changing ?

Thanks in advance

Note : I tried adding a modifier {w:0.1} to option description. This does get passed and understood by Foxycart, but it is not combined with the price modifier . Instead of perch passing {w:0.1|p+10} as the correct string for Foxycart, it is passed as {w:0}{p+10}. So Foxycart modifies the weight but not the price. I have tried manually adding {w:0.1|p+10} to the option name but Foxycart still does not process the price change

Tim Considine

Tim Considine 0 points

  • 5 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Hi Tim

Please post your template code.

Have you copied over the templates as described in the documentation?

Rachel

I copied a modified product.html into /perch/templates/shop I didn't yet copy the options.edit.pre.php and options.edit.post.php as I wasn't sure where to put them. But have copies of original code ! )))

I have temporarily reverted the pages to standard as it is not working.

product.html :

added <perch:input type="hidden" name="weight" value="<perch:shop id="productWeight" />" />

options.edit.pre.php :

<?php $Products = new PerchShop_Products($API); $Options = new PerchShop_Options($API);

$HTML = $API->get('HTML');
$Form = $API->get('Form');

$message = false;
$result = false;

if (isset($_GET['id']) && $_GET['id']!='') {
    $productID = (int) $_GET['id'];    
    $Product = $Products->find($productID, true);
    if(is_object($Product)) {
        // check to see if this product price or quantity is modified by the options
        $use_qty = $Product->productCountByOptions();
        $use_price = $Product->productPriceByOptions();
    }
}

if (isset($_GET['option_id']) && $_GET['option_id']!='') {
    $optionID = (int) $_GET['option_id'];    
    $Option = $Options->find($optionID, true);
    $details = $Option->to_array();
    $heading1 = 'Editing a Product Option';
    $heading2 = 'Edit Option';
} else {
    $Option = false;
    $optionID = false;
    $details = array();

    $heading1 = 'Creating a Product Option';
    $heading2 = 'Add Option';
}

$Form->require_field('optionTitle', 'Required');


if ($Form->submitted()) {
    $postvars = array('optionID','optionTitle','productID');

    $data = $Form->receive($postvars);

    if (is_object($Option)) {

        //deal with values. A new value first.

        if(isset($_POST['valueTitle'])) {

            if($use_qty==1) {
                /* ---- added by TC ---- */
                $weight = $_POST['valueWeight'];
                $count = $_POST['valueCount'];
            }else {
                $count = NULL;
            }
            if($use_price==1) {
                $price = $_POST['valuePrice'];
                $mod = $_POST['valueMod'];
            }else {
                $price = NULL;
                $mod=NULL;
            }
            $Option->add_option_value($_POST['valueTitle'],$price,$count,$mod);
        }

        //loop through options, update. If checked, delete
        $values = $Option->get_option_values();

        if(is_array($values)) {
            foreach($values as $row) {
                //are we deleting?
                if(isset($_POST['delete_'.$row['valueID']])) {
                    $Option->delete_option_value($row['valueID']);
                }

                if(isset($_POST['valueTitle_'.$row['valueID']])) {
                    $title = $_POST['valueTitle_'.$row['valueID']];
                    if($use_price==1) {
                        $price = $_POST['valuePrice_'.$row['valueID']];
                        $mod = $_POST['valueMod_'.$row['valueID']];
                    }else {
                        $price = NULL;
                        $mod=NULL;
                    }
                    if($use_qty==1) {
                        /* ---- added by TC ---- */
                        $weight = $_POST['valueWeight_'.$row['valueID']];
                        $count = $_POST['valueCount_'.$row['valueID']];
                    } else {
                        $count = NULL;
                    }
                    /* ---- modified by TC ---- */
                    $Option->update_option_value($row['valueID'],$title,$price,$weight,$count,$mod);
                }
            }
        }



        $result = $Option->update($data);
        PerchUtil::redirect($API->app_path() .'/options/edit/?id='.$productID.'&option_id='.$Option->id().'&updated=1');

    }else{

        if (isset($data['optionID'])) unset($data['optionID']);
        $new_option = $Options->create($data);

        if (is_object($new_option)) {
            PerchUtil::redirect($API->app_path() .'/options/edit/?id='.$productID.'&option_id='.$new_option->id().'&created=1');
        }else{
            $message = $HTML->failure_message('Sorry, that option could not be created.');
        }
    }

    if ($result) {
        $message = $HTML->success_message('Your option has been successfully updated. Return to %soption listing%s', '<a href="'.$API->app_path() .'">', '</a>');  
    }else{
        $message = $HTML->failure_message('Sorry, that option could not be updated.');
    }

    if (is_object($Option)) {
        $details = $Option->to_array();
    }else{
        $details = array();
    }
}

if (isset($_GET['created']) && !$message) {
    $message = $HTML->success_message('Your option has been successfully created. Return to %soption listing%s', '<a href="'.$API->app_path() .'/options">', '</a>');
}

if (isset($_GET['updated']) && !$message) {
    $message = $HTML->success_message('Your option has been successfully updated. Return to %soption listing%s', '<a href="'.$API->app_path() .'/options">', '</a>'); 
}

?>

options.edit.post.php :

<?php

# Side panel
echo $HTML->side_panel_start();
echo $HTML->para('This page lists the options for product.');




echo $HTML->side_panel_end();


# Main panel
echo $HTML->main_panel_start();
include('_subnav.php');



echo $HTML->heading1('Edit the option');

$filter = 'options';

/* ----------------------------------------- SMART BAR ----------------------------------------- */
    if(is_object($Product)) {
    ?>

    <ul class="smartbar">
        <li>
            <span class="set">
            <a class="sub" href="<?php echo $API->app_path(); ?>">Products</a> 
            <span class="sep icon"></span> 
            <a href="<?php echo $API->app_path() . '/edit/?id='.PerchUtil::html($Product->id());?>"><?php echo PerchUtil::html($Product->productTitle()); ?></a>
            </span>
        </li>
        <li class="selected"><a href="<?php echo $HTML->encode($API->app_path().'/options/?id='.$Product->id()); ?>"><?php echo $Lang->get('Product Options'); ?></a></li> 
    </ul>

    <?php
    }
    /* ----------------------------------------- /SMART BAR ----------------------------------------- */



echo $HTML->heading2('Option details');

echo $Form->form_start('content-edit', 'magnetic-save-bar');

echo $Form->text_field('optionTitle', 'Title', isset($details['optionTitle'])?$details['optionTitle']:false);    

//values?
if(is_object($Option)) {

    echo $HTML->heading2('Option values');        

    echo '<div class="helptext">';
    echo $HTML->para('The values entered here are the options the customer can pick from on the product page. For example an option of "Size" might have the values S, M, L, XL. The value the customer picks will be passed to FoxyCart.');
    echo '</div>';

    $values = $Option->get_option_values();

    $value_opts = array();
    $value_opts[] = array('label'=>'+','value'=>'+');
    $value_opts[] = array('label'=>'-','value'=>'-');


    echo '<table>';
    echo '<thead><tr>';
    echo '<th>'.$Lang->get('Option value').'</th>';
    if ($use_price==1)  echo '<th>'.$Lang->get('Price adjustment').'</th>';
    /* ---- added by TC ---- */
    if ($use_price==1)  echo '<th>'.$Lang->get('Weight adjustment').'</th>';
    if ($use_qty==1)    echo '<th>'.$Lang->get('Quantity').'</th>';
    echo '<th class="action">'.$Lang->get('Delete').'</th>';
    echo '</tr></thead><tbody>';

    if(is_array($values)) {
        foreach($values as $row) {
        ?>
        <tr>
            <?php 

            echo '<td>' .$Form->text('valueTitle_'.$row['valueID'], $Form->get_value('valueTitle_'.$row['valueID'], $row['valueTitle'])) .'</td>';

            if ($use_price==1) {  
                echo '<td>';
                echo $Form->select('valueMod_'.$row['valueID'], $value_opts, $Form->get_value('valueMod_'.$row['valueID'], $row['valueMod']));
                echo $Form->text('valuePrice_'.$row['valueID'], $Form->get_value('valuePrice_'.$row['valueID'], $row['valuePrice']), 's');
                echo '</td>';
            } 

            if ($use_qty==1) { 
                /* ---- added by TC ---- */
                echo '<td>' . $Form->text('valueWeight_'.$row['valueID'], $Form->get_value('valueWeight_'.$row['valueID'], $row['valueWeight']), 's'). '</td>'; 
                echo '<td>' . $Form->text('valueCount_'.$row['valueID'], $Form->get_value('valueCount_'.$row['valueID'], $row['valueCount']), 's'). '</td>'; 
            } 
            ?>
            <td><?php echo $Form->checkbox('delete_'.$row['valueID'], "true", false); ?></td>
        </tr>
        <?php 
        }
    }

    echo '<tr>';


    echo '<td>' .$Form->text('valueTitle', $Form->get_value('valueTitle', '')) .'</td>';

    if ($use_price==1) { 
        echo '<td>';
        echo $Form->select('valueMod', $value_opts, $Form->get_value('valueMod', ''));
        echo $Form->text('valuePrice', $Form->get_value('valuePrice', '0.00'), 's');
        echo '</td>';
    }

    if ($use_qty==1) { 
        echo '<td>' . $Form->text('valueWeight', $Form->get_value('valueWeight', ''), 's'). '</td>'; 
        echo '<td>' . $Form->text('valueCount', $Form->get_value('valueCount', ''), 's'). '</td>'; 
    } 

    echo '<td></td>'; // delete col

    echo '</tr>';



    echo '</tbody></table>';

}


echo $Form->hidden('optionID', isset($details['optionID'])?$details['optionID']:false);
echo $Form->hidden('productID', $productID);
echo $Form->submit_field('btnSubmit', 'Save', $API->app_path());

echo $Form->form_end();


echo $HTML->main_panel_end();

?>

Drew McLellan

Drew McLellan 2638 points
Perch Support

We can't support your modification of our add-on, I'm afraid. If you're going to change the files we're no longer in a position where we can usefully help.

Thanks Drew

I don't want to change the files. I want to know how I can legitimately support adding a product option.
You explicitly support adding custom fields into the templates.

All am I asking is how I can add a product option

Drew McLellan

Drew McLellan 2638 points
Perch Support

I have added code to options.edit.pre.php and options.edit.post.php

This is not how you do it.

Is this a new implementation of FoxyCart? The add-on is about to be deprecated and then discontinued in favour of Perch Shop.

So perhaps you could advise how to do it?

Drew McLellan said:

Is this a new implementation of FoxyCart?

No, I have been using it successfully for couple of years, now I need to extend its use

Drew McLellan said:

The add-on is about to be deprecated and then discontinued in favour of Perch Shop.

No news anywhere on your site that it is about to be deprecated.

The Perch Shop which is not even released yet ? I've been watching for its release but it's been on pre-release application-only basis for yonks. How do we have confidence in it ?

As mentioned earlier, I have put back all code.

There is no indication anywhere (e.g. a readme file in relevant directories) about what should not be touched. However there is plenty of encouragement to create customisations. So forgive me if I explored too far.

All I am asking is how to go about adding product options for additional fields. I am not asking you to do it for me, I'm just asking for guidance on how to do it.

Please let me know. Thank you

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Please post your Diagnostics Report. You would have been asked for this when creating the thread.

Diagnostics report
HEALTH CHECK

Perch is up to date
PHP 5.5.20 is up to date
MySQL 5.6.28-log is up to date
Image processing available
SUMMARY INFORMATION

Perch: 2.8.23, PHP: 5.5.20, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $, with PDO
Server OS: Linux, cgi-fcgi
Installed apps: content (2.8.23), assets (2.8.23), categories (2.8.23), perch_blog (4.6), perch_events (1.9.2), perch_forms (1.8.3), perch_shop_foxycart (2.1), perch_members (1.2), perch_podcasts (1.0), perch_backup (1.2), perch_twitter (3.2)
App runtimes: <?php include(PERCH_PATH.'/core/apps/content/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_blog/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_shop_foxycart/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_forms/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_events/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_members/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_twitter/runtime.php'); include(PERCH_PATH.'/addons/apps/perch_podcasts/runtime.php'); ?>
PERCH_LOGINPATH: /perch
PERCH_PATH: /home/saladsem/public_html/perch
PERCH_CORE: /home/saladsem/public_html/perch/core
PERCH_RESFILEPATH: /home/saladsem/public_html/perch/resources
Image manipulation: GD Imagick
PHP limits: Max upload 128M, Max POST 8M, Memory: 256M, Total max file upload: 8M
F1: dc1fef2ad0fcd9f943c02ebb43d85dbc
Resource folder writeable: Yes
DOCUMENT_ROOT: /home/saladsem/public_html
HTTP_HOST: saladseminars.com
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think to have different weights for each variant you'll need to add them each as a product - I don't think there's an option for it otherwise.

Thank you Drew