Can't add content to woocommerce_after_shop_loop - wordpress

I'm trying to add a second product description to my WooCommerce page: https://alvinecph.dk/kategori/to-go/ from the guide from https://www.businessbloomer.com/woocommerce-add-a-second-content-box-product-category-pages/ .
I do however experience an issue, that the second description isn't showing for the hook woocommerce_after_shop_loop. I've tried to change the hook to woocommerce_before_shop_loop_item, and this seems to work, showing the text. So it could seem like woocommerce_after_shop_loop isn't present on the page.
I'm using the Divi theme, and have created a template for the category/archive page with their builder. To this template I've the Woo Product block. So I'm really baffled about this.
Is there something I'm missing. I've also tried to disable all plugins.
BR
Martin
// 1. Display field on "Add new product category" admin page
add_action( 'product_cat_add_form_fields', 'bbloomer_wp_editor_add', 10, 2 );
function bbloomer_wp_editor_add() {
?>
<div class="form-field">
<label for="seconddesc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label>
<?php
$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);
wp_editor( '', 'seconddesc', $settings );
?>
<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</div>
<?php
}
// ---------------
// 2. Display field on "Edit product category" admin page
add_action( 'product_cat_edit_form_fields', 'bbloomer_wp_editor_edit', 10, 2 );
function bbloomer_wp_editor_edit( $term ) {
$second_desc = htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="second-desc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label></th>
<td>
<?php
$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);
wp_editor( $second_desc, 'seconddesc', $settings );
?>
<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</td>
</tr>
<?php
}
// ---------------
// 3. Save field # admin page
add_action( 'edit_term', 'bbloomer_save_wp_editor', 10, 3 );
add_action( 'created_term', 'bbloomer_save_wp_editor', 10, 3 );
function bbloomer_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
if ( isset( $_POST['seconddesc'] ) && 'product_cat' === $taxonomy ) {
update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
}
}
// ---------------
// 4. Display field under products # Product Category pages
add_action( 'woocommerce_after_shop_loop', 'bbloomer_display_wp_editor_content', 5 );
function bbloomer_display_wp_editor_content() {
if ( is_product_taxonomy() ) {
$term = get_queried_object();
if ( $term && ! empty( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) {
echo '<p class="term-description">' . wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) . '</p>';
}
}
}

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/

Adding custom field on ckeckout page

