Skip to content

Save Button Eng

Save button creation

This is the instruction to create a button that can be used to export data from “Website Item” Doctype to “my.prom.ua” web-service.

To add a custom button for the “Website item” doctype in ERPNext v14 using the Frappe framework, you have to follow the attached steps:

Button creation

  1. Login to ERPNext as a System Manager or Administrator.
  2. Go to the search bar and type “Client Script List” and select the following option.
  3. Click on the ”+ Add Client Script” button to add a new client script. In the new client script, type “Website Item” in “Doctype” field and add the following code:
frm.add_custom_button(__("Save to Prom"), function() {
frm.refresh();
setTimeout(() => {frm.reload_doc();}, 500);
setTimeout(() => {
frappe.call({
method: "erpnext.e_commerce.doctype.website_item.website_item.save_to_prom_button",
args: {doc: frm.doc},
callback: function(result) {
frappe.msgprint(result);
}
}); }, 1000);
}, __("Marketplaces"));

This code adds a custom button labeled “Save to Prom” to the “Website Item” form, and calls the server method. 4. Check “Enabled” to “true” and save the client script by clicking on the “Update” button. After following these steps, you should see the “Save to Prom” button in the “Website Item” form, which will call the specified method when clicked.

How to check You can check whether button has been added by going to the "Website Item" Doctype record and see the button in Marketplaces section bar.

Server script creation

But the following “erpnext.e_commerce.doctype.website_item.website_item.save_to_prom_button” method ain’t standart frappe method. We have to add it.

We have to append the following script in the file by the path “frappe-bench/apps/erpnext/erpnext/e_commerce/doctype/website_item/website_item.py”:

import requests
import json
def save_to_prom(doc, token):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
product = requests.get(
"https://my.prom.ua/api/v1/products/by_external_id/{}".format(doc['name']),
headers=headers
).json().get('product')
if product:
presence_match = {
'+': 'available',
'-': 'not_available'
}
body = [{
'id': product['id'],
'price': doc['price_value']
}]
# Availability
try:
order_days = int(doc['availability'])
body[0]['presence'] = "order"
except ValueError:
body[0]['presence'] = presence_match[doc['availability']]
# Name, description and keywords in ukrainian
if doc['translation']:
for i in doc['translation']:
if i['language'] == 'ru':
body[0]['name'] = i['specific_name']
body[0]['description'] = i['specific_description']
body[0]['keywords'] = i['search_requests']
# Wholesale prises
if doc['wholesale_price']:
body[0]['prices'] = [{'price':int(i['price']), 'minimum_order_quantity': int(i['minimum_order_quantity'])} for i in doc['wholesale_price']]
resp = requests.post(
'https://my.prom.ua/api/v1/products/edit',
data=json.dumps(body),
headers=headers
).json()
frappe.msgprint(f"{resp}")
body = {
'product_id': str(product['id']),
"lang": "uk",
"name": doc['web_item_name'],
"keywords": doc['search_terms'],
"description": doc['description']
}
resp = requests.put(
'https://my.prom.ua/api/v1/products/translation',
data=json.dumps(body),
headers=headers
).json()
frappe.msgprint(f"{resp}!")
else:
frappe.msgprint("Product is not found on Prom. You should import Item first.")
@frappe.whitelist()
def save_to_prom_button(doc):
prom_settings = frappe.get_doc("Prom Settings", "Prom Settings")
token = prom_settings.get_password("prom_token")
doc = json.loads(doc)
save_to_prom(doc, token)

Now you can use button from the previous steps in “Website Item” Doctype record.