I provide freebies using WooCommerce plugin.
I want to ask, is it possible to determine the number of downloads on the freebies on the product detail page downloads?
Use this code:
add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
function show_number_of_downloads() {
global $wpdb, $product;
if ( empty( $product->id ) ) return;
if ( $product->product_type == 'variable' ) {
$product_ids = $product->get_children();
} else {
$product_ids = array( $product->id );
}
$query = "SELECT SUM( download_count ) AS count
FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE product_id IN (".implode( ',', $product_ids ).")";
$count = $wpdb->get_var( $query );
if ( ! empty( $count ) ) {
echo '<p><strong>' . __( 'Total downloads' ) . '</strong>: ' . $count . '</p>';
}
}
Keep this code either in your theme's function.php file or any custom plugin such that you don't loose this code after upgrade of either WordPress, plugins or theme.
The result:
Related
I am looking for a way to display a specific attribute on the single Product page of WooCommerce.
I found this code in the folling topic; Shortcode that display all product attributes set for a WooCommerce product
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I can display all attributes by using the shortcode [display-attributes] but I want to display only one specific attribute (in my case the attribute is "Kunstenaar"). However I don't know how to adjust the code to make this work. Anyone who can help me out?
If you need just specific attribute, you don't have to get them all and then go through them. WC have function get_attribute() - documentation
function get_product_attributes_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$kunstenaar = $product->get_attribute( 'Kunstenaar' );
return '<div class="product-attributes"><strong>Kunstenaar</strong>: ' . $kunstenaar . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
This code works perfect to search products by title on woocommerce .. ( dashbord backend ). However, it's affecting the search on media library, When the code is active it messes up the search in Media library Wordpress.... Is it possible to change the code to target only products instead of images, etc - media library. ???? Thank you.
function __search_by_title_only( $where, &$wp_query )
{
global $wpdb,$typenow,$pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_ids = array();
$terms = explode( ' ', $_GET['s'] );
foreach ( $terms as $term ) {
if ( is_numeric( $term ) ) {
$search_ids[] = $term;
}
if ( $search_term = $_GET['s'] ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $term ) ) . '%\'';
}
}
return $where;
}
}
add_filter('posts_search', '__search_by_title_only', 500, 2);
I am facing the issue that I want to search for every product IDs including product variants in woocommerce admin. How to search product by variation id in woocommerce admin ?
can you please help me?
Thanks in advance.
Finally I have implement the above functionality by modifying the filter "posts_search".
By using bellow code you can search variable products by their variations id in woocommerce admin product search area.
add_filter( 'posts_search', 'product_search' );
function product_search( $where ) {
global $pagenow, $wpdb, $wp;
if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' != $wp->query_vars['post_type'] ) {
return $where;
}
$search_ids = array();
$terms = explode( ',', $wp->query_vars['s'] );
foreach ($terms as $term){
if (is_numeric($term)){
$post_type = get_post_type( $term );
if($post_type == 'product_variation'){
$search_ids[] = wp_get_post_parent_id($term);
}else{
$search_ids[] = $term;
}
}
// Attempt to get a SKU
$sku_to_id = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE meta_key='_sku' AND meta_value LIKE %s;", '%' . $wpdb->esc_like( wc_clean( $term ) ) . '%' ) );
$sku_to_id = array_merge( wp_list_pluck( $sku_to_id, 'ID' ), wp_list_pluck( $sku_to_id, 'post_parent' ) );
if (sizeof($sku_to_id) > 0) {
$search_ids = array_merge($search_ids, $sku_to_id);
}
}
$search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
if ( sizeof( $search_ids ) > 0 ) {
$where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
}
return $where;
}
I am building Woocommerce WordPress Site. I am adding direct download product functionality. I added in my site by using following code`
<?php
foreach( $downloads as $key => $each_download ) {
$actions = array(
'download' => array(
'url' => $each_download["file"],
'name' => __( 'Download', 'demo' )
)
);
}
if ( $actions = apply_filters( 'woocommerce_account_download_actions', $actions, $download ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
//var_dump(apply_filters( 'woocommerce_account_download_actions', $actions, $download ));
}
?> `
I am getting Download link,but I can't get counter of Downloadable product Would you like to tell me how can i Update My WordPress Downloadable product table.
add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' ); function show_number_of_downloads() {
global $wpdb, $product;
if ( empty( $product->id ) ) return;
if ( $product->product_type == 'variable' ) {
$product_ids = $product->get_children();
} else {
$product_ids = array( $product->id );
}
$query = "SELECT SUM( download_count ) AS count
FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE product_id IN (".implode( ',', $product_ids ).")";
$count = $wpdb->get_var( $query );
if ( ! empty( $count ) ) {
echo '<p><strong>' . __( 'Total downloads' ) . '</strong>: ' . $count . '</p>';
} }
Keep this code either in your theme's function.php file or any custom plugin, so that you don't lose this code after an upgrade of either WordPress, plugins or theme.
source: https://stackoverflow.com/a/39383269/6574214
I'm trying to list categories but i need to exclude couple. How can I add exclude category to the following?
$size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
echo $product->get_categories('<p>' . _n( '', '', $size, 'woocommerce' ) . '</p>' );
Many thanks!
Please take a look at:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
the below example uses is_home. But you can also use the is_page() function to check for the page you are using and then apply the pre_get_posts.
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1,-1347' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );