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 Oscar-Abad-Folgueira/b1a3a0653777fd27ac8feccaf4484228 to your computer and use it in GitHub Desktop.
Save Oscar-Abad-Folgueira/b1a3a0653777fd27ac8feccaf4484228 to your computer and use it in GitHub Desktop.
Comprobar si un producto está en el carrito de WooCommerce
<?php
/**
* @snippet Comprobar si un producto está en el carrito de WooCommerce
* @author Oscar Abad Folgueira
* @author_url https://www.oscarabadfolgueira.com
* @snippet_url https://www.oscarabadfolgueira.com/comprobar-si-un-producto-esta-en-el-carrito-de-woocommerce/
*/
add_action( 'template_redirect', 'check_if_product_in_cart' );
function check_if_product_in_cart() {
$product_id = 80;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_id );
if ( !$in_cart ) {
WC()->cart->add_to_cart( $product_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment