percentage discount for product category
-
I am using WordPress together with WooCommerce Subscriptions. A user can buy subscriptions at my shop. I want that the user gets a dynamic percentages only for subscriptions of a specific product category.
I managed to create a custom coupon that with an indivual percentage discount. Here is a part from my code:
$coupon = $this->createOrGetCoupon($coupon_code); if (!in_array($coupon_code, WC()->cart->get_applied_coupons())) { WC()->cart->apply_coupon($coupon_code); } } private function createOrGetCoupon(string $coupon_code) { if($this->couponExistInDatabase($coupon_code)){ $coupon = new WC_Coupon($coupon_code); return $coupon; } $coupon_args = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); // create post type and save the ID $coupon_id = wp_insert_post( $coupon_args ); update_post_meta( $coupon_id, 'discount_type', 'percent' ); update_post_meta( $coupon_id, 'coupon_amount', $this->getPercentage($coupon_code) ); update_post_meta( $coupon_id, 'product_categories', 'my-special-groups' ); }
I want that the fresh created coupon is only applied to items with product_category
my-special-groups`. But it turns out its applied to all items in the cart.I found that one can create fixed price reduction coupons either for the cart or for specific items (that one can filter by product category). But is it true that the percentage discount coupon only works for the whole cart?
From the <a href=”http://%20https://docs.woocommerce.com/document/coupon-management/”>docs</a> it states:
> Percentage discount – A percentage discount for selected products only
What are selected products?
Is it not possible to apply a percentage discount only to specific items from the cart? If not, what route do I need to go to fake the coupon functionality for dynamic prices? The discount should also only apply on first purchase. Not on renewal.`
- The topic ‘percentage discount for product category’ is closed to new replies.