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
Related
I want to remove item from cart via ajax and I'm using Easy Digital Downloads plugin so is there any clue for that.
I already found below code but don't know how to use it as I made customizations to my custom theme by overriding templates.
function edd_ajax_remove_from_cart() {
if ( ! isset( $_POST['nonce'] ) ) {
edd_debug_log( __( 'Missing nonce when removing an item from the cart. Please read the following for more information: https://easydigitaldownloads.com/development/2018/07/05/important-update-to-ajax-requests-in-easy-digital-downloads-2-9-4', 'easy-digital-downloads' ), true );
}
if ( isset( $_POST['cart_item'] ) && isset( $_POST['nonce'] ) ) {
$cart_item = absint( $_POST['cart_item'] );
$nonce = sanitize_text_field( $_POST['nonce'] );
$nonce_verified = wp_verify_nonce( $nonce, 'edd-remove-cart-widget-item' );
if ( false === $nonce_verified ) {
$return = array( 'removed' => 0 );
} else {
edd_remove_from_cart( $cart_item );
$return = array(
'removed' => 1,
'subtotal' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_subtotal() ) ), ENT_COMPAT, 'UTF-8' ),
'total' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_get_cart_total() ) ), ENT_COMPAT, 'UTF-8' ),
'cart_quantity' => html_entity_decode( edd_get_cart_quantity() ),
);
if ( edd_use_taxes() ) {
$cart_tax = (float) edd_get_cart_tax();
$return['tax'] = html_entity_decode( edd_currency_filter( edd_format_amount( $cart_tax ) ), ENT_COMPAT, 'UTF-8' );
}
}
$return = apply_filters( 'edd_ajax_remove_from_cart_response', $return );
echo json_encode( $return );
}
edd_die();
}
Here is my Current delete item code inside overrided template inside edd_templates/checkout_cart.php file.
<a class="edd_cart_remove_item_btn" href="<?php echo esc_url( wp_nonce_url( edd_remove_item_url( $key ), 'edd-remove-from-cart-' . sanitize_key( $key ), 'edd_remove_from_cart_nonce' ) ); ?>"></a>
But it refresh cart page when I click on x(delete) in cart but I want to remove item using ajax any help will be appreciated.
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' );
can you please tell me the coding to include recently viewed products in WooCommerce
Try adding below snippet where you want to display
<div>
<?php
echo do_shortcode("[woocommerce_recently_viewed_products per_page='5']");
?>
</div>
Add below code to functions.php of your active theme
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// Get WooCommerce Global
global $woocommerce;
// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
// If no data, quit
if ( empty( $viewed_products ) )
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
// Create the object
ob_start();
// Get products per page
if( !isset( $per_page ) ? $number = 5 : $number = $per_page )
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ( $r->have_posts() ) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ( $r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products");
I'm working on displaying the order products in the View Orders page in the customer account page like this
Can we do this?
Add this to your theme's 'functions.php'.
function add_products_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add products after order total column
if ( 'order-total' === $key ) {
$new_columns['order-products'] = __( 'Products', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );
function add_products_data_my_account_orders_column( $order ) {
//loop through products of the order
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$is_visible = $product && $product->is_visible();
$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p>%s', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );
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: