woocommerce get product id when product is add to cart - wordpress

I need to do extra step in the backend when a product is add to cart.
I need to get the product ID juste after it's add to the cart.
I use the woocommerce hook woocommerce_add_to_cart
add_action('woocommerce_add_to_cart', 'attach_item');
function attach_item() {
// I need to have the product id here.
}
I tried many way to get the ID but nothing work.
Any idea ...

Today I also get the same problem but I got the solution.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart',10,2 );
function woo_custom_add_to_cart( $cart_item_data,$productId ) {
var_dump($productId);
}

At the time of writing this, the hook is actually called from WooCommerce like so: (source)
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Add your custom method like so:
add_action('woocommerce_add_to_cart', function($cartItemKey, $productId) {
// do sth with $productId
}, 10, 2);

Related

Remove simple product from product data woocommerce

How can we remove simple product and variable product from product data dropdown in woocommerce.
You can use product_type_selector filter hook and unset to remove simple product type. check below code. code will go into the active theme functions.php file.
add_filter( 'product_type_selector', 'remove_product_types_simple' );
function remove_product_types_simple( $types ){
unset( $types['simple'] );
return $types;
}

woocommerce how to hook into checkout page

I would like to add additional information / update the checkout table in woocommerce. This is the table i mean:
I would like to edit the quantity and the title of the product.
I have had a look into woocommerce hooks and came across the following:
woocommerce_checkout_cart_item_quantit
So i made a test function to have a play:
function add_meta_on_checkout_order_review_item( $quantity , $cart_item , $cart_item_key ) {
echo $cart_item[ 'quantity' ] + 5;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 1, 3 );
This however has no effect. Am i hooking into the right woocommerce hook?
Try changing this: add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 1, 3 );
To this : add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 10, 3 );
You can also try to return instead of echo the quantity.
Maybe I am little late here but you can try this:-
add_filter('woocommerce_cart_item_name','add_meta_on_checkout_order_review_item',10,3);
And this would surely help to add the meta data under the Title of the product.
Thanks,

Hook a function based on adding a particular product in Woo

I need to create an action hook based on a particular product being added to the Woocommerce cart:
When PRODUCT-#123 is added to the cart, then doThisFunction();
Any ideas how to do this?
Add the following code to your theme's functions.php file
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );
function custom_add_to_cart( $cart_item_key, $product_id ) {
// replace 123 with a valid product ID
if( 123 == $product_id ) {
//call the desired function
}
}

how to disable cart functionality from woocommerce?

How to disable cart functionality from woocommerce store. I want user can only see products available.customer can not purchase from store.
the easiest way is make the products not purchasable..
add_filter( 'woocommerce_is_purchasable','__return_false',10,2);
To fully disable woocommerce purchase functionality:
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
More option you can find here: https://react2wp.com/remove-hide-add-to-cart-button-in-woocommerce-while-disabling-keeping-purchase-functionality/
If you need conditions, you can use the following code:
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
// Conditions here.
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
For example, if you need to check users:
// Disable purchase for non-logged-in users.
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

WooCommerce Delete all products from cart and add current product to cart

I am new to WooCommerce and I need to be able to only add one single product in the cart. I want to clear all products and add the current product to the cart when I click the "Add to cart" button.
How can I do that ?
I have got an exact solution for this.
Try following code.
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
The above filter is defined in class-wc-cart.php within function add_to_cart().
http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905
Thus, when add to cart button is pressed, it empties the cart and then add the product.
Try this,
//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);
class file is wp-content/plugins/woocommerce/classes/class-wc-cart.php
Hope its helps..

Resources