This should work. Untested though.
I don’t know how you get $woocomerce_wookc_text, but since you mentioned CMB, I am taking you are saving it as product meta data
function wpse405916_add_to_cart( $cart_item_key, $product_id ) {
//Get the woocomerce_wookc_text value.
$woocomerce_wookc_text = get_post_meta( $product_id, 'woocomerce_wookc_text ', true );
//Check if the value exists
if ( $woocomerce_wookc_text && !empty($woocomerce_wookc_text) ) {
//Set the value as integer
$woocomerce_wookc_text = (int)$woocomerce_wookc_text;
//Add it to cart
WC()->cart->add_to_cart($woocomerce_wookc_text);
}
}
add_action( 'woocommerce_add_to_cart', 'wpse405916_add_to_cart', 10, 2 );
This will automatically add the product associated with the id from $woocomerce_wookc_text when the main product is added to cart. There are numerous checks should be added.
Related Posts:
- What are the differences between WPINC and ABSPATH?
- Problems with autoloading classes via sp_autoload_register / maybe interfering WP-specific autoloader
- Using require_once in a Plugin?
- How to Loop Plugin Option Field Set?
- Rename existing old uploaded images containing accented French characters (not during upload)
- How to Create a Directory in a Plugin Folder?
- Create plugin that works in background
- Checking for user role in a custom plugin
- HELP: Integrating WP with raw PHP code once published button is hit
- Loading class files via autoload method
- I would like to use create a function in my custom plugin to tell WP to use a different header
- Getting media library popup in custom plugin admin page
- jQuery function didn’t work in my plugin
- Sanitizing, Validating and Escaping in WordPress (Plugin)
- Escape when echoed
- Scope for PHP Variables Assigned in functions.php or a plugin
- Plugin Development sqlite or WordPress’ database
- WooCommerce get_price returning wrong price when used via ajax
- Open Post Thumbnail in New Child Theme File in WordPress
- Redirecting to home page after login as custom role WordPress
- Add keywords meta to index.php page
- Pulling data from custom plugin settings using PHP shortcode and Javascript
- wordpress plugin is not activating from widget
- Creating a custom post type, adding custom meta fields, preventing all future editability of posts of this type
- How to fetch products with the price in a page on woocommerce using a form or live search with php
- Creating a Callback URL for WordPress Woocommerce to update Order Status
- What are the specifics of WordPress development I need to know? [closed]
- How to replace WordPress Media Upload Window with user’s device window?
- How to create a custom post type that contains another custom post type?
- Setup wp_schedule_event within a custom plugin
- Issue running db create table query from static method
- execute function after one completed
- How to make multiple admin pages for one plugin?
- Include a php file from another plugin
- Integrating boxtal PHP library into a custom WordPress Plugin
- How to create plugin/ page that reads from database
- Save user total active time after login in wordpress [closed]
- Switch from wp-cron to a server cron job
- Why is it important to check for isset and is_array before checking in_array in this example?
- How to add/change (woocommerce) product tab icon
- Comment “like” problem – “users who like this” avatar linking to current user profile instead of “liker’s” profile
- I want to generate the pdf and save it in library?
- Calling a save function from a “submit” button
- Wp Debug Enabled True Notices and Warning
- How to call external functions from a PHP script in a WordPress plugin?
- How to write to a separate PHP file in plugin settings
- Create “new pages” within the admin section of plugin
- More gentle way to hook WordPress custom url
- How to display results from a data table with an encrypted user id?
- How to add custom function to pluggable.php
- How to displaying Custom image, text and default image logo from Redux Framework option panel
- How to Call Specific .PHP file on add_submenu_page selection in Plugin Development?
- i need to make custom cron_schedule with custom interval time as a parameter into a custom payment gateway plugin wordpress
- save_post_product action not firing
- How to get the custom field value using SQL query
- Unexpected add action function in WP plugin
- How to create admin setting for this small plugin
- Prevent WordPress from sending set-cookie http header
- Cookie value changes back to previous value after changing
- How use wp_insert_post and add og:custom tags?
- Is there any way to sync Facebook Comments and with comments on WordPress website?
- Problem with checked box on wp car manager plugin
- WordPress rewrite rule depend on meta values not ids
- How to change “Read More” text?
- How to use Datatable with Ajax when creating plugin on WordPress?
- Problem with wordpress has_action() function
- Offering a freemium plugin (a free version + paid version)
- Using id to show item
- Generate and send ICS file through WordPress
- Is admin section completely customizable in terms of styling?
- Woocommerce list variations that are added already to cart in Single Product
- WordPress Admin sub-level menu issue
- Change pricing in Woocommerce based on Category and Product [closed]
- Passing ajax variable to more than one wordpress plugin function
- WordPress environment not loading properly
- Form tries to download a file on submit
- WordPress not displaying information on post sent from custom admin form
- insert multiple entries in database using a loop issue
- Displaying friend’s posts only
- Creating a user ‘add custom field’ section
- Autoloading a PHP class – working outside, but not inside WordPress plugin
- Saving an array to get_options
- Doing action based on input from options menu
- Help me with my first very basic plugin
- How use Dynamic hyperlink on each wordpress post?
- My wordpress website admin panel loads very slow, what can i do about that? [closed]
- How to create post comparison in wordpress
- What can I do to customize a widget provided with this plugin? from where have I to start?
- wp_handle_upload – specified file failed upload test
- How to make a Template page to show the information of different things Shop and Product page?
- How to trigger plugin migration code when plugin updates?
- How to Save Category and Delete Category in same Function
- public custom posts not showing in my wordpress plugin
- Accessing Correct Database to Create REST API Endpoint
- add_action() not working for admin
- Read page- or post-content across different builder-plugins
- How to make WordPress Plugin run on single specific Admin Page
- Error 400 Invalid “products[1].productId” Kinguin [closed]
- Plugin question, How to insert a colon in a value box
- Custom page template from plugin does not work with pre-installed themes in WordPress 6.6.1