ACF field value display on woocommerce category archive page - wordpress

I am working on woocommerce based webiste. I have create field through ACF called Add Sub title for woocommerce product post type . I want to display that field value after Main Title on woocommerce category archive page.
add_action('woocommerce_after_shop_loop_item_title', 'display_sub_title', 3 );
function display_sub_title() {
global $product;
$id = $product->get_id();
$add_sub_title = get_field('add_sub_title',$id);
if ( $add_sub_title ) { ?>
<h5><?php echo $add_sub_title; ?></h5>
<?php }
}
I have place above code in functions.php file but it's not working. How can I display sub-title under main title on category archive page. ??

I use this and works good
function show_subtitle() {
global $product;
$id = $product->get_id();
$subtitle = get_field('sottotitolo',$id);
if ( $subtitle ) {
echo '<span>'.$subtitle.'</span>';
}
}
add_action('woocommerce_after_shop_loop_item_title', 'show_subtitle', 3 );

Related

Add short description under the product title in WooCommerce archive pages

My site is https://outlet.ltd/deals. I tried to add a short description of the WooCommerce product to the achieve page grid (under product title) by following links but was not successful. Nothing shows up.
Add shortened description under the product title in WooCommerce archive pages
Adding A Short Description To Product Archives
I'm using REHub theme. Could anyone help?
You can do it with woocommerce_after_shop_loop_item_title hook
add_action('woocommerce_after_shop_loop_item_title','my_product_description');
function my_product_description() {
// get the global product obj
global $product;
// get the global product obj
$description = $product->get_short_description();
echo $description;
}
Add this code on your theme functions.php file.
// define the woocommerce_after_shop_loop_item_title callback
function action_woocommerce_after_shop_loop_item_title( ) {
global $product;
if( is_shop() ):
echo $product->get_short_description();
endif;
};
// add the action
add_action( 'woocommerce_after_shop_loop_item_title', 'action_woocommerce_after_shop_loop_item_title', 5 );
it works for me.

WooCommerce product short descriptions on a Wordpress page

I use WooCommerce shortcode to show some products on the front page.
Like this [products limit="3" category="my-category" ids="86, 71, 54"].
The front page is a regular WordPress static page. The problem is that it doesn't show product short descriptions. If I use the code below but for is_front_page(), it shows short description of a regular WordPress post (not of the listed products).
function custom_short_description() {
if ( is_product_category() ) {
echo '<div class="custom-short-description">' . get_the_excerpt() . '</div>';
} }
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
Adding to the function
global $post;
$product = get_product($loop->post);
and using
$product->post->post_excerpt;
didn't help.
Any ideas how to show product short descriptions?
===================
Update
===================
If you create custom loops, you might want to create variables at the beginning of the loop and then use them:
$product = wc_get_product( $loop->post->ID );
$product_short_description = $product->get_short_description();
$product_url = $product->add_to_cart_url();
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
function custom_short_description() {
if (is_front_page()) {
global $product;
echo '<div class="custom-short-description">' . $product->get_short_description() . '</div>';
}
}
This should get you the outcome you're looking for.
Tried and tested WordPress 5.1.

Woocommerce adding custom product count to products only in specific category and in specific page

I have caught onto some of the logic but battling with how to implement:
display my custom product count only on products in a specific product category
also display product count only on a specific custom WP page (which I used the product_category shortcode)
My code in functions.php is as follows and it does add the $top50_counter value before the product thumbnail but it is doing it site-wide, hence why I need to narrow it down as per my points above.
/* ADD NUMBERING TO TOP 50 LIST PRODUCTS */
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_before_shop_loop_item', 5);
$top50_counter=1;
function custom_before_shop_loop_item() {
global $top50_counter;
echo '<h1>'.$top50_counter.'</h1>';
$top50_counter++;
}
I'm assuming I have to use the $terms = get_the_terms function in there somehow?
You need to use is_page and has_term conditionals. Try re-factoring the code to the following.
/* ADD NUMBERING TO TOP 50 LIST PRODUCTS */
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_before_shop_loop_item', 5);
$top50_counter=1;
function custom_before_shop_loop_item() {
global $top50_counter;
/* Replace 42 with the actual page ID and "your-category" with the actual category slug */
if( ( is_page( 42 ) ) || ( has_term( 'your-category' , 'product_cat') ) ):
echo '<h1>'.$top50_counter.'</h1>';
$top50_counter++;
endif;
}
P.S: untested code.
Try this. Use corresponding category name and custom wp page slug in the following function.
function custom_before_shop_loop_item(){
global $post, $term, $top50_counter;
$id = $post->ID;
$taxonomy = 'product_cat';
$terms = get_the_terms( $id, $taxonomy );
if( ($terms[0]->name == 'Category Name') || ($post->post_name == 'custom-wp-page-slug') ){
echo '<h1>'.$top50_counter.'</h1>';
}
$top50_counter++;
}
Hope this helps.

