feat: save shipping address to woocommerce customer

This commit is contained in:
Shivam Mishra 2020-05-08 15:12:08 +05:30
parent a4567a446f
commit da41724d9d

View File

@ -49,12 +49,13 @@ def _order(*args, **kwargs):
if event == "created":
sys_lang = frappe.get_single("System Settings").language or 'en'
raw_billing_data = order.get("billing")
raw_shipping_data = order.get("shipping")
customer_name = raw_billing_data.get("first_name") + " " + raw_billing_data.get("last_name")
link_customer_and_address(raw_billing_data, customer_name)
link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name)
link_items(order.get("line_items"), woocommerce_settings, sys_lang)
create_sales_order(order, woocommerce_settings, customer_name, sys_lang)
def link_customer_and_address(raw_billing_data, customer_name):
def link_customer_and_address(raw_billing_data, raw_shipping_data, customer_name):
customer_woo_com_email = raw_billing_data.get("email")
customer_exists = frappe.get_value("Customer", {"woocommerce_email": customer_woo_com_email})
if not customer_exists:
@ -72,30 +73,40 @@ def link_customer_and_address(raw_billing_data, customer_name):
if customer_exists:
frappe.rename_doc("Customer", old_name, customer_name)
address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email})
billing_address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email, "address_type": "Billing"})
shipping_address = frappe.get_doc("Address", {"woocommerce_email": customer_woo_com_email, "address_type": "Shipping"})
else:
billing_address = create_address(raw_billing_data, customer, "Billing")
shipping_address = create_address(raw_shipping_data, customer, "Shipping")
if customer_exists:
rename_address(billing_address, customer)
rename_address(shipping_address, customer)
def create_address(raw_data, customer, address_type):
address = frappe.new_doc("Address")
address.address_line1 = raw_billing_data.get("address_1", "Not Provided")
address.address_line2 = raw_billing_data.get("address_2", "Not Provided")
address.city = raw_billing_data.get("city", "Not Provided")
address.woocommerce_email = customer_woo_com_email
address.address_type = "Billing"
address.country = frappe.get_value("Country", {"code": raw_billing_data.get("country", "IN").lower()})
address.state = raw_billing_data.get("state")
address.pincode = raw_billing_data.get("postcode")
address.phone = raw_billing_data.get("phone")
address.email_id = customer_woo_com_email
address.address_line1 = raw_data.get("address_1", "Not Provided")
address.address_line2 = raw_data.get("address_2", "Not Provided")
address.city = raw_data.get("city", "Not Provided")
address.woocommerce_email = customer.woocommerce_email
address.address_type = address_type
address.country = frappe.get_value("Country", {"code": raw_data.get("country", "IN").lower()})
address.state = raw_data.get("state")
address.pincode = raw_data.get("postcode")
address.phone = raw_data.get("phone")
address.email_id = customer.woocommerce_email
address.append("links", {
"link_doctype": "Customer",
"link_name": customer.customer_name
})
address.flags.ignore_mandatory = True
address = address.save()
if customer_exists:
def rename_address(address, customer):
old_address_title = address.name
new_address_title = customer.customer_name + "-billing"
new_address_title = customer.customer_name + "-" + address.address_type
address.address_title = customer.customer_name
address.save()