I am migrating a webshop from a non- wordpress environment to woocommerce. The products would be synchronized from a 3rd party logistics API and the products contain extra data, like alcohol content, allergens, storage instructions, ingredients, etc... How can I add these properties to the product in code? I found plugins where I can add custom fields to the product page, but I don't need fields to be displayed, only some kind of extra data on the product page. I have the following class that implements the product creation process, how can I add the extra data to the product object?
class ProductRepository
{
public function syncProductToDatabase(array $productArray): int
{
$product = new WC_Product();
$product->set_name($productArray['name']);
$product->set_sku($productArray['sku']);
$product->set_status('publish');
$product->set_stock_quantity($productArray['availableQuantity']);
$product->set_price($productArray['price']);
/**
* @todo Add the extra parameters to the product
*/
return $product->save();
}
}
Your solution is to add a custom field to the product and this way you can see these fields on the product edit page and even retrieve them with coding, but the fields will not be displayed on the product front page.
Use this plugin to add a custom Meta box and add different fields to it.