def create(self, vals): ''' 功能:创建小店分组 :param vals: :return: ''' try: if 'wx_group_id' not in vals: wx_officialaccount_id = vals['wx_officialaccount'] wx_officialaccount = self.env['wx.officialaccount'].search([ ('id', '=', wx_officialaccount_id) ]) appid = wx_officialaccount.wx_appid appsecret = wx_officialaccount.wx_appsecret group_help = group_manager.group_manager(appid, appsecret) result = group_help.add_group(vals['wx_group_name']) if result['errcode'] == 0: vals['wx_group_id'] = result['group_id'] else: _logger.info("创建微信小店分组错误:%s" % result['errmsg']) #add self return super(wx_product_group, self).create(vals) except Exception as e: _logger.error(e)
def write(self, vals): ''' 功能:更新小店分组 :param vals: :return: ''' try: for product_group in self: appid = product_group.wx_officialaccount.wx_appid appsecret = product_group.wx_officialaccount.wx_appsecret group_help = group_manager.group_manager(appid, appsecret) result = group_help.modify_group( int(product_group.wx_group_id), vals['wx_group_name']) if result['errcode'] == 0: return super(wx_product_group, product_group).write(vals) _logger.info("更新微信小店分组成功:%s" % result['errmsg']) else: _logger.info("更新微信小店分组错误:%s" % result['errmsg']) except Exception as e: _logger.error(e)
def unlink(self): ''' 功能:删除小店分组 :return: ''' try: for product_group in self: appid = product_group.wx_officialaccount.wx_appid appsecret = product_group.wx_officialaccount.wx_appsecret group_help = group_manager.group_manager(appid, appsecret) # result = group_help.getbyid_group(int(product_group.wx_group_id)) # if result['errcode'] == 0: del_result = group_help.del_group( int(product_group.wx_group_id)) if del_result['errcode'] == 0: return super(wx_product_group, product_group).unlink() _logger.info("删除微信小店分组成功:%s" % del_result['errmsg']) else: _logger.info("删除微信小店分组错误:%s" % del_result['errmsg']) except Exception as e: _logger.error(e) raise e
def sync_group(self): try: appid = self.wx_officialaccount.wx_appid appsecret = self.wx_officialaccount.wx_appsecret group_help = group_manager.group_manager(appid, appsecret) result = group_help.getall_group() wx_productgroup_obj = self.env['wx.productgroup'] if result['errcode'] == 0: groups = result['groups_detail'] if len(groups) == 0: msg = '公众号"%s"下的微信小店还没有商品分组,无需同步' % self.wx_officialaccount.wx_name raise except_osv(_('提示'), _(msg)) else: for group in groups: values = {} values[ 'wx_officialaccount'] = self.wx_officialaccount.id values['wx_group_id'] = group['group_id'] values['wx_group_name'] = group['group_name'] exist_samename_groups = wx_productgroup_obj.search([ ('wx_group_name', '=', values['wx_group_name']), ('wx_officialaccount', '=', self.wx_officialaccount.id) ]) if len(exist_samename_groups) > 0: exist_samename_groups[0].write(values) else: wx_productgroup_obj.create(values) else: err_msg = "调用微信小店获取所有分组接口失败:%s" % result['errmsg'] _logger.info(err_msg) raise except_osv(_('错误'), _(err_msg)) except Exception as e: err_msg = e.message if hasattr(e, 'value'): err_msg += e.value _logger.error(err_msg) raise except_osv(_('提示'), _(err_msg))
def sync_product(self, single): try: product_id = self.wx_productid wx_product = self # 准备调用sdk official_account = wx_product.wx_shop.wx_official_account appid = official_account.wx_appid appsecret = official_account.wx_appsecret product_help = product_manager.product_manager(appid, appsecret) img_result = self.upload_images(product_help, wx_product, False) main_img_url = img_result['main_img_url'] imglist = img_result['imglist'] detaillist = img_result['detaillist'] # productinfo = product_help.get_productInfo(product_id) # detail_html=productinfo['product_info']['product_base']['detail_html'] # category_id = wx_product.wx_productcategory.wx_productcategory_id # 微信产品分类 product_name = wx_product.oe_product.name # 商品名字不传,调用接口后清空了 standard_price = wx_product.oe_product.list_price isPostFree = 0 isHasReceipt = 0 if self.wx_ishasreceipt: isHasReceipt = 1 isUnderGuaranty = 0 if self.wx_isunderguaranty: isUnderGuaranty = 1 isSupportReplace = 0 if self.wx_isSupportReplace: isSupportReplace = 1 buy_limit = self.wx_buylimit sku_info = [] for att_line in wx_product.oe_product.attribute_line_ids: dic = {} dic["spec_name"] = '$' + att_line.attribute_id.name dic["spec_values"] = ['$' + v.name for v in att_line.value_ids] sku_info.append({ "id": dic['spec_name'], 'vid': dic['spec_values'] }) # sku_list product_tmpl_id = wx_product.oe_product.id location_id = wx_product.wx_shop.oe_location.id products = self.env['product.product'].search([ ('product_tmpl_id', '=', product_tmpl_id) ]) # 指定库存库位 sku_list = [] pricelist_id = -1 if wx_product.wx_shop.oe_pricelist.id: pricelist_id = wx_product.wx_shop.oe_pricelist.id for product in products: wx_price = False if pricelist_id > 0: wx_price = self.get_wx_price(product.id, pricelist_id, False) dic = {} dic['sku_id'] = '' if wx_price: dic['lst_price'] = wx_price else: dic['lst_price'] = product.lst_price # wx_product_line = self.get_wx_product_line( wx_product.id, product.id) if wx_product.single_spec: if wx_product.single_spec_quantity > 0: sku_list.append({ "sku_id": dic["sku_id"], "price": dic['lst_price'] * 100, "product_code": str(int(time.time())), "icon_url": main_img_url, "ori_price": standard_price * 100, "quantity": wx_product.single_spec_quantity # 固定库存量 }) # 更新库存 modify_stock_qty_result = product_help.modify_stock_qty( product_id, '', wx_product.single_spec_quantity_offset) if modify_stock_qty_result['errcode'] == 0: wx_product.single_spec_quantity += wx_product.single_spec_quantity_offset wx_product.single_spec_quantity_offset = 0 _logger.info('产品:%s 更新库存成功' % product_name) else: _logger.info('产品:%s 更新库存失败' % product_name) elif wx_product_line['issuccess']: dic["sku_id"] += '$' + wx_product_line[ 'spec_name'] + ':$' + wx_product_line['spec_value'] dic['qty_available'] = wx_product_line['wx_quantity'] sku_list.append({ "sku_id": dic["sku_id"], "price": dic['lst_price'] * 100, "product_code": str(int(time.time())), "icon_url": main_img_url, "ori_price": wx_product_line['wx_lst_price'] * 100, "quantity": dic['qty_available'] }) # 更新库存 stock_qty = wx_product_line["wx_quantity_offset"] if stock_qty <> 0: _logger.info("满足更新库存条件,准备更新库存") stock_skuinfo_id = wx_product_line['spec_name'] stock_skuinfo_vid = wx_product_line['spec_value'] stock_skuinfo = "$" + stock_skuinfo_id + ":$" + stock_skuinfo_vid modify_stock_qty_result = product_help.modify_stock_qty( product_id, stock_skuinfo, stock_qty) if modify_stock_qty_result['errcode'] == 0: wx_product_line_obj = self.env[ 'ycloud.wx.product.line'] wx_product_line4stock = wx_product_line_obj.search( [('id', '=', wx_product_line['id'])]) wx_product_line4stock.wx_quantity += stock_qty wx_product_line4stock.wx_quantity_offset = 0 _logger.info('%s 更新库存成功' % stock_skuinfo) else: _logger.info('%s 更新库存失败' % stock_skuinfo) else: _logger.info("不满足更新库存条件,不处理库存") # location country = wx_product.wx_productcountry.name province = wx_product.wx_productprovince.name location = { "country": country, "province": province, "city": wx_product.wx_productcity, "address": wx_product.wx_productaddress } # 运费模版,暂时统一 delivery_info = { "delivery_type": 0, "template_id": 0, "express": [{ "id": 10000027, "price": 0 * 100 }] } result = product_help.modify_product_info( category_id, product_name, product_id, main_img_url, imglist, detaillist, buy_limit, sku_info, sku_list, isPostFree, isHasReceipt, isUnderGuaranty, isSupportReplace, delivery_info, location) isSuccess = False if result['errcode'] == 0: # ********修改分组商品****** group_ids = '' list_old = wx_product.wx_group.split(',') list_new = [] if wx_product.wx_group_cross: for group in wx_product.wx_group_cross: group_ids += str(group.wx_group_id) + ',' list_new.append(str(group.wx_group_id)) wx_product.wx_group = group_ids[:-1] list_group_del = list(set(list_old) - set(list_new)) list_group_add = list(set(list_new) - set(list_old)) group_help = group_manager.group_manager(appid, appsecret) for group_id in list_group_add: if len(group_id) > 0: group_result = group_help.product_mod_group( int(group_id), wx_product.wx_productid, 1) if group_result["errcode"] == 0: _logger.info("pass") else: _logger.info("error") for group_id in list_group_del: if len(group_id) > 0: group_result = group_help.product_mod_group( int(group_id), wx_product.wx_productid, 0) if group_result["errcode"] == 0: _logger.info("pass") else: _logger.info("error") # if wx_product.wx_group_cross: # for group in wx_product.wx_group_cross: # group_result = group_help.product_mod_group(group.wx_group_id,wx_product.wx_productid, 1) # if group_result["errcode"] == 0: # _logger.info("商品:%s已经成功加入%s分组" % (product_name, group.wx_group_name)) # else: # pass # ********修改分组商品****** if single: _logger.info('同步成功') # raise except_osv(_('提示'), _('同步成功!')) else: isSuccess = True else: if single: msg = '同步商品出错,错误原因: %s' % result['errmsg'] raise except_osv(_('错误'), _(msg)) else: pass return isSuccess except Exception as e: _logger.error(e) raise e
def create_product(self, cr, uid, ids, context=None): count = len(ids) if count == 0: print 'count=0' else: result_success = "" result_error = "" for id in ids: try: wx_product = self.browse(cr, uid, id, context=context) pricelist_id = -1 # 默认无价目表 if wx_product.wx_shop.oe_pricelist.id: pricelist_id = wx_product.wx_shop.oe_pricelist.id print pricelist_id official_account = wx_product.wx_shop.wx_official_account appid = official_account.wx_appid appsecret = official_account.wx_appsecret product_help = product_manager.product_manager( appid, appsecret) # 1.上传商品图片 img_result = self.upload_images(product_help, wx_product, True) main_img_url = img_result['main_img_url'] imglist = img_result['imglist'] detaillist = img_result['detaillist'] if not main_img_url: raise except_osv(_('错误'), _('没有维护产品主图,不允许发布!')) # 2.创建商品到微信小店 categoryid = wx_product.wx_productcategory.wx_productcategory_id # "537119729" print categoryid isSupportReplace = wx_product.wx_isSupportReplace product_name = wx_product.oe_product.name standard_price = wx_product.oe_product.list_price isUnderGuaranty = wx_product.wx_isunderguaranty # TODO: 商品分类上的属性,非必输项 propertylist = [] isHasReceipt = wx_product.wx_ishasreceipt # 运费模版,暂时统一 delivery_info = { "delivery_type": 0, "template_id": 0, "express": [{ "id": 10000027, "price": 0 * 100 }] } buy_limit = wx_product.wx_buylimit # 获取多规格 product_tmpl_id = wx_product.oe_product.id # sku_info=[{ # "id":"$重量", # "vid":["$50克","$100克"] # }, # {"id": "$长度", "vid":["$1米","$2米"]} # ] sku_info = [] for att_line in wx_product.oe_product.attribute_line_ids: dic = {} dic["spec_name"] = '$' + att_line.attribute_id.name dic["spec_values"] = [ '$' + v.name for v in att_line.value_ids ] sku_info.append({ "id": dic['spec_name'], 'vid': dic['spec_values'] }) product_ids = self.pool.get('product.product').search( cr, uid, [('product_tmpl_id', '=', product_tmpl_id)]) location_id = wx_product.wx_shop.oe_location.id # context['location'] = location_id # 指定库存库位 products = self.pool.get('product.product').browse( cr, uid, product_ids, context={'location': location_id}) # sku_list = { # "sku_id": "$重量:$50克;$长度:1米", # "price": standard_price, # "product_code": "testing", # "icon_url": main_img_url, # "ori_price": standard_price, # "quantity": 800 # } sku_list = [] for product in products: wx_price = False if pricelist_id > 0: wx_price = self.get_wx_price( cr, uid, product.id, pricelist_id, False, context) dic = {} dic['sku_id'] = '' if wx_price: dic['lst_price'] = wx_price else: dic['lst_price'] = product.lst_price # wx_product_line = self.get_wx_product_line( cr, uid, id, product.id, context) if wx_product.single_spec: if wx_product.single_spec_quantity > 0: sku_list.append({ "sku_id": dic["sku_id"], "price": dic['lst_price'] * 100, "product_code": str(int(time.time())), "icon_url": main_img_url, "ori_price": standard_price * 100, "quantity": wx_product.single_spec_quantity # 固定库存量 }) elif wx_product_line['issuccess']: dic["sku_id"] += '$' + wx_product_line[ 'spec_name'] + ':$' + wx_product_line[ 'spec_value'] dic['qty_available'] = wx_product_line[ 'wx_quantity'] sku_list.append({ "sku_id": dic["sku_id"], "price": dic['lst_price'] * 100, "product_code": str(int(time.time())), "icon_url": main_img_url, "ori_price": wx_product_line['wx_lst_price'] * 100, "quantity": dic['qty_available'] }) # # !!!!!!!!!!!!!!!!!! isPostFree = 0 country = wx_product.wx_productcountry.name province = wx_product.wx_productprovince.name location = { "country": country, "province": province, "city": wx_product.wx_productcity, "address": wx_product.wx_productaddress } if len(sku_list) > 0: # 固定库存量 result = product_help.create_product( categoryid, product_name, main_img_url, imglist, detaillist, propertylist, buy_limit, sku_info, sku_list, isPostFree, isHasReceipt, isUnderGuaranty, isSupportReplace, delivery_info, location) print "-->" + str(result) if result['errcode'] == 0: wx_product.wx_productid = result['product_id'] wx_product.wx_image_url = main_img_url # ********修改分组商品****** group_ids = '' group_help = group_manager.group_manager( appid, appsecret) if wx_product.wx_group_cross: for group in wx_product.wx_group_cross: group_ids += str(group.wx_group_id) + ',' group_result = group_help.product_mod_group( group.wx_group_id, wx_product.wx_productid, 1) if group_result["errcode"] == 0: _logger.info("商品:%s已经成功加入%s分组" % (product_name, group.wx_group_name)) wx_product.wx_group = group_ids[:-1] # ********修改分组商品****** wx_product.state = 'released' result_success += product_name else: skd_error_msg = "调用微信商品发布接口报错:%s" % result['errmsg'] result_error += product_name raise except_osv(_('错误'), _(skd_error_msg)) else: msg = "没有满足发布条件的商品,不能进行发布操作!%s" % '\n1.单规格商品,数量必须大于0;\n2.多规格商品,至少有一个规格的数量大于0!' _logger.debug(msg) raise except_osv(_('错误'), _(msg)) except Exception as e: _logger.error(e) raise e result_success = result_success[:-1] result_error = result_error[:-1] result_msg = "" if result_success != "": result_msg += "商品:" + result_success + "发布成功" if result_error != "": result_msg += "商品:" + result_error + "发布失败" # raise except_osv(_('提示'), _(result_msg)) _logger.debug(result_msg)