Show Custom Fields under "Add to Cart" button - wordpress

I have using this function code to make new Custom Text Area, but have issue to show it in product page.
// Custom Field Product
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_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">';
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Custom Text:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Changes to DB
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
}
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
Looks like when press save, its storing data in DB, but its not showing in single product page. Can someone to tell me where is issue in this function?

The problem is that in the following function, $post is not defined. (The code was indented for clarity.)
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
So, a simple fix would be to add global $post; to the function:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $post;
// custom content.
if ( $post ) {
echo get_post_meta( $post->ID, '_textarea', true );
}
}
Alternatively, you can use the global $product object:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $product;
// custom content.
if ( $product ) {
echo get_post_meta( $product->get_id(), '_textarea', true );
}
}

Related

Add a link to the product page woocomerce

I have made a custom field in the product card and displayed it in the catalogue, but the problem is that there is no link to go to the product card. I would appreciate any help.
Here is the code for adding a field in the product card
// Field Short name
add_action( 'woocommerce_product_options_general_product_data', 'shorttitle_custom_general_fields' );
add_action( 'woocommerce_process_product_meta', 'shorttitle_custom_general_fields_save' );
function shorttitle_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'shorttitle_field',
'label' => __( 'Краткое название', 'woocommerce' ),
//'desc_tip' => 'true',
//'description' => __( 'Краткое название', 'woocommerce' ),
'type' => 'html'
)
);
echo '</div>';
}
function shorttitle_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['shorttitle_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'shorttitle_field', esc_attr( $woocommerce_text_field ) );
}
This is the code for displaying this field on the catalogue page
// Change the output of the product header (in the catalogue)
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );
function custom_woocommerce_template_loop_product_title() {
global $post;
$product = wc_get_product( $post->ID );
$title = get_post_meta( $post->ID, 'shorttitle_field', true );
if( $title ) {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
} else {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
}
}
Page https://www.krisstyle.com.ua/shop/

Display Custom Order Filter In WooCommerce

I am trying to display a custom admin order column in WooCommerce and I am having a hard time getting the code right here.
I have this code:
add_filter( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1);
function display_order_data_in_admin( $output )
global $wp_query
$output .= wc_product_dropdown_categories( array(
'show_option_none' => 'Filter by Location',
'taxonomy => 'product_tag',
'name' => 'product_tag',
'selectd' => isset( $wp_query->query_vars['product_tag'] ) ? $wp_query->vars['procut_tag'] :
) );
return $output;
}
I am having a hard time displaying a column for filtering my orders by location that has been selected in checkout.
add_filter( 'manage_edit-shop_order_columns', 'wc_add_new_order_admin_list_column' );
function wc_add_new_order_admin_list_column( $columns ) {
$columns['billing_country'] = 'Country';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_new_order_admin_list_column_content' );
function wc_add_new_order_admin_list_column_content( $column ) {
global $post;
if ( 'billing_country' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_billing_country();
}
}

Custom Meta Field Has Stopped Working

I have a custom meta checkbox in a custom post type that until this afternoon was working fine, I went into add some new post types and the checkbox won't save I did add the isset value in the show_meta but it was still working after I did this and does not start working again if I remove it, and nothing is showing up in my debug can anybody help?:
function add_tiles_meta_box() {
add_meta_box(
'tiles_meta_box', // $id
'Show in Large Tiles?', // $title
'show_tiles_meta_box', // $callback
'tiles', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_tiles_meta_box' );
function show_tiles_meta_box( $post, $metabox ) {
$tiles = get_post_meta( $post->ID, 'tiles', true );
wp_nonce_field( basename(__FILE__), 'tiles_meta_box_nonce' );
?>
<!-- All fields will go here -->
<p>
<input type="checkbox" name="tiles" <?php echo (isset($_POST['tiles'])?"value='on'":"value='off'")?> <?php checked( $tiles, 'on' ); ?>/> Yes this is a large tile.
</p>
<?php
}
$post_ID = $post->ID;
function save_tiles_meta($post_ID) {
$post_ID = (int) $post_ID;
$post_type = get_post_type($post_ID);
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "tiles", $_POST["tiles"]);
}
return $post_ID;
}
add_action( 'save_post', 'save_tiles_meta' );
Your save_tiles_meta function is unnecessarily convoluted. Something may be failing along the way. Here's a more standard way to do that:
add_action( 'save_post', 'save_tiles_meta' );
function save_tiles_meta(){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}
Try with below code
add_action( 'save_post', 'save_tiles_meta',99,1 );
function save_tiles_meta($postId){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}

Display content on single product page in Woocommerce based on category

I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );

Output error with get_post_meta in woocommerce

I am trying to add a custom metabox in woocommerce. It has been added perfectly. My ultimate goal is to call that custom field in a function and show it in the cart.php. So I coded :
For Custom field: [I would refer http://www.remicorson.com/mastering-woocommerce-products-custom-fields/] in this regard
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">';
woocommerce_wp_text_input(
array(
'id' => 'number_field',
'label' => __( '<strong style="color:#239804">Your Free Products</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Please enter a number', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
}//woo_add_custom_general_fields
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['number_field'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'number_field', esc_attr( $woocommerce_number_field ) );
}//woo_add_custom_general_fields_save( $post_id )
It has perfectly fitted in the Product Admin Page. Now I am creating another function where I am creating a counter for cart.php
function free_products(){
global $woocommerce ,$product, $post;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$free_number = get_post_meta( $post->ID, 'number_field', true );
$free_product = $cart_item['quantity'] * $free_number;
echo apply_filters( 'woocommerce_cart_item_quantity', $free_product, $cart_item_key );
}
}
In My cart.php when I add
<td class="product-quantity">
<?php
echo free_products();
?>
</td>
The output become zero in front end. Can anyone please help me what I am going wrong. Thanks in advance.
Try below code :
function free_products(){
global $woocommerce ,$product, $post;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$my_var = $cart_item['product_id'];
$free_number = get_post_meta( $my_var, 'number_field', true );
$free_product = $cart_item['quantity'] * $free_number;
echo apply_filters( 'woocommerce_cart_item_quantity', $free_product);
}
}
Let me know if It is working for you or not.Its working for me.

Resources