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),
|
|
|
|
'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):
|
2020-06-17 18:52:19 +02:00
|
|
|
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',
|
|
|
|
'manage_stock': 'true',
|
|
|
|
'stock_quantity': 12,
|
|
|
|
}
|
|
|
|
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
|