Title: Woocommerce Code integration &#8211; php functions
Last modified: November 17, 2020

---

# Woocommerce Code integration – php functions

 *  [lucasddweb](https://wordpress.org/support/users/lucasddweb/)
 * (@lucasddweb)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/woocommerce-code-integration-php-functions/)
 * I’m using the membership plugin to restrict some products from buying, because
   users should buy a yearly membership in order to be able to buy my products.
   
   I would like to write a function in which if the product “membership” (that unlocks
   all the other products) is in the cart, woocommerce automatically unlocks all
   the other products. This because I want users to be able to buy products, without
   doing a double separate purchase. Membership (to unlock the products) and then
   products. Actually they can’t because the system tells the users “You have to
   buy the membership to purchase this product”even if the “membership” product 
   is already in the cart.
 * I hope to have described clearly the request, in case not, please ask me further
   details.
 * Thanks a lot

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [con](https://wordpress.org/support/users/conschneider/)
 * (@conschneider)
 * Engineer
 * [5 years, 6 months ago](https://wordpress.org/support/topic/woocommerce-code-integration-php-functions/#post-13674785)
 * Hi there,
 * > I’m using the membership plugin to restrict some products from buying,
 * Since each plugin has it’s own structure: Which membership plugin are you using
   exactly?
 * Kind regards,
 *  Thread Starter [lucasddweb](https://wordpress.org/support/users/lucasddweb/)
 * (@lucasddweb)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/woocommerce-code-integration-php-functions/#post-13676479)
 * Hi Con
 * I’m using “WooCommerce Memberships” by “SkyVerge”
 * I added this function in order to insert the product to cart(for not logged users):
 *     ```
       function add_product_to_cart() {
   
                       if ( ! is_user_logged_in(false) ) {
   
                                       global $woocommerce;
   
                                       $product_id = 7660;
   
                                       $found = false;
   
                                       //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_id );
   
                                       } else {
   
                                                       // if no products in cart, add it
   
                                                       $woocommerce->cart->add_to_cart( $product_id );
   
                                       }
   
                       }
   
       }
   
       add_action( 'init', 'add_product_to_cart' );
       ```
   
 * This function works correctly, but now i need a function that is able to replicate
   this scenario:
 * If product 7660 (membership product) is in the cart, allow to add to cart all
   products “available”.
 * Actually when i try to add any products to cart without having the subscription
   active (product_id = 7660 alredy buyed) the system tells me: “you have to buy
   the membership in order to add this product to cart”. So people is obliged to
   do 2 separate purchase.
 * Thanks a lot
 *  Thread Starter [lucasddweb](https://wordpress.org/support/users/lucasddweb/)
 * (@lucasddweb)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/woocommerce-code-integration-php-functions/#post-13682171)
 * up

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Woocommerce Code integration – php functions’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [lucasddweb](https://wordpress.org/support/users/lucasddweb/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/woocommerce-code-integration-php-functions/#post-13682171)
 * Status: not resolved