Add specific product to cart if coupon is applied WooCommerce - php

I want to build the following:
A landingspage with a Apply Coupon field.
If the coupon is valid it needs to add a specific product to the cart.
So basically the coupon needs to be attached to a specific product. (in this case product_id 99.
I found this somewhere:
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'JLSDFO' === $coupon_code ) {
$product_id = 99;
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
But this is for one coupon specific. I want to add for example 1000 coupons which are merged with this product. The customer can select this product on the back-end while adding this coupons. So when the user enters one of the 1000 coupons it understands to add for example product_id 99 to the cart.

Related

Hide WooCommerce sale prices for logged-out user

I'm using the WooCommerce product table plugin by Barn2, this plugin allows you to add custom columns so I added a custom column to display "SALE PRICE" in a separate column using _sale_price custom field. The sale price gets displayed and I want to hide that sale price column data for non-logged-in customers, although regular prices are hidden for non-logged-in customers using the Wholesale pro plugin(By Barn2) Now, I'm struggling to find a solution to hide the sale price column for non-logged-in customers. I attempted this code:
add_filter( 'woocommerce_product_get_sale_price', function( $price ) {
if ( ! is_user_logged_in() ) {
return '';
}
return $price; // Return original price
} );

Is there a possibility to make custom prices?

I'm selling Gift Cards via WooCommerce on Wordpress. My customer should be able to set a value for the gift card amount by himself. I just was able to do this via a plugin. Is there a possibilty to do this by changing some code or via functions.php?
Installed Pimwick Gift Card Pro
Yes, but it's a fairly complex process if doing so from a completely fresh WooCommerce installation with no additional plugins. You will need to do the following things to achieve it:
Add a custom input field to the product to add the custom price
Save the data from the custom input field to the session (cart) when that product is added to the cart
Add the cart meta (created above in #2) to the order when the order is created
Adjust the cost of the product based on the custom price meta (added above in #3).
Step 1: Add a custom input field:
You can add the input field using the woocommerce_before_add_to_cart_button filter as shown below.
Alternatively you can use the woocommerce_wp_text_input - here's an example.
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_price_input', 100 );
function add_custom_price_input() {
if(get_the_ID() != 123) { //use the product ID of your gift card here, otherwise all products will get this additional field
return;
}
echo '<input type="number" min="50" placeholder="50" name="so_57140247_price">';
}
Step 2: Save custom price to Cart/Session
Next, we need to make sure that your custom input field data is carried over to the cart/session data. We can make use of the woocommerce_add_cart_item_data ( docs | example ) filter to that:
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_meta_to_cart', 10, 3 );
function add_custom_meta_to_cart( $cart_item_data, $product_id, $variation_id ) {
$custom_price = intval(filter_input( INPUT_POST, 'so_57140247_price' ));
if ( !empty( $custom_price ) && $product_id == 123 ) { //check that the custom_price variable is set, and that the product is your gift card
$cart_item_data['so_57140247_price'] = $custom_price; //this will add your custom price data to the cart item data
}
return $cart_item_data;
}
Step 3: Add cart meta to the order
Next we have to add the meta from the cart/session to the order itself so that it can be used in the order-total calculations. We use the woocommerce_checkout_create_order_line_item ( docs | example ) to do this:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_meta_to_order', 10, 4 );
function add_custom_meta_to_order( $item, $cart_item_key, $values, $order ) {
//check if our custom meta was set on the line item of inside the cart/session
if ( !empty( $values['so_57140247_price'] ) ) {
$item->add_meta_data( '_custom_price', $values['so_57140247_price'] ); //add the value to order line item
}
return;
}
Step 4: Adjust total of gift card line item
Finally, we simply adjust the cost of the line item of the gift card based on the value entered into the input field. We can hook into woocommerce_before_calculate_totals ( docs | example ) to do this.
add_action( 'woocommerce_before_calculate_totals', 'calculate_cost_custom', 10, 1);
function calculate_cost_custom( $cart_obj ) {
foreach ( $cart_obj->get_cart() as $key => $value ) {
$price = intval($value['_custom_price']);
$value['data']->set_price( $price );
}
}

Woocommerce add free products with ACF Post Object field

I am trying to build a custom functionality in Woocommerce with Advanced Custom Fields (ACF) plugin.
I created some code already but it is not working properly.
I want to select (simple) products at a Woocommerce product with ACF post object field. That products must be added free into the Woocommerce cart when that product will be added to the cart. Also when I add two or more items the free products must be added together.
I got also some images how the situation should work.
This is the ACF field setup.
For example: this is the product we give away.
This is the product we give away selected at a Woocommerce single product.
This is my actual code:
add_action('woocommerce_check_cart_items', 'free_products' );
function free_products() {
if( ( is_cart() || is_checkout () ) {
$free_product = get_field('gratis_producten'); // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product );
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
if( $free_product ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart 40 free products
WC()->cart->add_to_cart( $free_product, 40 );
}
}
}
Any help will be highly appreciated.

Allow add to cart for specific products based on cart total in WooCommerce

In woocommerce, I am trying to find a way to only allow a product to be added a to cart only when a specific cart total amount is reached.
Example: We want to sell a bumper sticker for $1, but only if a user already has $25 worth of other products already in the cart. This is similar to Amazon's "add on" feature. However I can't find a similar WooCommerce plugin or function.
I have tried yet some code without success… Any help will be appreciated.
Can be done with a custom function hooked in woocommerce_add_to_cart_validation filter hook, where you will define:
a product Id (or multiple product Ids).
the threshold cart amount to be reached.
It will avoid for those defined products to be added to cart (displaying a custom notice) until that specific cart amount is reached.
The code:
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {
// HERE define one or many products IDs in this array
$products_ids = array( 37, 27 );
// HERE define the minimal cart amount that need to be reached
$amount_threshold = 25;
// Total amount of items in the cart after discounts
$cart_amount = WC()->cart->get_cart_contents_total();
// The condition
if( $cart_amount < $amount_threshold && in_array( $product_id, $products_ids ) ){
$passed = false;
$text_notice = __( "Cart amount need to be up to $25 in order to add this product", "woocommerce" );
wc_add_notice( $text_notice, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.

WooCommerce - Getting a Free product for one bought promotion for selected products

I have a WooCommerce web site. I would like to add a promotion like buy 1 and Get 1 Free for some selected products.
How can I do this?
Thanks
Yes it's possible without a plugin. There is different ways but the best and easy one is that one… Based on product category and on auto-applied coupon of 50% (easy and better).
1) First create a product category in backend Products > categories, for example 'two4one'.
Set this category to all the products you want to have this promotional plan (2 for one)
2)Creating a special coupon in woocommerce (to be auto-applied):
In woocommerce create a coupon that you will name for example '2for1'. You will make this special settings to it:
General > Discount type: Product % Discount
General > Coupon Amount: 50 (this is a percent see line above)
Usage Restriction > Individual use only: enabled
Usage Restriction > Product categories: two4one (add your special product category here)
So this coupon restrict the discount only to two4one category products. If all products with two4one category are removed from cart, the coupon will be removed too.
3) The code: Once the coupon is created and correctly set, you can use this hooked functions code:
// Add to Cart 2 products at the same time of the "two4one" product category
add_action( 'woocommerce_add_to_cart', 'add_to_cart_qty_by_two', 10 );
function add_to_cart_qty_by_two($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
// When a product has 'two4one' as category increment quantity by one (2 products)
if( has_term( 'two4one', 'product_cat', $product_id ) ) {
$quantity += $quantity;
WC()->cart->set_quantity($cart_item_key, $quantity);
}
}
// Auto applying coupon if products with two4one" category are in cart
add_action( 'woocommerce_before_calculate_totals', 'auto_add_a_coupon_discount', 10 );
function auto_add_a_coupon_discount( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $item ) {
// When a product has 'two4one' as category auto apply coupon '2for1'.
if( has_term( 'two4one', 'product_cat', $item["product_id"] ) && !$cart_object->has_discount('2for1') )
WC()->cart->add_discount('2for1');
}
}
// If customer discrease or increse quantity it will be restored to an even number on checkout
add_action( 'woocommerce_before_checkout_form', 'checking_promotional_products', 10 );
function checking_promotional_products() {
foreach ( WC()->cart->cart_contents as $item_key => $item ) {
// if it's a promo product category and quantity is an even number
if( has_term( 'two4one', 'product_cat', $item["product_id"] ) && $item["quantity"] % 2 != 0 ) {
// checking that item quantity is always an even number (if not adds 1)
$quantity = $item["quantity"] + 1;
WC()->cart->set_quantity($item_key, $quantity);
}
}
}
This code goes on function.php file of your active child theme (or theme) or in any plugin file.

Categories