How to add a coupon code to cart?

How to add a coupon code to cart?

Create a coupon code that you want to apply once a certain product is added to cart (go to WooCommerce / Coupons / Add New and decide your coupon code. For example “freeweek”, which is the coupon code we will use later in the PHP snippet)

How to add link that applies coupon code to a Shopify cart?

{ { event.extra.checkout_url }}&discount= {% coupon_code ‘CouponName’ %} You may want to apply a static discount code to a shopping session, but not link the customer directly back to their cart. Instead, you may want to send them to your homepage or a collection page.

How to add a coupon code to a link?

To achieve this functionality, you will need to add a parameter to a link or button. When your customer clicks the button they will be taken to the link and the discount will be automatically applied to their next cart. You can only add one discount code per email. You can use the same code in multiple places, but you cannot use multiple codes.

How to apply coupon programmatically on WooCommerce?

Users can manually enter a coupon code, refresh the Cart and see their discount apply… or you can do that automatically (or “programmatically” as we say in the dark web) when a user adds a product to the WooCommerce Cart All you’ve got to do is creating a coupon, and then a PHP function will do the whole work.

How to apply a coupon programmatically in PHP?

So far I’ve modified cart.php to get the total weight of all the products in the cart, I’ve created a coupon that applies the discount (if entered manually) and I’ve added some code to functions.php to check the weight and display a message to the user. EDIT: Partial code removed, completed code included in the solution below.

How to calculate discounts based on cart weight?

If you do not do this, a refresh is required before user will see updated totals when discount is removed. $woocommerce->cart->calculate_totals (); } } } } The following compact code in one hooked function will add a discount (coupon) based on cart total weight displaying a custom message.