Silkograph
Silkograph

Reputation: 85

Read value of product's custom options in custom script of Opencart 2.x

Is is possible to read variable value of custom option using key value pair?

I am trying to read option value for a product in custom function.I am able to read value by referring it with array index like below

$option = array_filter($this->request->post['option']); $product_serial_no = $option['93'] ;

I get magic array index $option[93] by reading variable value in Neatbean debug mode.

enter image description here

But option index is changing for different products thus I want to read value of variable 'Serial Number' using something like associative array. Is it possible?

The options on product page look like below image.

enter image description here

Upvotes: 0

Views: 270

Answers (1)

Dmitriy Zhuk
Dmitriy Zhuk

Reputation: 767

when you are viewing the product page and say you select some of the options and click "add to cart" the post will carry this array

$_POST['option'] = array(93 => '22222') (as you have presented in your example)

the 93 is the $product_option_id and it allows you to pull all the information you need with this request:

$product_id = $this->request->post['product_id'];
foreach ($this->request->post['option']) as $product_option_id => $value) {
    $option_query = $this->db->query("SELECT 
    po.product_option_id, 
    po.option_id, 
    od.name, 
    o.type 
    FROM " . DB_PREFIX . "product_option po 
    LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) 
    LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id)
    WHERE po.product_option_id = '" . (int)$product_option_id . "' 
    AND po.product_id = '" . (int)$product_id . "' 
    AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

    print_r($option_query->row);
}

This info should be sufficient enough to do any manipulations with options.

if the option has type of select or radio you may want to query also the possible variations like this

//... put this inside the foreach loop, listed above right after print_r($option_query->row);
if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') {
    $option_value_query = $this->db->query("SELECT 
    pov.option_value_id, 
    ovd.name, 
    pov.quantity, 
    pov.subtract, 
    pov.price, 
    pov.price_prefix, 
    pov.points, 
    pov.points_prefix, 
    pov.weight, 
    pov.weight_prefix 
    FROM " . DB_PREFIX . "product_option_value pov 
    LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id =     ov.option_value_id) 
    LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) 
    WHERE pov.product_option_value_id = '" . (int)$value . "' 
    AND pov.product_option_id = '" . (int)$product_option_id . "' 
    AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

    print_r($option_value_query->row);
}

And in case you have a checkbox (like in the example above)

//...also place this in the loop foreach in the code above.
if ($option_query->row['type'] == 'checkbox' && is_array($value)){
    foreach ($value as $product_option_value_id) {
        $option_value_query = $this->db->query("SELECT 
        pov.option_value_id, 
        pov.quantity, 
        pov.subtract, 
        pov.price, 
        pov.price_prefix, 
        pov.points, 
        pov.points_prefix, 
        pov.weight, 
        pov.weight_prefix, 
        ovd.name 
        FROM " . DB_PREFIX . "product_option_value pov 
        LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id) 
        WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "' 
        AND pov.product_option_id = '" . (int)$product_option_id . "' 
        AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        print_r($option_value_query->row);
    }
}

You can view this exact code in system/library/cart/cart.php

hope this helps.

Upvotes: 1

Related Questions