Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wooexperte/3ce6aa157a24885cb6cbe78e45c84b2e to your computer and use it in GitHub Desktop.
Save wooexperte/3ce6aa157a24885cb6cbe78e45c84b2e to your computer and use it in GitHub Desktop.
WooCommerce - Produkt A beim Kauf von Produkt B automatisch im Warenkorb hinzufügen
add_action( 'woocommerce_add_cart_item_data', function( $cart_item_data, $product_id ) {
// ID austauschen. Wenn Produkt A mit ID 54 im Warenkorb liegt, kommt Produkt B mit ID 56 automatisch im Warenkorb hinzu.
if ( 54 == $product_id ) {
WC()->cart->add_to_cart( 56 );
}
}, 10, 2 );
@MrThiemann
Copy link

does that work with categories?
I tried to use the "$ product_id" code with "$ term_ids", but that does not work.

`
add_action( 'woocommerce_add_cart_item_data', function( $cart_item_data, $term_ids ) {

// ID austauschen. Wenn Produkt A mit ID 54 im Warenkorb liegt, kommt Produkt B mit ID 56 automatisch im Warenkorb hinzu. 
if ( 419 == $term_ids ) {
	WC()->cart->add_to_cart( 2436 );
}

}, 10, 2 );`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment