Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

50%
+0 −0
Q&A 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...

0 answers  ·  posted 1y ago by gft‭  ·  edited 1y ago by gft‭

Question php woocommerce
#3: Post edited by user avatar gft‭ · 2023-09-29T11:09:31Z (about 1 year ago)
  • 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 by user avatar gft‭ · 2023-09-29T11:07:34Z (about 1 year ago)
  • 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 by user avatar gft‭ · 2023-09-29T11:06:31Z (about 1 year ago)
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');
```