I have e-commerce website where I would like people to see view count in Woocommerce single product page.
I tried this kind of code, but it only shows 1 view on every product that is wrong number.
Can you please help me?
Thank you in advance.
Here is a code:
add_action( 'woocommerce_single_product_summary', 'add_content_func',0 );
function add_content_func() {
global $product;
$id = $product->id;
$meta = get_post_meta( $product_id, '_wcfm_product_views', true );
if(empty($meta))
{
$result = 0;
}
else
{
$result = count(explode(',',$meta));
}
echo "<div class='custom-visitor-count-st' style='font-size: 20px;'>";
echo "<i class='fa fa-eye'></i>";
echo "<span class='cv-value'>";
echo $result;
echo " Views</span></div>";
}
P.S. I am using WCFM Marketplace. That's why I used this kind of meta to get product views: get_post_meta( $product_id, '_wcfm_product_views', true );
Related
I'm using "Search and filter" plugin, and "WP Favorites" plugin. I'm using following code in custom template
if ($query->have_posts())
{
while ($query->have_posts())
{
$query->the_post();
$meta_values = get_post_meta( get_the_ID() );
$post_meta_value = get_post_meta( get_the_ID(), '_audioigniter_tracks',true );
foreach($post_meta_value as $name) {
$kjkj = get_the_ID();
echo do_shortcode("[favorite_button post_id='$kjkj']");?>
<a class="a" href="<?php echo $name['track_url']?>"><?php the_title() ?></a><br/><?php
}
}
}
While in functions.php I'm using following code to sort but it's not working as I'm missing something related to user favorite posts meta
function myfunctions( $query, $sfid ) {
//global $wpdb, $query, $wp;
if($sfid==133203)
{
$favs = get_user_favorites();
foreach($post_meta_valuee as $fst) {
$query['order'] = 'DESC';
$query['orderby'] = 'meta_value';
$query['meta_key'] = 'simplefavorites_count';
}
return $query;
}
add_filter( 'sf_edit_query_args', 'myfunctions', 20, 2 );
Problem is in functions.php code, I'm somehow not able to let the query know info about users favorites (simplefavorites_count is total count by all users, not by current user). So I need help here.
Hello I am creating a woocommerce wordpress website and I'm trying to replace the default reviews display with our own integration from Bazaarvoice. so I calledback the reviews tab to display my own function that gets the model number of the product and puts it into the code to display the bazaarvoice review display. The code works except that global $product doesn't work because I don't really know how to use hooks. I know the add_action for function bv_reviews_tab isn't correct so I'm not really sure what I am supposed to be putting there. I have been racking my head all day
add_filter( 'woocommerce_product_tabs', 'woo_renam_tabs', 98 );
function woo_renam_tabs( $tabs ) {
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
$tabs['additional_information']['callback'] = 'woocommerce_product_additional_information_tab';
$tabs['reviews']['callback'] = 'bv_reviews_tab';
return $tabs;
}
add_action( 'woocommerce_product_tabs', ' bv_reviews_tab' );
function bv_reviews_tab( ) {
global $product;
if ( $product->is_type('simple' ) ) {
$model = esc_attr( get_post_meta( $product->get_id(), '_model_number', true ) );
}
elseif ( $product->is_type('variable' ) ) {
// Get childIDs in an array
$children_ids = $product->get_children();
// Loop
foreach ( $children_ids as $child_id ) {
// Get product
$product = wc_get_product( $child_id );
$model = esc_attr( get_post_meta( $child_id, '_model_number', true ) );
}
}
?>
<div
data-bv-show="reviews"
data-bv-product-id= "<?php echo $model; ?>"
>
</div>
<?php
}
There is no need of add_action in this case. The callback value given in the woo_renam_tabs function will trigger your custom function. Remove that add_action line and you should be good to go. global $product will also work.
Also, you will need to give a title to the reviews tab and ensure reviews tab is enabled in the WooCommerce >Settings > Products
This is the code that I have tested and is working:
add_filter( 'woocommerce_product_tabs', 'woo_renam_tabs', 98 );
function woo_renam_tabs( $tabs ) {
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
$tabs['additional_information']['callback'] = 'woocommerce_product_additional_information_tab';
$tabs['reviews']['title'] = __( 'Reviews' ); ;
$tabs['reviews']['callback'] = 'bv_reviews_tab';
return $tabs;
}
function bv_reviews_tab() {
global $product;
?>
<div data-bv-show="reviews">
<?php echo $product->get_name();?>
</div>
<?php
}
I'm searching for an option to show the meta description of a Woocommerce product under the title in the product category overview. The code below in the functions.php of the theme repeat the SKU what the title is of the product. Wondering how to replace this with the meta description:
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_sku_in_cart', 20, 1);
function woocommerce_after_shop_loop_item_sku_in_cart( $template ) {
global $product;
$sku = $product->get_sku();
echo $sku;
}
Thanks
You can access a short description using post->post_excerpt. check below code.
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_sku_in_cart', 20, 1);
function woocommerce_after_shop_loop_item_sku_in_cart( $template ) {
global $product;
$sku = $product->get_sku();
echo $sku;
echo "</br>";
echo get_post_meta( $product->get_id(), '_yoast_wpseo_metadesc', true );
}
I am using a function to add custom meta to products. I have used the following hooks for SHOW on Product Loop woocommerce_after_shop_loop_item and Product Single Page woocommerce_product_meta_end.
So, when applying to get the same results on CART PAGE product/item by using the hook
woocommerce_after_cart_item_name it doesn’t work with no results.
Why isn’t the hook woocommerce_after_cart_item_name working if it works with the other previous hooks mentioned?
This the code I am using. I just change the hook to make it to show in Product Loop and Product Single Page, need it to show on cart Products as well. I was just wondering why it doesn't work with cart item hook..
public function woocommerce_after_cart_item_name()
{
global $product;
if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
return;
$product_id = $product->get_id();
$season = get_post_meta( $product_id, 'season', true );
$car_type = get_post_meta( $product_id, 'car_type', true );
$tips_seasons = $this->ui_tips_season();
$tips_car_types = $this->ui_tips_car_types();
?>
<div class="tyre-details tyre-tips">
<?php if ( PluginOptions::value('shop_season') && $season ): ?>
<?php echo $tips_seasons[ strtolower($season) ]; ?>
<?php endif ?>
<?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
<?php echo $tips_car_types[ strtolower($car_type) ]; ?>
<?php endif ?>
</div>
<?php
}
It is from a plugin. I was just given this code from woocommerce support but i do not know how to complete it. He says to replace with my meta_keys in the code which I will post below here. Can you help me finish this? or tell me where I need to replace. My meta_keys are $season and $car-type but i don't know how to apply with the code provided by woocommerce.
// Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
$meta_key = 'PR CODE';
if ( isset($cart_item['add_size']) && isset($cart_item['add_size'] [$meta_key]) ) {
$item_data[] = array(
'key' => $meta_key,
'value' => $cart_item['add_size'][$meta_key],
);
}
return $item_data;
}
// Save cart item custom meta as order item meta data and display it everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
$item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}
I located the ClassName (class FeatureTyreTips) and added that to the code you provided. I entered this in functions.php and it said fatal error when loading the cart page. I also tried placing it in the cart.php with same results...and I tried in the same plugin file where this code came from originally. All 3 locations did not work...only difference was that it did not show fatal error when adding and activating your new code in the plugin file when loading cart page. Any ideas? grazie Vdgeatano
The "Fatal Error" is most likely due to the fact that "the non-static method cannot be called statically": PHP - Static methods
I have now corrected the code.
Considering that the class name that was missing in your code is FeatureTyreTips, the correct code to add some text after the product name is:
add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
// create an instance of the "PluginOptions" class
$PluginOptions = new PluginOptions();
if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
return;
}
$product = $cart_item['data'];
$season = get_post_meta( $product->get_id(), 'season', true );
$car_type = get_post_meta( $product->get_id(), 'car_type', true );
// create an instance of the "FeatureTyreTips" class
$FeatureTyreTips = new FeatureTyreTips();
$tips_seasons = $FeatureTyreTips->ui_tips_season();
$tips_car_types = $FeatureTyreTips->ui_tips_car_types();
if ( ! $PluginOptions->value('shop_season') || ! $season ) {
$season = '';
}
if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
$car_type = '';
}
$html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
echo $html;
}
The code has been tested (where possible) and works.It needs to be added to your theme's functions.php file or in your plugin.
I am working on a project in woocommerce.
Can anyone help me to add a custom text field next to "add to cart"?
I tried the following but it is not working.
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post);
echo "</div>";
return true;
}
Perhaps try using $post->ID like this:
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post->ID);
echo "</div>";
return true;
}
put your html code inside content-product.php (wp-content/themes/YOUR THEME/woocommerce/content-product.php)
content-product.php contains code of list page in wordpress. if you want to edit product page than edit content-single-product.php.
You are using Woocommerce there are two case :
You are reading post ID
You want to display a SKU
You can implement it via single function :
add_action( 'woocommerce_single_product_summary', 'display_productCode', 5 );
function display_productCode(){
global $product;
echo 'SKU: ' . $product->get_sku();
echo 'Product ID:'.$product->get_id();
}