Woocommerce product images replaced with a gallery plugin

First I would like to analyze my problem. Using Wordpress/Woocommerce I need to add videos beside images in the gallery of the product. Woocommerce does not support videos at all.
So, I thought to install an extra gallery plugin that supports both images and videos.
Now, I want to map a specific image/video gallery collection to a specific product. I want also to view this gallery collection in a new region that is not belong to the standard text fields like description or short description. Lets say above of the main product image. The php code that represent the gallery collection id=1 looks like below :
<?php echo do_shortcode('[wonderplugin_gallery id="1"]'); ?>
The problem is that I need the gallery collection id to be variable, something like this :
<?php echo do_shortcode('[wonderplugin_gallery id="X"]'); ?>
where X represnt the specific gallery collection. How the heck can I connect the gallery collection ID XXXX to my Product page XXXXX?
I have programming skills but I am new to the wordpress code logic.
Any other suggestions to my problem like plugins that may replace the default product gallery with better one ?
Regards,
I'd either use the product custom fields as Anand suggested, or create a metabox with the necessary input fields (or dropdowns depending on how you use the gallery plugin).
First I'd create a metabox, and in that metabox I'd pull the info from the plugin (gallery id's and names). Out of that you can create a dropdown. You should be able to select the id from that metabox for each product like you suggested. For instance something like this could work:
<?php
if ( ! function_exists( 'product_add_meta' ) ){
function product_add_meta(){
add_meta_box("gallery_dropdown", "Select Gallery", "product_gallery_meta_box", "product");
}
}
add_action("admin_init", "product_add_meta");
if ( ! function_exists( 'product_gallery_meta_box' ) ){
function product_gallery_meta_box( $post ){
$post_types = array('product'); //limit meta box to certain post types
global $post;
$product = get_product( $post->ID );
$values = get_post_custom( $post->ID );
$gallery = (isset($values['gallery'][0])) ? $values['gallery'][0] : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<select name="gallery" id="gallery">
//example of how the option should look
<option value="<?php echo $gallery_id; ?>" <?php selected( $gallery, $gallery_id ); ?>><?php echo $gallery_name; ?></option>
<?php
//pull options from plugin here and create an option dropdown with foreach
?>
</select>
</p>
<?php
}
}
if ( ! function_exists( 'product_gallery_save_meta_box' ) ){
function product_gallery_save_meta_box( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
if( !isset( $_POST['gallery'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) {
return;
}
if( !current_user_can( 'edit_pages' ) ) {
return;
}
if( isset( $_POST['gallery'] ) ){
update_post_meta( $post_id, 'gallery', wp_kses( $_POST['gallery'] ,'') );
}
}
}
add_action( 'save_post', 'product_gallery_save_meta_box' );
If you put this in the functions.php, it should show a metabox called 'Select Gallery' with an empty dropdown on your woocommerce product page.
I haven't filled the options that you get from the plugin with which you create your galleries, but it shouldn't be too hard.
One way is to bind the product page id and gallery id. If you can change the id of a gallery then change it to match the id of the product page. Now you can create shortcode with any of these two examples.
// outside the loop use global ( uncomment appropriate statement )
// global $product;
// global $post;
do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', $product->id ) );
do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', $post->ID ) );
HERE is a link for plugin that reveals most of the IDs on admin pages.
Another is to create Custom Field ( post meta ) in edit product admin page ( named gallery_id for example ), and to save there id of the gallery to use. To create shortcode use get_post_meta() function that retrieves the saved post meta.
do_shortcode( sprintf( '[wonderplugin_gallery id="%d"]', get_post_meta( $post->ID, 'gallery_id', true ) ) );
To get the gallery id meta use $post->ID, $product->id, or get_the_ID() function, the latter only inside the loop.

wordpress woocommerce: how to display product custom attribute on category page

i am using woocommerce theme. created some products with custom attribute Qun
i want to display Qun on ca tegorypage below the product image.
is there any solution for that? have a look at the attached image.
i want to display that custom attribute on category page.
You can use while loop like this
if (have_posts()) {
while (have_posts()) {
the_post();
$key_1_values = get_post_meta( $post->ID, 'Qun' );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
}
}
Try this
$result = array_shift(woocommerce_get_product_terms($product->id, 'attr/cate_name', 'names'));

Resources