איך להוסיף מוצר לעגלה במידה ויש בעגלה מעל סכום מסוים

הטמעת קוד פשוטה וקלה

העתקת קוד פשוטה והתאמות קלות ויש לך קוד מקצועי לשילוב של מוצרים בעגלה להוספה לפי מחיר קבוע מראש.

מצורפים כל השלבים כולל צילומי מסך להמחשה:

שלב 1

התקנת תוסף קוד –
לחצו על לשונית תוספים  ולאחר מכן על add a plugin
בלוח הבקרה של וורדפרס.

שלב 2

מחפשים בשורת החיפוש את המילה code ומתקינים אחד מן התוספים המסומנים ומפעילים אותו.

שלב 3

בלשוניות בלוח הבקרה של וורדפרס תופיע לשונית חדשה תחת השם CODE או Snippets תלחצו עליה ואז על הכפתור add new 

שלב 4

הוסיפו כותרת שמתארת בצורה ברורה מה מטרת הקוד- כדי שלא תשכחו / מי שיבוא אחרי ידע מה מטרתו
העתיקו את הקוד  המצורף פה בעמוד 
והדביקו באיזור התוכן של הסניפט.

חשוב לשים לב שהקוד מוגדר כקוד PHP
כעת תשמרו את הקוד

				
					// הצגת מוצרי מבצע בעגלה אם הסכום מעל 299
add_action( 'woocommerce_cart_totals_after_order_total', 'custom_cart_offer_products' );
function custom_cart_offer_products() {
    if ( WC()->cart->get_subtotal() < 299 ) return;

    $offer_products = array( 5986 ); // מזהי המוצרים למבצע
    $max_quantity   = 1;

    echo '<div class="custom-cart-offer" style="border:1px solid #ccc;padding:15px;margin-top:20px;background:#f9f9f9;">';
    echo '<h3>הוספת חנוכייה ב200 במקום 285</h3>';
    echo '<ul>';

    foreach ( $offer_products as $product_id ) {
        $product = wc_get_product( $product_id );
        if ( ! $product ) continue;

        // בדיקה אם המוצר כבר בעגלה
        $in_cart = false;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['product_id'] == $product_id ) {
                $in_cart = true;
                break;
            }
        }

        echo '<li style="margin-bottom:10px;">';
        echo $product->get_image( 'thumbnail' ) . ' ';
        echo '<strong>' . $product->get_name() . '</strong> ';

        if ( $in_cart ) {
            echo '<span style="color:green;font-weight:bold;">המוצר כבר בעגלה</span>';
        } else {
            $add_url = esc_url( add_query_arg( 'add-to-cart', $product_id, wc_get_cart_url() ) );
            echo '<a href="' . $add_url . '" class="button">הוספה לעגלה</a>';
        }

        echo '</li>';
    }

    echo '</ul>';
    echo '</div>';
}

// שינוי מחיר למבצע
add_action( 'woocommerce_before_calculate_totals', 'custom_set_offer_price' );
function custom_set_offer_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $offer_products = array( 5986 );
    $offer_price    = 200;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( in_array( $cart_item['product_id'], $offer_products ) ) {
            $cart_item['data']->set_price( $offer_price );
        }
    }
}

// הגבלת כמות מוצרי מבצע לסל
add_filter( 'woocommerce_add_to_cart_quantity', 'limit_offer_product_quantity', 10, 2 );
function limit_offer_product_quantity( $quantity, $product_id ) {
    $offer_products = array( 5986 );
    $max_quantity   = 1;

    if ( in_array( $product_id, $offer_products ) ) {
        $quantity = $max_quantity;
    }

    return $quantity;
}

// מניעת עדכון כמות ידני מעבר למקסימום
add_filter( 'woocommerce_update_cart_validation', 'prevent_offer_cart_update', 10, 4 );
function prevent_offer_cart_update( $passed, $cart_item_key, $values, $quantity ) {
    $offer_products = array( 5986 );
    $max_quantity   = 1;

    if ( in_array( $values['product_id'], $offer_products ) && $quantity > $max_quantity ) {
        wc_add_notice( 'ניתן להוסיף רק ' . $max_quantity . ' יחידה ממוצרי המבצע.', 'error' );
        return false;
    }

    return $passed;
}

				
			

שלב 5 – עריכת הקוד להתאמה לאתר שלכם

לכו ללשונית מוצרים ושימו את העכבר מעל למוצר שאתם רוצים לתת עליו הנחה – יופיעו לכם שם כפתורי פעולה ובנוסף באפור יופיע המספר הסידורי של המוצר הID (בתמונה פה הוא 6125)
העתיקו את מספר הID וחזרו אל הקוד שיצרתם באתר.

את מספר הID הדביקו במקום המספר המופיע בקוד בשורות
6, 49 , 62, 75
חשוב להחליף בכל המקומות.

התאמת מחיר:

בשורה 4 בקוד מופיע התנאי שאם המחיר עבר את הסכום 299 אתם יכולים להגדיר את הסכום לאיך שתרצו שיתאים לכם.

בשורה 50 מופיע המחיר החדש שבו יוכנס המחיר החדש גם אותו ניתן לשנות כראות עיניכם.

הערה חשובה הקוד לא עובד במידה ובאתר מותקן תוסף כמו dicount rules – יש לי קוד שעוקף גם את התוסף אולי בעתיד אפרסם 🙂

בונוס: קוד הזנה אוטומטית של המוצר לעגלה

