Post by ITF » Tue Jan 26, 2016 6:24 am

I'm making a simple mod using VQMOD method. Mostly dealing with a text input from options. I wish to display a json error if the text input value is not what I want when "Add to cart" button is pressed.

So far, I've got the error message working, but I am having trouble knowing how to get the value from text input. In the beginning I thought $product_option['option_id'] would be grabbing the value, but it's only getting the option_id from database. I've also tried $_POST["option[$product_option['product_option_id']"] which is from the input form name="option[227]".. but the $_POST[] doesn't work either.

in my code, my option_id 13 is a text input. I only want this to have the check, not other text input options.

Does anyone know how to get the value or what code to add for that to be possible?

Code: Select all

<file name="catalog/controller/checkout/cart.php">
    <operation>
        <search position="after"><![CDATA[
        foreach ($product_options as $product_option) {
        ]]></search>
        <add><![CDATA[

        if ($product_option['option_id'] == 13 && [text input value] != $product_info['sku']) {
                        $json['error']['option'][$product_option['product_option_id']] = 'Sorry, Unacceptable input';
                    }
        ]]></add>
    </operation>
</file>
Thanks!
Last edited by ITF on Wed Jan 27, 2016 1:38 am, edited 2 times in total.

ITF
New member

Posts

Joined
Tue Apr 20, 2010 7:09 am

Post by vcabreu.py » Tue Jan 26, 2016 6:35 pm

Ello, mate
Try to use $_POST['option'][$product_option]['product_option_id'] instead $_POST["option[$product_option['product_option_id']"].


:joker:

User avatar
Newbie

Posts

Joined
Thu Jan 14, 2016 12:30 am
Location - Americana | São Paulo | Brazil

Post by ITF » Wed Jan 27, 2016 12:00 am

vcabreu.py wrote:Ello, mate
Try to use $_POST['option'][$product_option]['product_option_id'] instead $_POST["option[$product_option['product_option_id']"].


:joker:
Hey vcabreu.py, thanks for a reply! :)

So, I tried it but nothing shows.
Usually if the text input is left empty, an auto "required" error is displayed, but after using that suggested method, neither the default required error nor the custom error shows.

Any other methods? I'm open to trying it! ;D

ITF
New member

Posts

Joined
Tue Apr 20, 2010 7:09 am

Post by Qphoria » Wed Jan 27, 2016 12:36 am

Nah that won't do it..

You'll need to dump $option before
foreach ($product_options as $product_option) {

so you can see the structure

you can write it to a file

Code: Select all

file_put_contents(DIR_LOGS . 'options.txt', print_r($option,1));
Then you can see exactly how the option array is structured and validate there.
Here's an example of the post data right as it is set to $option

Code: Select all

$option
: array = 
  218: string = "5"
  223: array = 
    0: string = "8"
    1: string = "10"
  208: string = "test"
  217: string = "4"
  209: string = "this is my textarea"

Code: Select all

foreach ($product_options as $product_option) {
	if ($product_option['type'] == 'text') {
		if ($option[$product_option['product_option_id']] == 'test') {
			$json['error']['option'][$product_option['product_option_id']] = 'No default text';
		}
	}
}
I do the same thing in a vQmod script here:
viewtopic.php?f=131&t=110581

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by Qphoria » Wed Jan 27, 2016 12:58 am

See updates

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by ITF » Wed Jan 27, 2016 1:22 am

Qphoria wrote:Nah that won't do it..

You'll need to dump $option before
foreach ($product_options as $product_option) {

so you can see the structure

you can write it to a file

Code: Select all

file_put_contents(DIR_LOGS . 'options.txt', print_r($option,1));
Then you can see exactly how the option array is structured and validate there.
Here's an example of the post data right as it is set to $option

Code: Select all

$option
: array = 
  218: string = "5"
  223: array = 
    0: string = "8"
    1: string = "10"
  208: string = "test"
  217: string = "4"
  209: string = "this is my textarea"

Code: Select all

foreach ($product_options as $product_option) {
	if ($product_option['type'] == 'text') {
		if ($option[$product_option['product_option_id']] == 'test') {
			$json['error']['option'][$product_option['product_option_id']] = 'No default text';
		}
	}
}
I do the same thing in a vQmod script here:
viewtopic.php?f=131&t=110581
Thanks Qphoria! That absolutely solved the issue.

Even though you've solved the issue, I'd like to learn from it. I simply don't understand enough to think of using

Code: Select all

$option[$product_option['product_option_id']]
for the past day. I had tried many different methods.
Do you recommend any good tutorials to better grasp this? I've tried w3schools and some others but they're simple examples. I've been playing with OC on and off for a long time and still struggling to understand php and arrays properly. Thank you! :)

EDIT: Added "SOLVED" to topic. Thanks.

ITF
New member

Posts

Joined
Tue Apr 20, 2010 7:09 am

Post by Qphoria » Fri Jan 29, 2016 2:21 pm

It's really just a multidimensional array.
Like any other language

Code: Select all

$arr1 = array(
    'one' => 'apple',
    'two' => 'banana'
    'three' => array(
        'sub1' => 'orange',
        'sub2' => 'pear'
        'sub3' => array(
            'buzz' => 'grape',
            'fuzz' => 'lemon'
        )
    )
);
Or more easily read

Code: Select all

echo $arr1['one']; // "apple"
echo $arr1['two']; // "banana"
echo $arr1['three']['sub3']['fuzz']; // "lemon"

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by ITF » Sat Jan 30, 2016 1:59 am

PHP is my first real language and learning as a hobby. I started with html and others told me I needed to learn something better. ;D
Thanks for the example. It always helps. :)
Qphoria wrote:It's really just a multidimensional array.
Like any other language

Code: Select all

$arr1 = array(
    'one' => 'apple',
    'two' => 'banana'
    'three' => array(
        'sub1' => 'orange',
        'sub2' => 'pear'
        'sub3' => array(
            'buzz' => 'grape',
            'fuzz' => 'lemon'
        )
    )
);
Or more easily read

Code: Select all

echo $arr1['one']; // "apple"
echo $arr1['two']; // "banana"
echo $arr1['three']['sub3']['fuzz']; // "lemon"

ITF
New member

Posts

Joined
Tue Apr 20, 2010 7:09 am

Post by Qphoria » Sat Jan 30, 2016 3:35 am

I should actually clarify.. the original question regarding this:

Code: Select all

$option[$product_option['product_option_id']]
actually isn't a multidimensional array.. it's actually just a single dimensional array loading data from another single dimensional array

$arr1[14] = 'red';
$arr1[15] = 'green';
$arr1[16] = 'blue';

$arr2['option_id'] = 15
So you simply substitute the second array into the the first array thus making it dynamic:
echo $arr1[$arr2['option_id']]
which is the same as
echo $arr1[15]
returns 'green' since element 15 is mapped to green

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by ITF » Fri Jun 10, 2016 3:54 am

Hi Qphoria!

Just wanted to pop in and thank you for your clarity. After your previous posts, it helped me a lot. I started tinkering with OC. Life got in the way and I haven't been able to really learn more until recently.
Qphoria wrote:I should actually clarify.. the original question regarding this:

Code: Select all

$option[$product_option['product_option_id']]
actually isn't a multidimensional array.. it's actually just a single dimensional array loading data from another single dimensional array

$arr1[14] = 'red';
$arr1[15] = 'green';
$arr1[16] = 'blue';

$arr2['option_id'] = 15
So you simply substitute the second array into the the first array thus making it dynamic:
echo $arr1[$arr2['option_id']]
which is the same as
echo $arr1[15]
returns 'green' since element 15 is mapped to green

ITF
New member

Posts

Joined
Tue Apr 20, 2010 7:09 am
Who is online

Users browsing this forum: Roger Wendover and 32 guests