Support » Plugin: WooCommerce » percentage discount for product category

  • Resolved elenktik

    (@elenktik)


    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_categorymy-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&gt; 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.`

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Is it not possible to apply a percentage discount only to specific items from the cart?

    In core WooCommerce, you can create a coupon that only applies to specific items by adding categories and/or products in the usage restriction tab: https://docs.woocommerce.com/document/coupon-management/#usage-restriction

    > Percentage discount – A percentage discount for selected products only
    What are selected products?

    This is only if you define selected products in the usage restriction tab. When you leave it blank, the discount will apply to everything.

    Thread Starter elenktik

    (@elenktik)

    In core WooCommerce, you can create a coupon that only applies to specific items by adding categories and/or products in the usage restriction tab: https://docs.woocommerce.com/document/coupon-management/#usage-restriction

    I thought so too, but it didn’t work, even though I added a the product category / product id. And after reading the docs again, I realized why:

    Products – Products that the coupon will be applied to, or that need to be in the cart in order for the fixed or percentage discount to be applied.

    and

    Product categories – Product categories that the coupon will be applied to, or that need to be in the cart in order for the fixed or percentage discount to be applied.

    So in other words, if I specify product ids/categories together with percentage, then it means the coupon will be applied to whole cart if one of the restricted product id / category is in the cart.

    Plugin Support abwaita a11n

    (@abwaita)

    Hi @elenktik,

    >I thought so too, but it didn’t work, even though I added a the product category / product id.

    To get better context, when you say it didn’t work, is it that it never applied to the products you’d specified?

    >So in other words, if I specify product ids/categories together with percentage, then it means the coupon will be applied to whole cart if one of the restricted product id / category is in the cart.

    Actually, it applies only to the qualifying product(s) rather than the entire cart. Be sure that in the Usage Limits, you leave it at “Apply to all qualifying items in cart“.

    To give an example, I created a 50% off coupon that applies only to the product called Single that’s selling at $2https://snipboard.io/goRiBF.jpg
    I applied the coupon, and note that it only deducted $1 rather than half of the total cart amount – https://snipboard.io/i4zrSx.jpg
    *Thus, it only applied to the product costing $2.

    I hope this clarifies, we’ll be happy to help further.
    Thanks.

    • This reply was modified 2 years, 9 months ago by abwaita a11n. Reason: added clarifications

    Hi @elenktik

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. If you have further questions, please feel free to open a new topic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘percentage discount for product category’ is closed to new replies.