Not sure if you need to hook within the single product page, you can remove that part of the code if needs be.
code goes in functions.php of your child theme or a code snippets plugin.
/**
* Function to render UI in Admin Product add/edit page
*/
function show_custom_price_admin() {
global $thepostid;
woocommerce_wp_text_input(
array(
'id' => 'custom_price',
'name' => '_custom_price',
'value' => get_post_meta( $thepostid, '_custom_price', true ),
'class' => 'wc_input_price short',
'label' => __( 'Custom Price ($)', 'vg' ),
)
);
}
add_action( 'woocommerce_product_options_pricing', 'show_custom_price_admin' );
/**
* Function to update custom price Admin Product add/edit page
*
* @param int $post_id Product's post id.
*/
function process_product_custom_data( $post_id ) {
$product = wc_get_product( $post_id );
$product->update_meta_data(
'_custom_price',
wc_clean( wp_unslash( filter_input( INPUT_POST, '_custom_price' ) ) )
);
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'process_product_custom_data' );
/**
* Function to show custom price front end page
*/
function show_custom_price_frontend() {
global $post;
$custom_price = get_post_meta( $post->ID, '_custom_price', true );
if ( $custom_price ) {
$custom_price = wc_price( $custom_price );
printf( '<p class="price">%s</p>', $custom_price );
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'show_custom_price_frontend' );
Thread Starter
ginogt
(@ginogt)
Hi Phill i have no coding experience what so ever, so i just wanted to ask is this just for the dashboard ? only visible to me under all products ? not visible on the actual site itself ?
You should remove the final part. So your code would be as below.
I tested on a fresh Blocksy theme and it works as described (not displaying in the front end). Like all code changes, make sure you backup before hand incase of any conflicts.
/**
* Function to render UI in Admin Product add/edit page
*/
function show_custom_price_admin() {
global $thepostid;
woocommerce_wp_text_input(
array(
'id' => 'custom_price',
'name' => '_custom_price',
'value' => get_post_meta( $thepostid, '_custom_price', true ),
'class' => 'wc_input_price short',
'label' => __( 'Custom Price ($)', 'vg' ),
)
);
}
add_action( 'woocommerce_product_options_pricing', 'show_custom_price_admin' );
/**
* Function to update custom price Admin Product add/edit page
*
* @param int $post_id Product's post id.
*/
function process_product_custom_data( $post_id ) {
$product = wc_get_product( $post_id );
$product->update_meta_data(
'_custom_price',
wc_clean( wp_unslash( filter_input( INPUT_POST, '_custom_price' ) ) )
);
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'process_product_custom_data' );
Let me know if it works!
-
This reply was modified 10 months, 1 week ago by Phill.
Thread Starter
ginogt
(@ginogt)
Ok so i eventually found the file, is there a specific place i need to copy it in ? or do i just slap it anywhere in there ? as i see there’s already a bit of code there
You can put it at the bottom of the file, after the other code. Alternatively you can download a plugin like Code Snippets and drop the code in there (and activate).
If you’re putting in functions.php, it’s best in a child theme as otherwise when you update your theme later you will lose your code.
Thread Starter
ginogt
(@ginogt)
hello again, i managed to install code snippets, watch a video on how to implement the code, but now what u said about child theme and having lose the code on a theme update, im not sure as to which is a child theme ? iv got astra installed and woocommerce and elementor ? which would be a child them ?
Hello,
if you add the code within code snippets, you don’t need to use a child theme.
-
This reply was modified 10 months, 1 week ago by Phill.
Thread Starter
ginogt
(@ginogt)
ok awesome! well i think i got it working, it shows up and i can edit it, thank you very much for the help, appreciate it