我正在使用prestashop 1.6,并且使用webservice(api)和prest5ashop进行了完全正常工作的设置,但我对购物车添加机制存在轻微问题。
基本上我去创建我的购物车,但是当我执行每个后续项目时,我在(cart_rows)下添加(cart_row)似乎格式不正确,因此无法添加。
我的代码是:
import json
import os
FILENAME = "./f.json"
# init the data file
def init_data():
with open(FILENAME, "wb") as f:
json.dump({}, f)
def load_content():
with open(FILENAME) as f:
infos = json.load(f)
return infos
def save_content(content):
with open(FILENAME, "w+") as f:
json.dump(content, f)
return True
def save_info(username, password):
infos = load_content()
if username in infos:
return False
infos[username] = password
save_content(infos)
return True
def sign_in(username, password,):
status = save_info(username, password)
if not status:
print "username exists"
def login(username, password):
infos = load_content()
if username in infos:
if password == infos[username]:
print "login success"
return True
else:
print "password wrong"
return False
else:
print "no user named %s" %username
if __name__ == "__main__":
# here is some simple test
os.system("rm -f %s" %FILENAME)
if not os.path.exists(FILENAME):
init_data()
# login fail
login("hello","world")
# sign_in
sign_in("hello", "world")
# login success
login("hello","world")
# sign_in fail
sign_in("hello", "world")
# login fail
login("hello", "hello")
所以基本上在上面的例子中,产品1被添加但2和3没有。当我查看Prestashop的XML响应时,我注意到了这一点:
$product_list = array(
"1" => array("id_product" => "219", "quantity" => "1"),
"2" => array("id_product" => "219", "quantity" => "1"),
"3" => array("id_product" => "219", "quantity" => "3")
);
$i = 0;
foreach ($product_list as $product) {
$xml->cart->associations->cart_rows->cart_row[$i]->id_product = $product['id_product'];
$xml->cart->associations->cart_rows->cart_row[$i]->quantity = $product['quantity'];
$i++;
}
$opt = array('resource' => 'carts');
$opt['postXml'] = $xml->asXML();
echo '<pre>'; print_r($opt); echo '</pre>';
$xml = $webService->add($opt);
$id['cart'] = $xml->cart->id; // ID of created cart
我可以看到添加的第二和第三个产品看起来格格不入,但我不明白为什么。有人可以帮忙吗?
谢谢!
答案 0 :(得分:1)
为什么id产品相同?多个购物车行中不接受具有不同数量的相同ID产品。它应该在发送之前添加。请使用不同的id产品并分享结果。