How to exclude product for coupon use [duplicate] - wordpress

This question already has an answer here:
Create a coupon programmatically since WooCommerce 3+
(1 answer)
Closed 3 months ago.
I am creating a custom coupon and trying to exclude products to use that specific coupon but when i am adding products id in that code its only adding 1 product in the exclude not others.
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$bemail= 'email#gmail.com';
$coupon = new WC_Coupon();
$coupon->set_code( substr(str_shuffle($permitted_chars), 0, 16) . '5489' );
$coupon->set_amount(5489);
$coupon->set_excluded_product_ids(1393,1324);
$coupon->set_email_restrictions($bemail);
$coupon->save();

try to pass an array of product ids in set_excluded_product_ids().
example:-
$pids = array(101, 102, 103);
$coupon->set_excluded_product_ids($pids);
Reference:- https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-coupon.php#L622

Related

Woocommerce get total (no cart) [duplicate]

This question already has answers here:
Get order totals by order id without Formatting in WooCommerce
(2 answers)
How to get WooCommerce order details
(6 answers)
Closed 9 months ago.
I have this code:
$amount = $woocommerce->cart->total;
if ((! $option['amount_min'] || $amount >= $option['amount_min']) && (! $option['amount_max'] || $amount <= $option['amount_max'])) {
This works fine when in cart.
=> How to make it work for manually (admin) created orders with a payment link? So there is technically no cart.

Adding Sequential numbers in woocommerce for each order separated by product

I have a website selling digital postbox numbers, currently only one city so I used custom order numbers plugin to change the order numbers automaticallly to sequential numbers to match the postbox numbers I can provide.
Now I'm adding 2 more cities, and I want to create a separate numbering system for them as each city's postboxes starts with the number 1200, so I will not be able to provide 2 postboxes with number 1200 for example for 2 cities if I'm using custom order numbers.
is there a solution for this?
my only idea is to create subdomains for each city with different WP installation
so simply it should be
Product A = Numbers start from 1200, +1 added for each sale
Product B = numbers start from 1200, +1 added for each sale
Product C = numbers start from 1200, +1 added for each sale
Try looking into a prefix or suffix for your order ids which will denote which city its been purchased for
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
function change_woocommerce_order_number( $order_id, $order ) {
$prefix = '#CITY-';
// you will need to get your city as a variable to pass in to priffix
$suffix = '-' . date(Y);
// You can use either one of $order->id (or) $order_id
// Both will work
return $prefix . $order->id . $suffix;
}

Get WooCommerce Total Sales Dollar Amount (revenue) [duplicate]

This question already has answers here:
Get orders total purchases amount for the day in Woocommerce
(2 answers)
Closed 4 years ago.
Is there anyway to get WooCommerce total sales dollar amount, I'm using this plugin and it's shortcode for this purpose but I'm looking for a way to get the today's total revenue amount (gross sales) without using a plugin. I'm gonna use that code inside another plugin that I'm developing.
Try this below code
$Select_Order_Details = $wpdb->get_results( "SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' )");
$Select_Order_Details = json_decode(json_encode($Select_Order_Details),true);
print_r($Select_Order_Details);exit;

Where in the database is the Woocommerce product category count stored?

I am trying to have custom count in woocommerce categories. So categories don't show if they have products that are out of stock or are marked as do not display on website.
I can't seem to change the value that displays on the categories page.
I have tried:
Changing the value in the count column of wp_term_taxonomy table.
Changing the value in the wp_termmeta of metakey product_count_product_tag
With both of these changes the count displayed doesn't seem to change.
I can't seem to find anywhere the value might be being cached.
Nor can I find any hooks that might alter the value.
So I have found at least a partial answer to my question.
The count is stored in the term_taxonomy table but when it is displaying the subcategories it shows all of them
I found a hook that allows you to change the args so you can hide empty since by default hide empty = false and pad_count = 1 so you need to set hide_empty to true and set pad_count = 0 (otherwise it adds one to the count and none of them are 0)
add_filter( 'woocommerce_product_subcategories_args','hide_subcategories_with_no_products', 10, 1 );
function hide_subcategories_with_no_products( $args ) {
$args['hide_empty'] = 1;
$args['hierarchical'] = true;
$args['pad_counts'] = 0;
return $args;
}
NOTE: for me the woocommerce_product_subcategories_hide_empty HOOK did not work. I think this may be because either it is using count(*) instead of the count column or the pad_count being set to 1

WordPress > Getting Average Value of a Numerical Custom Field for Children of A Custom Post Type with a Given Post ID

The situation:
We have a custom post type, let's call it: 'cpt_parent'
We have another custom post type, which is a child of the aformentioned post type, let's call it 'cpt_child'
Our child custom post, 'cpt_child' has a custom field, let's call it 'cpt_child_numeric'
Our custom field 'cpt_child_numeric' holds only 1 value of 5 available values, 1, 2, 3, 5
What we want to do is get the average value of 'cpt_child_numeric' for a specific post_id of cpt_parent. For instance, say 'cpt_parent' has a post id of 33. For this id, we want to 1) count all child posts (cpt_child) 2) sum the value of 'cpt_child_numeric' for each 'cpt_child' post where the parent is ID 33 3) calculate the average.
What's tripping us up is that we don't want to do this within a loop, we want to be able to make the calculation by just having the parent post_id.
More than happy to clarify if need be
Thanks in advance!
You can use wordpress custom query to get all these results like below:
$postID = ''; // your parent post id
1) For getting count of child posts
$wpdb->get_results(" SELECT COUNT( columns_name ) FROM $wpdb->posts WHERE post_parent = $postID ");
2) For getting sum of child posts
$wpdb->get_results(" SELECT SUM( columns_name ) FROM $wpdb->posts WHERE post_parent = $postID ");
3) For getting averageof child posts
$wpdb->get_results(" SELECT AVG( columns_name ) FROM $wpdb->posts WHERE post_parent = $postID ");
I hope this is what you want.
Cheers!!!!

Resources