Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the...
#3: Post edited
- I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the page), what is wrong with it ?
- Here is the code:
- ```
- function add_phone_protection_option_to_product_page() {
- global $product;
- if (strpos(strtolower($product->get_title()), 'word') !== false) {
- $phone_protection_image_url = 'https://example.com';
- echo '<div class="phone-protection-option">
- <h2 style="font-size: 20px;">protection product?</h2>
- <div style="display: flex; align-items: center;">
- <label for="add_phone_protection">
- <input type="checkbox" class="add-phone-protection-checkbox" name="add_phone_protection" value="yes">
- </label>
<img src="' . esc_url($phone_protection_image_url) . '" alt="Protecție pentru telefon" style="max-width: 100px;">- <p style="margin-left: 10px;">protection <span style="font-weight: bold; color: red;">1 $</span></p>
- </div>
- </div>';
- }
- }
- add_action('woocommerce_single_product_summary', 'add_phone_protection_option_to_product_page', 30);
- function add_products_to_cart() {
- if (isset($_POST['add_phone_protection']) && $_POST['add_phone_protection'] === 'yes') {
- $product_id = get_the_ID();
- $phone_protection_product_sku = '01234567';
- WC()->cart->add_to_cart($product_id);
- WC()->cart->add_to_cart(wc_get_product_id_by_sku($phone_protection_product_sku));
- }
- }
- add_action('woocommerce_add_to_cart', 'add_products_to_cart');
- ```
- I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the page), what is wrong with it ?
- Here is the code:
- ```
- function add_phone_protection_option_to_product_page() {
- global $product;
- if (strpos(strtolower($product->get_title()), 'word') !== false) {
- $phone_protection_image_url = 'https://example.com';
- echo '<div class="phone-protection-option">
- <h2 style="font-size: 20px;">protection product?</h2>
- <div style="display: flex; align-items: center;">
- <label for="add_phone_protection">
- <input type="checkbox" class="add-phone-protection-checkbox" name="add_phone_protection" value="yes">
- </label>
- <img src="' . esc_url($phone_protection_image_url) . '" alt="protection" style="max-width: 100px;">
- <p style="margin-left: 10px;">protection <span style="font-weight: bold; color: red;">1 $</span></p>
- </div>
- </div>';
- }
- }
- add_action('woocommerce_single_product_summary', 'add_phone_protection_option_to_product_page', 30);
- function add_products_to_cart() {
- if (isset($_POST['add_phone_protection']) && $_POST['add_phone_protection'] === 'yes') {
- $product_id = get_the_ID();
- $phone_protection_product_sku = '01234567';
- WC()->cart->add_to_cart($product_id);
- WC()->cart->add_to_cart(wc_get_product_id_by_sku($phone_protection_product_sku));
- }
- }
- add_action('woocommerce_add_to_cart', 'add_products_to_cart');
- ```
#2: Post edited
- I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the page), what is wrong with it ?
- Here is the code:
- ```
- function add_phone_protection_option_to_product_page() {
- global $product;
- if (strpos(strtolower($product->get_title()), 'word') !== false) {
$phone_protection_image_url = 'https://example.com'; // Replace with your image URL- echo '<div class="phone-protection-option">
- <h2 style="font-size: 20px;">protection product?</h2>
- <div style="display: flex; align-items: center;">
- <label for="add_phone_protection">
- <input type="checkbox" class="add-phone-protection-checkbox" name="add_phone_protection" value="yes">
- </label>
- <img src="' . esc_url($phone_protection_image_url) . '" alt="Protecție pentru telefon" style="max-width: 100px;">
- <p style="margin-left: 10px;">protection <span style="font-weight: bold; color: red;">1 $</span></p>
- </div>
- </div>';
- }
- }
- add_action('woocommerce_single_product_summary', 'add_phone_protection_option_to_product_page', 30);
- function add_products_to_cart() {
- if (isset($_POST['add_phone_protection']) && $_POST['add_phone_protection'] === 'yes') {
- $product_id = get_the_ID();
- $phone_protection_product_sku = '01234567';
- WC()->cart->add_to_cart($product_id);
- WC()->cart->add_to_cart(wc_get_product_id_by_sku($phone_protection_product_sku));
- }
- }
- add_action('woocommerce_add_to_cart', 'add_products_to_cart');
- ```
- I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the page), what is wrong with it ?
- Here is the code:
- ```
- function add_phone_protection_option_to_product_page() {
- global $product;
- if (strpos(strtolower($product->get_title()), 'word') !== false) {
- $phone_protection_image_url = 'https://example.com';
- echo '<div class="phone-protection-option">
- <h2 style="font-size: 20px;">protection product?</h2>
- <div style="display: flex; align-items: center;">
- <label for="add_phone_protection">
- <input type="checkbox" class="add-phone-protection-checkbox" name="add_phone_protection" value="yes">
- </label>
- <img src="' . esc_url($phone_protection_image_url) . '" alt="Protecție pentru telefon" style="max-width: 100px;">
- <p style="margin-left: 10px;">protection <span style="font-weight: bold; color: red;">1 $</span></p>
- </div>
- </div>';
- }
- }
- add_action('woocommerce_single_product_summary', 'add_phone_protection_option_to_product_page', 30);
- function add_products_to_cart() {
- if (isset($_POST['add_phone_protection']) && $_POST['add_phone_protection'] === 'yes') {
- $product_id = get_the_ID();
- $phone_protection_product_sku = '01234567';
- WC()->cart->add_to_cart($product_id);
- WC()->cart->add_to_cart(wc_get_product_id_by_sku($phone_protection_product_sku));
- }
- }
- add_action('woocommerce_add_to_cart', 'add_products_to_cart');
- ```
#1: Initial revision
Adding two products into cart with help of a checkbox.
I've created a code snippet in "functions.php" that should add another product to your cart if you mark the checkbox, but somehow, only the original product is add (like the single product from the page), what is wrong with it ? Here is the code: ``` function add_phone_protection_option_to_product_page() { global $product; if (strpos(strtolower($product->get_title()), 'word') !== false) { $phone_protection_image_url = 'https://example.com'; // Replace with your image URL echo '<div class="phone-protection-option"> <h2 style="font-size: 20px;">protection product?</h2> <div style="display: flex; align-items: center;"> <label for="add_phone_protection"> <input type="checkbox" class="add-phone-protection-checkbox" name="add_phone_protection" value="yes"> </label> <img src="' . esc_url($phone_protection_image_url) . '" alt="Protecție pentru telefon" style="max-width: 100px;"> <p style="margin-left: 10px;">protection <span style="font-weight: bold; color: red;">1 $</span></p> </div> </div>'; } } add_action('woocommerce_single_product_summary', 'add_phone_protection_option_to_product_page', 30); function add_products_to_cart() { if (isset($_POST['add_phone_protection']) && $_POST['add_phone_protection'] === 'yes') { $product_id = get_the_ID(); $phone_protection_product_sku = '01234567'; WC()->cart->add_to_cart($product_id); WC()->cart->add_to_cart(wc_get_product_id_by_sku($phone_protection_product_sku)); } } add_action('woocommerce_add_to_cart', 'add_products_to_cart'); ```