web_shop_woocommerce/product.py

60 lines
1.9 KiB
Python
Raw Normal View History

2020-06-04 17:09:33 +02:00
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.pool import PoolMeta
from trytond.tools import slugify
from .web import ShopWooCommerceIdMixin
class Category(ShopWooCommerceIdMixin, metaclass=PoolMeta):
__name__ = 'product.category'
def get_woocommerce_entity(self):
values = {
'name': self.name,
'slug': slugify(self.name).lower(),
2020-06-04 17:09:33 +02:00
'parent': 0,
}
if self.parent:
if self.parent.woocommerce_id:
values['parent'] = self.parent.woocommerce_id
else:
return
return values
class Product(ShopWooCommerceIdMixin, metaclass=PoolMeta):
__name__ = 'product.product'
def get_woocommerce_entity(self):
short_description = description = (self.description or '')
2020-06-04 17:09:33 +02:00
lines = description.splitlines()
if len(lines) > 1:
short_description = lines[0]
description = '\n'.join(lines[1:])
2020-06-17 19:14:44 +02:00
list_price = self.list_price
sale_price = self.get_sale_price([self], 0)[self.id]
if sale_price > list_price:
list_price = sale_price
2020-06-04 17:09:33 +02:00
values = {
'name': self.name,
'type': 'simple',
2020-06-17 19:14:44 +02:00
'regular_price': str(list_price),
'sale_price': str(sale_price),
2020-06-04 17:09:33 +02:00
'description': description,
'short_description': short_description,
'status': 'publish',
}
2020-06-22 13:55:42 +02:00
if self.type != 'service':
values['manage_stock'] = True
2020-06-22 13:55:42 +02:00
values['stock_quantity'] = self.forecast_quantity
2020-06-04 17:09:33 +02:00
if self.code:
values['sku'] = self.code
categories = []
for category in self.categories:
if category.woocommerce_id:
categories.append({'id': category.woocommerce_id})
values['categories'] = categories
return values