רוצים לתת מתנה ללקוחות בחינם ושאם הם עברו המוצר יתווסף להם אוטומתית לעגלה?
צירפתי לכם פה קוד שעושה בדיוק את זה – מוזמני להזין אותו באותה דרך בדיוק כמו הקוד הראשון 

שימו לב השורות קצת שונות אבל העיקרון זהה צריך להזין את הID של המוצר שמתווסף ולקבוע את מחיר הסף.

				
					// --- הגדרות ---
function my_offer_get_settings() {
    return array(
        'offer_products' => array( 4263 ), // מזהי מוצרים במבצע
        'threshold'      => 159,           // סף בש"ח
        'max_qty'        => 1,
    );
}

// --- הצגת הצעת המבצע ---
add_action( 'woocommerce_cart_totals_after_order_total', 'custom_cart_offer_products' );
function custom_cart_offer_products() {
    $s = my_offer_get_settings();

    if ( ! WC()->cart ) return;
    if ( WC()->cart->get_subtotal() < $s['threshold'] ) return;

    echo '<div class="custom-cart-offer" style="border:1px solid #ccc;padding:15px;margin-top:20px;background:#f9f9f9;">';
    echo '<h3>בלון הליום קוטר 18 במתנה!</h3>';
    echo '<ul>';

    foreach ( $s['offer_products'] as $product_id ) {
        $product = wc_get_product( $product_id );
        if ( ! $product ) continue;

        $in_cart = false;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['product_id'] == $product_id ) {
                $in_cart = true;
                break;
            }
        }

        echo '<li style="margin-bottom:10px;">';
        echo $product->get_image( 'thumbnail' ) . ' ';
        echo '<strong>' . $product->get_name() . '</strong> ';

        if ( $in_cart ) {
            echo '<span style="color:green;font-weight:bold;">המוצר בעגלה (ניתן להסרה)</span>';
        } else {
            $add_url = esc_url( add_query_arg( 'add-to-cart', $product_id, wc_get_cart_url() ) );
            echo '<a href="' . $add_url . '" class="button">הוספת בלון הליום ללא עלות</a>';
        }

        echo '</li>';
    }

    echo '</ul>';
    echo '</div>';
}

// --- ניהול אוטומטי של מוצר המבצע ---
add_action( 'template_redirect', 'auto_manage_offer_product' );
function auto_manage_offer_product() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( ! function_exists( 'WC' ) ) return;
    if ( ! WC()->cart ) return;

    $s = my_offer_get_settings();
    $session = WC()->session;

    // אחסון מצב האם המשתמש הסיר את המוצר
    $removed_offer = $session->get( 'offer_removed', false );

    $subtotal = WC()->cart->get_subtotal();

    // אם הסכום קטן מהסף - הסר את המוצר ואפס את הדגל
    if ( $subtotal < $s['threshold'] ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( in_array( $cart_item['product_id'], $s['offer_products'] ) ) {
                WC()->cart->remove_cart_item( $cart_item_key );
            }
        }
        $session->set( 'offer_removed', false );
        return;
    }

    // אם המשתמש הסיר את המוצר, לא להחזיר אותו אוטומטית
    if ( $removed_offer ) return;

    // אם המוצר לא בעגלה, הוסף אותו
    $found = false;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( in_array( $cart_item['product_id'], $s['offer_products'] ) ) {
            $found = true;
            break;
        }
    }

    if ( ! $found ) {
        foreach ( $s['offer_products'] as $pid ) {
            $prod = wc_get_product( $pid );
            if ( $prod ) {
                WC()->cart->add_to_cart( $pid, 1 );
            }
        }
    }
}

// --- שינוי מחיר למוצר מבצע ---
add_action( 'woocommerce_before_calculate_totals', 'custom_set_offer_price', 20 );
function custom_set_offer_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( ! $cart ) return;

    $s = my_offer_get_settings();
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( in_array( $cart_item['product_id'], $s['offer_products'] ) ) {
            $cart_item['data']->set_price( 0 );
            $cart->cart_contents[ $cart_item_key ]['quantity'] = min( $cart_item['quantity'], $s['max_qty'] );
        }
    }
}

// --- הגבלת כמות ---
add_filter( 'woocommerce_add_to_cart_quantity', 'limit_offer_product_quantity', 10, 2 );
function limit_offer_product_quantity( $quantity, $product_id ) {
    $s = my_offer_get_settings();
    if ( in_array( $product_id, $s['offer_products'] ) ) return $s['max_qty'];
    return $quantity;
}

// --- מניעת עדכון ידני מעבר ליחידה ---
add_filter( 'woocommerce_update_cart_validation', 'prevent_offer_cart_update', 10, 4 );
function prevent_offer_cart_update( $passed, $cart_item_key, $values, $quantity ) {
    $s = my_offer_get_settings();
    if ( in_array( $values['product_id'], $s['offer_products'] ) && $quantity > $s['max_qty'] ) {
        wc_add_notice( 'ניתן להוסיף רק יחידה אחת ממוצר המבצע.', 'error' );
        return false;
    }
    return $passed;
}

// --- כשהמוצר מוסר - שמור שהמשתמש הסיר ---
add_action( 'woocommerce_remove_cart_item', 'mark_offer_removed', 10, 2 );
function mark_offer_removed( $cart_key, $cart ) {
    $s = my_offer_get_settings();
    $removed_product_id = $cart->removed_cart_contents[ $cart_key ]['product_id'] ?? null;

    if ( $removed_product_id && in_array( $removed_product_id, $s['offer_products'] ) ) {
        WC()->session->set( 'offer_removed', true );
    }
}

				
			

בהצלחה 🙂

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *