Require registration in WooCommerce for specific product - php

It's straightforward to required registration globally in WooCommerce settings but for most of the products we sell, it's not necessary and I would rather restrict the logged in users.
However for one product I would like to require registration.
Something like
$product_id = $product->get_id();
if ($product_id !== 1024) {
remove_action( 'woocommerce_before_checkout_registration_form', 'action_woocommerce_checkout_registration_form', 10, 1 );
}
but it's obviously not that simple. Any ideas?

This answer assumes that WooCommerce > Settings > Accounts & privacy > Allow customers to place orders without an account is enabled
You can then use the following code, that will ensure that when there is 1 or more productID's in the shopping cart, registration during checkout will be required
function filter_woocommerce_checkout_registration_required( $bool_value ) {
// Several can be added, separated by a comma
$product_ids = array ( 30, 813 );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Product ID from cart item in specific array
if ( in_array( $cart_item['product_id'], $product_ids ) ) {
// Registration_required
$bool_value = true;
// Break loop
break;
}
}
return $bool_value;
}
add_filter( 'woocommerce_checkout_registration_required', 'filter_woocommerce_checkout_registration_required', 10, 1 );

Related

WooCommerce - Enable this to only allow one of this item to be bought in a single order

I tried to enable this option for a few items in the store, But It doesn’t work.
Option under Inventory tab - Enable this to only allow one of this item to be bought in a single order
I am still able to add other items in Cart.
PS: Sold individual means that multiple quantities of that product cannot be purchased in a single order. Other products can be purchased along with it.
add_filter( 'woocommerce_add_to_cart_validation', 'remove_all_other_if_individual' );
function remove_all_other_if_individual( $cart_item_data ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$is_sold_individual_incart = false;
foreach ( $items as $item => $values ) {
$_product = wc_get_product( $values[ 'data' ]->get_id() );
if ( $_product->is_sold_individually() )
$is_sold_individual_incart = TRUE;
}
if ( $is_sold_individual_incart )
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}

Allow specific coupons when there is backordered items in Woocommerce

Is it possible to allow a certain woocommerce coupon only on backorders?
I've tried to use If WooCommerce Cart items are on backorder do not apply coupon answer code that restricts backordered products from using any coupons.
I have changed if( $stock_info < 1 ){ to if( $stock_info > 0 ){ instead
But then I can only use coupons on backorders… but where do I make this work on a certain coupon?
May I use its ID? but where?
The following code will make specific defined coupons codes valid when there is backordered items in cart:
add_filter( 'woocommerce_coupon_is_valid', 'specific_coupons_valid_for_backorders', 10, 3 );
function specific_coupons_valid_for_backorders( $is_valid, $coupon, $discount ){
// HERE below define in the array your coupon codes
$coupon_codes = array( 'summer', 'tenpercent' );
if( in_array( $coupon->get_code(), $coupon_codes ) ) {
$is_valid = false;
// Loop through cart items and check for backordered items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$is_valid = true; // Backorder found, coupon is valid
break; // Stop and exit from the loop
}
}
}
return $is_valid;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Set a custom cart item price value from a GET variable In Woocommerce 3

I have a function on my Woocommerce website that enables customers to set a custom amount to pay for a specific product, based on a value I'm passing through the URL.
I'm using the woocommerce_before_calculate_totals hook, and up until I upgraded to WC 3.3.5, it was working fine. Now, when I run the code, the checkout initially shows the custom amount.
However, once the loader has finished updating, it resets the price to '0' (i.e. displaying £0.00 the checkout page's total fields).
Here's that code:
add_action( 'woocommerce_before_calculate_totals', 'pay_custom_amount', 99);
function pay_custom_amount() {
$payment_value = $_GET['amount'];
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == 21 ){
$cart_item['data']->set_price($payment_value);
}
}
}
Well, colour me baffled. I've trawled Stack Overflow for solutions but can't see any similar problems. I see the hook runs multiple times but gather this is normal.
If anyone know what might be happening here, it would be great if you could share.
You can't get a price from an URL and set it in woocommerce_before_calculate_totals action hook. This needs to be done differently.
In the below code:
the first hooked function will get that "amount" from the URl and will set it (register it) in cart item object as custom data.
the 2nd hooked function will read that amount from custom cart item data and will set it as the new price.
Now your the target product ID in your code need to be the same ID that the added to cart product.
The code:
// Get the custom "amount" from URL and save it as custom data to the cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_pack_data_to_cart_item_data', 20, 2 );
function add_pack_data_to_cart_item_data( $cart_item_data, $product_id ){
if( ! isset($_GET['amount']) )
return $cart_item_data;
$amount = esc_attr( $_GET['amount'] );
if( empty($amount) )
return $cart_item_data;
// Set the custom amount in cart object
$cart_item_data['custom_price'] = (float) $amount;
$cart_item_data['unique_key'] = md5( microtime() . rand() ); // Make each item unique
return $cart_item_data;
}
// Alter conditionally cart item price based on product ID and custom registered "amount"
add_action( 'woocommerce_before_calculate_totals', 'change_conditionally_cart_item_price', 30, 1 );
function change_conditionally_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your targeted product ID
$targeted_product_id = 21;
foreach ( $cart->get_cart() as $cart_item ) {
// Checking for the targeted product ID and the registered "amount" cart item custom data to set the new price
if($cart_item['data']->get_id() == $targeted_product_id && isset($cart_item['custom_price']) )
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Woocommerce - Show cart even if it's empty

My WordPresss related task is simple, but I can't find the solution. I have 2 products in my woocommerce shop, and I would like to show both of them on the cart-page in the woocommerce table, let the customer set the quantity of them. If the customer don't want to buy something, just leave it on 0.
The problem is the cart table is not shown if it is empty and I can only see the items that I put in there.
you can add product like that and add condition according you i set for admin and product id. change product id with your product id
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}

Sending Two Simple Product To Add To Cart In Single Woocommerce Button Click

I am adding two different products in my website and they both combines together to form one single product, what i am trying to achieve is that when i click on the add to cart button, I want to send two product id's to "add to cart" which means two different products, one is the page from where the a"dd to cart" is clicked and the second one is manual product id which i've assigned to hidden text box.
I tried the ajax call but that didn't work. Also I don't want to go with woocommerce paid extensions for bundle products and other types.
So it would be great if i can make some changes to add to cart.
Try something simple, like "Woocommerce Free Gift" plugin, there is a free version as well. You can setup automatic product add to cart on certain circumstances.
Otherwise, you need to modify your functions.php, whenever you are adding "product A" -> add "product B". Something likes this:
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = ID_OF_PRODUCT_A;
$product_b = ID_OF_PRODUCT_B;
$found = false;
if( $woocommerce->cart->total > 0 ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( $found )
$woocommerce->cart->add_to_cart( $product_b );
} else {
// do something
}
}
}
}

Categories