You are doing it all wrong, place your code like the following in your file.
<?php
echo 'hook called'; // This is not showing any message
exit();
global $woocommerce;
$product_id = $_POST['selected_package_id'];
echo $product_id; // This is not displaying id
exit();
$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 );
}
The approach you are using to do it by hooks is not correct.
Related Posts:
- woocommerce_package_rates not fired everytime
- call_user_func_array() expects parameter 1 to be a valid callback, class ‘WC_Rapyd’ does not have a method ‘install’
- WooCommerce: Can’t use wc_get_products for custom REST API endpoints
- How to get rid of the hover zoom in WooCommerce single products
- What’s the difference between WC() and $woocommerce
- Show only geolocated user country into Woocommerce checkout country fields
- Extend Woocommerce rest api routes fails
- Remove action on product archive page [closed]
- WooCommerce add_to_cart() with custom price [closed]
- wc_get_product in a none woocommerce page
- The correct way to override WooCommerce product gallery from a plugin [closed]
- display available size on hover, in woocommerce shop page [closed]
- Customize WooCommerce orders displayed shipping
- WooCommerce add_to_cart url not working [closed]
- How can I show an empty WooCommerce cart? [closed]
- Is there a Woocommerce hook that fires when applying a coupon but before checking if it’s valid?
- What is the action hook for an order that fails on frontend checkout in WooCommerce?
- How to unhook a function in Woocommerce Template?
- Mini cart in header using divi is not getting updated on changing the cart items count:
- Using a filter with multiple parameters and $this
- Remove Order List Row Link in WooCommerce Admin?
- Get rid of product images mobile swipe functionality from WooCommerce single product [closed]
- Which hook should I use to capture $_POST(‘password’) via profile update and password reset
- Problems adding in-store product managers
- differentiate hooks and filters on the same page [closed]
- woocommerce_package_rates not fired when wordpress woocommerce accessed as non ajax
- ‘profile_update’ hook alternative for WooCommerce user meta data
- How to update custom field in WooCommerce
- Viewing checkout page removes fees from mini cart
- Add Stripe Connect data to wc_stripe_payment_request
- Programatically Remove Item from Cart Not Working – Needs Page Refresh
- Redirect customer to login page (with other signup plugin) if user not logged in when proceeding to checkout
- How to subscribe free subscription on user registration in woocommerce subscription plugin?
- How to list all WC products by their attributes values?
- Showing the WordPress standard text area on a Woocommerce Single Product Page?
- Woocommerce Moving Review Below add to cart breaks button
- Is it possible to intercept all ajax requests and get the parameters and the returns?
- WooCommerce update_shipping_method ajax call wipes shipping method set on non-checkout page [closed]
- How to override Woocommerce functionality in Reports section?
- how can use woocommerce add to cart popup?
- How to Update the Order-Items While Editing an Order on the WooCommerce Admin Order Screen [closed]
- WooCommerce Hook: Content get’s displayed twice and not AFTER the order total
- How to re-arrange this hooked content?
- CF7 Ajax isn’t working when called inside WooCommerce “woocommerce_before_add_to_cart_button” hook [closed]
- How to exclude a taxonomy from shop & search page wooCommerce?
- ajax request in wordpress
- How to change a WooCommerce Subscription Deposit and Monthly Payment? [closed]
- How can I add a so i can show sale price under normal price
- password_reset doesnt work
- WooCommerce checkout page price break down table hook
- WooCommerce event works in jQuery but not in JavaScript
- How to output woocommerce products to a page, style and modify the html structure
- Filter default_content only for products
- Woocommerce – Adding row to cart table
- Display custom product option in admin order details
- How to alter taxes amounts when updating an order
- Hooks For Design variation in woo commerce single product page
- How to add woocommerce products header title in woocommerce_breadcrumb hook to function.php
- Trying to run Javascript on AJAX call
- Use ID as slug for product category by default in WooCommerce?
- What is the problem with these lines of code?
- WooCommerce variable product can not add to cart using ajax after updating latest version of WooCommerce
- Woocommerce : Add name filed of checkout page to buyers wordpress display name
- Running JavaScript after using WooCommerce Filter Plugin
- Help to locate a woocommerce hook method
- Call product search from custom hook
- woocommerce deposit Form with AJAX and shortcode won’t update but add to previous output
- Woocommerce Variation product added twice to cart
- check shipping amount in woocommerce_after_checkout_validation?
- woocommerce REST api on the website’s front-end
- How to display two different custom taxonomy terms on a WooCommerce single product page
- Woocommerce Disabling Auto Restocking for Cancelled Orders
- Parameters in woocommerce_before_add_to_cart_button hook
- Pass values to ajax
- Fragments to Update Cart Counter not working on homepage – Woocommerce
- How can i add product Accessories from front-end using custom php form?
- Overriding Plugin function in your child theme
- woocommerce_add_to_cart custom function called several times
- Update / refresh mini cart, after custom AJAX add_to_cart event
- How to track product price changes?
- Remove username and account password field from checkout page
- Can’t update WooCommerce payment method title
- Using category and its sub-categories products attributes as filter in products pages
- Removing Product from Woocommerce checkout page using Ajax
- Add suffix to price html does not work
- add_filter not works in ajax
- How to hook in to WooCommerce Related Products
- Woocommerce: Unique price for individual basket items
- Hide add to cart when product variation has no price or is unavailable
- SyntaxError: unterminated string literal – if WordPress Debug in wp-config is set to true
- Hook woocommerce price in backend order edition
- Create woocommerce order on new user registration
- How to add ajax loading icon after click add to cart button on woocommerce
- WooCommerce Hook after Billing form completed, but before payment Gateway
- dakon show vender address on checkout page
- Hide a shipping method if cart total is higher than an amount
- WordPress error
- Add second price option based on payment method at cart item price
- Use a woocommerce hook to create a table and populate it with a query
- Woocommerce ajax search can’t work on mobile