web_shop_woocommerce/product.py

87 lines
2.7 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.
2020-06-23 13:07:46 +02:00
from trytond.model import fields
2020-06-04 17:09:33 +02:00
from trytond.pool import PoolMeta
2020-06-23 13:07:46 +02:00
from trytond.pyson import Eval
2020-06-04 17:09:33 +02:00
from trytond.tools import slugify
from .web import ShopWooCommerceIdMixin
class Category(ShopWooCommerceIdMixin, metaclass=PoolMeta):
__name__ = 'product.category'
2020-06-23 13:07:46 +02:00
woocommerce_tax_class = fields.Char("WooCommerce Tax Class",
states={
'invisible': ~Eval('accounting', False),
},
depends=['accounting'])
2020-06-04 17:09:33 +02:00
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'
2020-06-23 13:07:46 +02:00
@property
def woocommerce_tax_class(self):
if self.account_category:
parent = self.account_category
while parent:
if parent.woocommerce_tax_class:
return parent.woocommerce_tax_class
parent = parent.parent
return ''
def woocommerce_disable_data(self, shop):
return {
'status': 'private',
'catalog_visibility': 'hidden',
}
2020-06-04 17:09:33 +02:00
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
2020-06-23 13:07:46 +02:00
tax_class = self.woocommerce_tax_class
if tax_class is not None:
values['tax_class'] = tax_class
2020-06-04 17:09:33 +02:00
return values