是否可以使用键值对读取自定义选项的变量值?
我正在尝试在自定义函数中读取产品的选项值。我能够通过以下数组索引引用来读取值
import numpy as np
import pandas as pd
from scipy.optimize import minimize
from sklearn.metrics import r2_score
url = 'test_data.txt'
x = pd.read_csv(url)
e1 = x['strain'].values
sigx = x['stress'].values
e=np.array(e1)
sig=np.array(sigx)
j=[1000,1]
def sig1(e):
return j[0]*np.power(e,j[1])
def diff(e):
return sig1(e)-sig
def sqerr(e):
return np.power(diff(e),2)
res= minimize(sqerr,j,)
print(res)
我通过在Neatbean调试模式下读取变量值来获得魔术数组索引ALPHABET = ("a".."z").to_a.join
def encoder(str)
str.tr(ALPHABET, ALPHABET.reverse)
end
。
但是不同产品的选项索引有所不同,因此我想使用类似关联数组的方式读取变量“序列号”的值。 有可能吗?
产品页面上的选项如下图所示。
答案 0 :(得分:1)
当您查看产品页面并说选择某些选项并单击“添加到购物车”时,帖子将带有此数组
$ _ POST ['option'] = array(93 =>'22222')(如示例中所示)
93是$product_option_id
,它允许您提取此请求所需的所有信息:
$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);
}
此信息应足以进行任何带有选项的操作。
如果该选项的类型为select
或radio
,您可能还希望查询类似的可能变化
//... 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);
}
如果您有一个复选框(如上例所示)
//...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);
}
}
您可以在system/library/cart/cart.php
希望这会有所帮助。