I have added a custom text field to woocommerce and also displayed it in the frontend of WC Vendor pro. So vendors can add a youtube or vimeo link to embed their movies.
However for some reason I can't get it to save and display in the product page on the front end.
The code I have so far in functions.php:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'video_url',
'label' => __( 'Your product video link (youtube/vimeo)', 'woocommerce' ),
'placeholder' => 'https://',
'desc_tip' => 'true',
'description' => __( 'Copy the Youtube or Vimeo link here', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['video_url'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'video_url', esc_attr( $woocommerce_text_field ) );
}
And to display the field in the vendor section:
<div class="all-100">
<!-- Media uploader -->
<div class="wcv-product-media">
<?php BuddyBoss_BM_Templates::product_media_uploader( $object_id ); ?>
<?php woo_add_custom_general_fields( $object_id ); ?>
</div>
</div>
So now vendors can enter the field. However saving nor displaying it won't work. I added the following to the product page:
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
// tried this one as well:
echo get_post_meta( $post->ID, 'video_url', true );
Thanks any help would be greatly appreciated!
Assuming you use acf to use get_field, i think you miss the second parameter $post_id, the function need to know the post id of the field you want to get.
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
You must right it like this
global $post; // I don't know where is your script (in the loop ? in a function ?)
$youtubevideo_code = wp_oembed_get( get_post_meta($post->ID, 'video_url', true )); // can be get_field('video_url, $post->ID);
echo $youtubevideo_code;
Related
I'm struggling with a basic php function.
I implemented a backend checkbox in wordpress / woocommerce general tab which works fine:
// Add checkbox in Backend
function action_woocommerce_product_options_general_product_data() {
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not)
'label' => __( 'Warenkorb Button', 'woocommerce' ), // Text in the editor label
'desc_tip' => false, // true or false, show description directly or as tooltip
'description' => __( 'Warenkorb Button ausblenden', 'woocommerce' ) // Provide something useful here
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset, yes or no
$checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';
// Update meta
$product->update_meta_data( '_prevent_add_to_cart_button', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
Now i want to ask for the value of that checkbox and do things based on it.
It works in this function as expected:
// Add function for category / listing page add to cart button hook
function hide_listingpage_button( $add_to_cart_html, $product, $args ){
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
$before = '<span style="display:none;">';
$after = '</span>';
return $before . $add_to_cart_html . $after;
}
else {
return $add_to_cart_html;
}
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'hide_listingpage_button', 10, 3 );
but it does not work in this function(s):
// Add functions for singlepage before / after add to cart button hook
function beforehide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
function afterhide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'beforehide_singlepage_button' );
add_action( 'woocommerce_after_add_to_cart_button', 'afterhide_singlepage_button' );
Im my opinion im doing basically the same things?
I give the $product object as argument in the function, ask then for yes / no boolean with
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
and then echo something with the if clause.
however, i get no output of the page from that line, and also no error message.
What am i missing?
thank you!
I am using the code generated based on Add a custom multi-select field to admin product options settings in Woocommerce
// Display a multiselect field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_handles_product_field' );
function display_handles_product_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="handles_product"><?php _e( 'Handles Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_handles_product_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 );
function save_handles_product_field_value( $product ){
$data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array();
$product->update_meta_data( '_handles_product_ids', $data );
}
This code adds a custom search box and adds related products in the admin area when creating or editing a product.
I also created a new tab on the product page to show the added products.
/* New Handles Product Tab */
add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' );
function new_handles_product_tab( $tabs ) {
/* Add new tab */
$tabs['new_handles_product_tab'] = array(
'title' => __( 'Handles Product', 'woocommerce' ),
'priority' => 50,
'callback' => 'new_handles_product_tab_content'
);
return $tabs;
}
/* Display content in new tab */
function new_handles_product_tab_content() {
// Content
}
Tell me how to add these products to the new tab correctly? And is my code correct?
I will be glad for your help!
Using [products] shortcode with ids argument, the function displaying the product handles in a custom product tab will be:
function new_handles_product_tab_content() {
global $product;
$product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles
if( ! empty($product_ids) )
$product_ids = implode(',', $product_ids);
echo do_shortcode( "[products ids='$product_ids']" ); // Using [products] shortcode for display.
}
Untested it should work.
I created a custom post type with different input fields. For example first name and last name.
So I want this inputs as title in the post column-list. To do this I used the filter option:
add_filter( 'the_title', function( $title ) {
$title_firstname = esc_html( get_post_meta( get_the_ID(), 'first_name', true ) );
$last_name = esc_html( get_post_meta( get_the_ID(), 'last_name', true ) );
$title = $first_name . ' ' . $last_name;
return $title;
} );
Okay this works for me but there is one problem:
All titles form default-posts and pages are gone.
Whta can I do to change only my custom post titles?
Hope somebody can help me :)
Kind reagrds,
Jop
You can put in a conditional check to see what post type is being displayed to modify the title for only those custom posts.
add_filter( 'the_title', 'change_custom_post_title', 10, 2 );
function change_custom_post_title( $title, $id ) {
global $wp_query;
if( 'custom_post_type' !== get_post_type( $wp_query->post->ID ) )
return $title;
return 'My New custom post title';
}
I am working on a WordPress eCommerce website, with the chosen shopping platform; WooCommerce.
I have created a Custom Checkbox, within the WooCommerce Product Dashboard, by placing the following code into the functions.php file:
function product_custom_fields_add(){
global $post;
$input_checkbox = get_post_meta( $post->ID, '_engrave_text_option', true );
if( empty( $input_checkbox ) || $input_checkbox == 'no' ) $input_checkbox = '';
echo '<div class="product_custom_field">';
woocommerce_wp_checkbox(
array(
'id' => '_engrave_text_option',
'desc' => __('set custom Engrave text field', 'woocommerce'),
'label' => __('Display custom Engrave text field', 'woocommerce'),
'desc_tip' => 'true',
'value' => $input_checkbox
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');
To save the Custom Field's values, I have inserted the following code into the functions.php file:
function woocommerce_product_custom_fields_save($post_id){
$_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_engrave_text_option', $_engrave_text_option );
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
The idea is that when a Site Admin selects the Checkbox, it triggers the below code, in order to create a Custom Text Box on the Product Page:
function add_engrave_text_field() {
global $post;
// Get the checkbox value
$engrave_option = get_post_meta( $post->ID, '_engrave_text_option', true );
// If is single product page and have the "engrave text option" enabled we display the field
if ( is_product() && ! empty($engrave_option) ) {
?>
<div>
<label class="product-custom-text-label" for="engrave_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_engrave_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_engrave_text_option',true);?>" />
</label>
</div><br>
<?php
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
?>
The above code works when selecting the Checkbox, to create the Custom Text Field on the Product Page. Where the problem lies, is that when the Checkbox is deselected, the Custom Text Box remains on the Product Page.
Is anyone able to see where I am going wrong here?
Here is missing point:
$_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
So your meta value never gets empty value. It gets yes or no.
There are 2 solutions.
Change "no" to "";
$_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : '';
Change empty to =='yes'
if ( is_product() && $engrave_option=='yes' ) {
I added a custom field to a single product page for woocommerce in order to show ISBN number for the books I sell. I found a nice guide and managed to add everything as I want. However when I empty the custom field for ISBN it won't go empty on the site.
I have the following code in the functions.php
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_ISBN_field',
'label' => __( 'ISBN', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'ISBN.', 'woocommerce' )
)
);
function woo_add_custom_general_fields_save( $post_id ){
// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
}
Then in the short-description.php I made it so that it shows on the product page. However it still displays the name ISBN10: if it's an empty field.
<?php
// Display Custom Field Value
if (!((get_post_meta($post->ID, '_ISBN_field', true))==”)) {
//Not empty
echo '<b>ISBN10: </b>',get_post_meta( $post->ID, '_ISBN_field' , true);
}
?>
So the two problems are I can't edit the product to contain an empty custom field. And if the field is empty (only possible when field hasn't been previously contained data) it still displays the field name.
Thanks in advance.
Your save function should be like
function woo_add_custom_general_fields_save( $post_id ){
// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
else
update_post_meta( $post_id, '_ISBN_field', '' );
}
If !empty( $woocommerce_text_field ) returns true only if $_POST['_ISBN_field'] has some value so the post meta is not updated if $_POST['_ISBN_field'] is empty
what does:
var_dump( get_post_meta( $post->ID, '_ISBN_field' , true) );
return?
i guess the problem ist that the field still contains some value even it's empty..
check that var_dump and than adjust your if statement
and i guess the statement should be like:
if ( get_post_meta( $post->ID, '_ISBN_field', true ) != '' ) {
Try this:
<?php
// Display Custom Field Value
$ISBN_field = get_post_meta($post->ID, '_ISBN_field', true);
if( !empty( $ISBN_field ) ){
echo '<b>ISBN10: </b>'.$ISBN_field;
} ?>
Regards