I'd like to add a cost field to products of a WooCommerce website to make it easier to track cost vs. price and profitability. There is a Cost of Goods plugin, but I try to steer clear of plugins whenever possible.
I figure this would be something to add to the functions.php but I haven't yet found a solution and so I've decided to reach out to the StackOverflow community.
How can I start to add a custom field
includes the following code in functions.php:
## PRECIO DE COSTE (precio_coste)
// Añade el campo precio de coste del producto
function campos_personalizados_precio_coste() {
woocommerce_wp_text_input( array(
'id' => 'precio_coste',
'class' => 'wc_input_price short',
'label' => __( 'Precio de coste' . ' (' . get_woocommerce_currency_symbol() . ')', 'woocommerce' ),
'description' => __( 'Precio de coste del proveedor/fabricante. Necesario para poder calcular la factura del proveedor para Globe', 'woocommerce' ),
'style' => 'width:280px;'
) );
}
// add_action( 'woocommerce_product_options_pricing', 'campos_personalizados_precio_coste' );
add_action( 'woocommerce_product_options_sku', 'campos_personalizados_precio_coste' );
//Actualiza los valores introducidos en cada campo "precio_coste"
function guarda_campos_personalizados_precio_coste( $product_id ) {
// stop the quick edit interferring as this will stop it savingproperly, when a user uses quick edit feature
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce')) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if (isset($_POST['precio_coste'])) {
if (is_numeric($_POST['precio_coste'])) {
update_post_meta( $product_id, 'precio_coste', $_POST['precio_coste'] );
}
} else {
delete_post_meta( $product_id, 'precio_coste' );
}
}
add_action( 'save_post', 'guarda_campos_personalizados_precio_coste' );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.