Good morning,
I wrote/modified the following code to create 2 different new fileds on woocommerce (bithdate, and city of bith).
Everything works like a charm (in admin page, in my account page) but I'd like to show them on checkout page (so at the first time the customer buy something I can collect that info too [required]).
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'Data di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
woocommerce_form_field( 'birthcity_field', array(
'type' => 'text',
'label' => __( 'Città di nascita', 'woocommerce' ),
'placeholder' => __( 'Città di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthcity_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Prego inserire data di nascita', 'woocommerce' ) );
}
if ( isset($_POST['birthcity_field']) && empty($_POST['birthcity_field']) ) {
$args->add( 'error', __( 'Prego inserire città di nascita', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
if( isset($_POST['birthcity_field']) && ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field($_POST['birthcity_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add field - admin
function add_user_birtday_field( $user ) {
?>
<h3><?php _e('Dati aggiuntivi','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="birthday_field"><?php _e( 'Data di nascita', 'woocommerce' ); ?></label></th>
<td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<table class="form-table">
<tr>
<th><label for="birthcity_field"><?php _e( 'Città di nascita', 'woocommerce' ); ?></label></th>
<td><input type="text" name="birthcity_field" value="<?php echo esc_attr( get_the_author_meta( 'birthcity_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'show_user_profile', 'add_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birthcity_field', 10, 1 );
// Save field - admin
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
if( ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field( $_POST['birthcity_field'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
add_action( 'personal_options_update', 'save_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birthcity_field', 10, 1 );
I'll be very gratefull to everyone who can help me.
Warm regards
Ale
I solved by myself inspired by this article Post on Stackoverflow, BUT as I usual do I'd like to share the solution because could be helpful for someone.
Based on the code posted above You can add the field on the checkout page with the following code:
// Add the field to the checkout
add_action( 'woocommerce_after_checkout_billing_form', 'birth_day_checkout_field' );
function birth_day_checkout_field( $checkout ) {
// if customer is logged in and his birth date is defined
if(!empty(get_user_meta( get_current_user_id(), 'birthday_field', true ))){
$checkout_birht_date = esc_attr(get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
// The customer is not logged in or his birth date is NOT defined
else {
$checkout_birht_date = $checkout->get_value( 'birthday_field' );
}
echo '<p id="custom_birthday_field">';
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'class' => array('birth-date form-row-wide'),
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'mm/dd/yyyy', 'woocommerce' ),
'required' => true,
), $checkout_birht_date);
echo '</p>';
}
// Process the checkout
add_action('woocommerce_checkout_process','birth_day_checkout_field_process');
function birth_day_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['birthday_field'] )
wc_add_notice( __( 'Inserire la <strong>Data di nascita</strong>', 'woocommerce' ), 'error' )
;
}
// update order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'birth_day_checkout_field_update_order_meta' );
function birth_day_checkout_field_update_order_meta( $order_id ) {
if (!empty( $_POST['birthday_field'])){
update_post_meta( $order_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
// updating user meta (for customer my account edit details page post data)
update_user_meta( get_post_meta( $order_id, '_customer_user', true ), 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
// update user meta with Birth date (in checkout and my account edit details pages)
add_action ( 'personal_options_update', 'birth_day_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'birth_day_save_extra_profile_fields' );
add_action( 'woocommerce_save_account_details', 'birth_day_save_extra_profile_fields' );
function birth_day_save_extra_profile_fields( $user_id )
{
// for checkout page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
// for customer my account edit details page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
In this way this data will be updated everywhere (my account, admin and checkout).
Warm regards
Ale

Add image upload custom field at dokan template new-product.php

I am using dokan plugin for a multivendor website, I want to add image upload custom field in the template
new-product.php, I used CMB2 plugin to create image upload custom field with WooCommerce like this
function themebox_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = 'themebox_met_';
// Product Settings
$header_settings = new_cmb2_box( array(
'id' => 'Extra_settings',
'title' => esc_html__( 'Extra Settings', 'themebox' ),
'object_types' => array( 'product'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$header_settings->add_field( array(
'name' => esc_html__( 'Add Image Detail size 590x300 px', 'themebox' ),
'id' => $prefix . 'img_detail',
'type' => 'file'
) );
}
I want to add this custom image upload field in template form new-product.php and when save form in dokan
the image upload custom field update with an image added in dokan .....exactly like featured product image in WooCommerce
You can override the new-product.php and new-product-single.php file and then add your field on the product upload/edit file. To add new field for product edit/add the template you will have to add a new field in this template (Override via child-theme) - dokan-lite/templates/products/new-product-single.php. However, there are some other steps required to successfully save them.
You need to modify the Dokan product upload template and then you have to add an extra field by overriding the template. After adding the input filed you have to save the value of the field. On that place you have to use do_action( 'dokan_new_product_added', $product_id, $post_data ); this hook to save the field data.
When you will edit the product that time you have to use do_action( 'dokan_product_updated', $post_id ); to re-save.
Thanks :)
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['title'] ) ) {
update_post_meta( $product_id, 'title', $postdata['title'] );
}
if ( ! empty( $postdata['subtitle'] ) ) {
update_post_meta( $product_id, 'subtitle', $postdata['subtitle'] );
}
if ( ! empty( $postdata['subdescription'] ) ) {
update_post_meta( $product_id, 'subdescription', $postdata['subdescription'] );
}
if ( ! empty( $postdata['vidimg'] ) ) {
update_post_meta( $product_id, 'vidimg', $postdata['vidimg'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,8);
function show_on_edit_page($post, $post_id){
$subtitle = get_post_meta( $post_id, 'subtitle', true );
$title = get_post_meta( $post_id, 'title', true );
$subdesc = get_post_meta( $post_id, 'subdescription', true );
$vidimg = get_post_meta( $post_id, 'vidimg', true );
?>
<div class="dokan-form-group">
<h6 class="auto">Ajoutez du contenu pour mettre en valeur cette oeuvre !</h6>
<input type="hidden" name="title" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Autre Titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'title', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $title ) ); ?>
<p class="help-block">50 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<input type="hidden" name="subtitle" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="subtitle" class="form-label"><?php esc_html_e( 'Sous titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'subtitle', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $subtitle ) ); ?>
<p class="help-block">80 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<label for="subdescription" class="form-label">Paragraphe d'introduction</label>
<div class="dokan-rich-text-wrap">
<?php dokan_post_input_box( $post_id, 'subdescription', array('placeholder' => 'ajouter une description', 'value' => $subdesc ), 'textarea' ); ?>
</div>
</div>
<div class="dokan-feat-image-upload">
<?php
$wrap_class = ' dokan-hide';
$instruction_class = '';
$feat_image_id = 0;
if (!empty($vidimg) ) {
$wrap_class = '';
$instruction_class = ' dokan-hide';
$imaid =attachment_url_to_postid($vidimg);
}
?>
<div class="instruction-inside<?php echo esc_attr( $instruction_class ); ?>">
<input type="hidden" name="vidimg" class="dokan-feat-image-id" value="<?php echo esc_attr($vidimg ); ?>">
<i class="fa fa-cloud-upload"></i>
<?php esc_html_e( 'Upload a product cover image', 'dokan-lite' ); ?>
</div>
<div class="image-wrap<?php echo esc_attr( $wrap_class ); ?>">
<a class="close dokan-remove-feat-image">×</a>
<?php if ( ! empty($vidimg) ) { ?>
<img src="<?php echo esc_url(wp_get_attachment_url($vidimg ) ); ?>" alt="">
<?php } else { ?>
<img height="" width="" src="" alt="">
<?php } ?>
</div>
</div><!-- .dokan-feat-image-upload -->
<?php
}

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.

Add a step to WooCommerce Checkout

Is there any way to add a new step to the Checkout process in WooCommerce? I need something between the cart and the Billing Details page to collect some additional information. I've researched quite a bit and turned up nothing. I can't be the only person who wants to do this.
From my tutorial on WooCommerce Custom Checkout Fields this is how to add a custom checkout field which you could use to collect additional information.
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

Resources