{"id":19716,"date":"2025-01-30T15:58:15","date_gmt":"2025-01-30T15:58:15","guid":{"rendered":"http:\/\/a8759758b2.nxcli.io\/?post_type=docs&#038;p=19716"},"modified":"2025-06-09T12:08:44","modified_gmt":"2025-06-09T12:08:44","password":"","slug":"wie-wendet-man-automatisch-einen-gutschein-an-wenn-sich-ein-bestimmtes-produkt-im-woocommerce-warenkorb-befindet","status":"publish","type":"docs","link":"https:\/\/couponaffiliates.com\/de\/docs\/wie-wendet-man-automatisch-einen-gutschein-an-wenn-sich-ein-bestimmtes-produkt-im-woocommerce-warenkorb-befindet\/","title":{"rendered":"Wie man einen Gutschein automatisch anwendet, wenn ein bestimmtes Produkt im WooCommerce-Warenkorb liegt (Funktion)"},"content":{"rendered":"<p>Es gibt keine Option zur automatischen Anwendung von Gutscheinen, wenn sich ein bestimmtes Produkt standardm\u00e4\u00dfig im WooCommerce-Warenkorb befindet.<\/p>\n\n\n\n<p>Es ist jedoch m\u00f6glich, dies mit etwas benutzerdefiniertem Code zu tun.<\/p>\n\n\n\n<p>Hier ist ein Beispiel, das eine Option zu Ihren Gutscheineinstellungen hinzuf\u00fcgt, um ein Produkt auszuw\u00e4hlen, das mit diesem Gutschein verkn\u00fcpft werden soll. Wenn das Produkt in den Warenkorb gelegt wird, wird auch der Gutschein angewendet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Hinzuf\u00fcgen der Option \"Auto-Apply When Product In Cart\" zu den WooCommerce Coupon-Einstellungen.\n *\/\nfunction wcusage1_add_coupon_auto_apply_option($coupon_id, $coupon) {\n    woocommerce_wp_select(array(\n        'id' =&gt; 'auto_apply_product',\n        label' =&gt; __('Auto-Apply When Product In Cart', 'woocommerce'),\n        'description' =&gt; __('W\u00e4hlen Sie ein Produkt aus, um die automatische Coupon-Anwendung auszul\u00f6sen.', 'woocommerce'),\n        'desc_tip' =&gt; true,\n        options' =&gt; wcusage1_get_product_dropdown_options(),\n        value' =&gt; get_post_meta($coupon_id, 'auto_apply_product', true),\n    ));\n    ?&gt;\n    \n     'ver\u00f6ffentlichen',\n        'limit' =&gt; -1,\n        'return' =&gt; 'objects',\n    ));\n\n    $options = array('' =&gt; __('None', 'woocommerce'));\n    foreach ($products as $product) {\n        $options[$product-&gt;get_id()] = $product-&gt;get_name();\n    }\n\n    return $options;\n}\n\n\/**\n * Automatische Anwendung von Gutscheinen, wenn sich ein passendes Produkt im Warenkorb befindet.\n *\/\nfunction wcusage1_auto_apply_coupon() {\n    if (is_admin() &amp;&amp; !defined('DOING_AJAX')) {\n        return;\n    }\n\n    $cart = WC()-&gt;cart;\n    $cart_product_ids = array();\n\n    \/\/ Alle Produkt-IDs im Warenkorb abrufen\n    foreach ($cart-&gt;get_cart() as $cart_item) {\n        $cart_product_ids[] = $cart_item['product_id'];\n    }\n\n    \/\/ Alle Gutscheine mit dem eingestellten Auto-Apply-Produkt abrufen\n    $args = array(\n        'post_type' =&gt; 'shop_coupon',\n        'post_status' =&gt; 'publish',\n        'posts_per_page' =&gt; -1,\n    );\n\n    $coupons = get_posts($args);\n\n    foreach ($coupons as $coupon) {\n        $coupon_code = $coupon-&gt;post_title;\n        $auto_apply_product = get_post_meta($coupon-&gt;ID, 'auto_apply_product', true);\n\n        \/\/ Pr\u00fcfen, ob sich das Produkt im Warenkorb befindet\n        if ($auto_apply_product &amp;&amp; in_array($auto_apply_product, $cart_product_ids)) {\n            if (!$cart-&gt;has_discount($coupon_code)) {\n                $cart-&gt;apply_coupon($coupon_code);\n            }\n        }\n    }\n}\nadd_action('woocommerce_before_calculate_totals', 'wcusage1_auto_apply_coupon');\n\n\/**\n * Entferne automatisch angewandten Gutschein, wenn das entsprechende Produkt aus dem Warenkorb entfernt wird.\n *\/\nfunction wcusage1_remove_auto_applied_coupon($cart_item_key) {\n    $cart = WC()-&gt;cart;\n    $cart_product_ids = array();\n\n    \/\/ Alle Produkt-IDs im Warenkorb abrufen\n    foreach ($cart-&gt;get_cart() as $cart_item) {\n        $cart_product_ids[] = $cart_item['product_id'];\n    }\n\n    \/\/ Alle Gutscheine mit dem eingestellten Auto-Apply-Produkt abrufen\n    $args = array(\n        'post_type' =&gt; 'shop_coupon',\n        'post_status' =&gt; 'publish',\n        'posts_per_page' =&gt; -1,\n    );\n\n    $coupons = get_posts($args);\n\n    foreach ($coupons as $coupon) {\n        $coupon_code = $coupon-&gt;post_title;\n        $auto_apply_product = get_post_meta($coupon-&gt;ID, 'auto_apply_product', true);\n\n        \/\/ Pr\u00fcfen, ob sich das Produkt im Warenkorb befindet\n        if ($auto_apply_product &amp;&amp; !in_array($auto_apply_product, $cart_product_ids)) {\n            if ($cart-&gt;has_discount($coupon_code)) {\n                $cart-&gt;remove_coupon($coupon_code);\n            }\n        }\n    }\n}\nadd_action('woocommerce_cart_item_removed', 'wcusage1_remove_auto_applied_coupon');<\/code><\/pre>\n\n\n\n<p>Der Code ist m\u00f6glicherweise nicht 100% vollst\u00e4ndig oder richtig getestet. Bitte stellen Sie sicher, dass Sie Ihre eigenen Tests durchf\u00fchren und Backups der Website erstellen, bevor Sie den benutzerdefinierten Code verwenden.<\/p>","protected":false},"excerpt":{"rendered":"<p>Es gibt keine Option zur automatischen Anwendung von Gutscheinen, wenn sich ein bestimmtes Produkt standardm\u00e4\u00dfig im WooCommerce-Warenkorb befindet. Es ist jedoch m\u00f6glich, dies mit etwas benutzerdefiniertem Code zu tun. Hier ist ein Beispiel, das eine Option zu Ihren Gutscheineinstellungen hinzuf\u00fcgt, um ein Produkt auszuw\u00e4hlen, das mit diesem Gutschein verkn\u00fcpft werden soll. Wenn das Produkt hinzugef\u00fcgt wird [...]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","footnotes":"","_links_to":"","_links_to_target":""},"doc_category":[136],"doc_tag":[],"class_list":["post-19716","docs","type-docs","status-publish","hentry","doc_category-developers","wpbf-post"],"year_month":"2026-04","word_count":561,"total_views":"1961","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"display_name":"Elliot Sowersby","author_link":"#"},"doc_category_info":[{"term_name":"Developers","term_url":"https:\/\/couponaffiliates.com\/de\/kategorie-docs\/entwickler\/"}],"doc_tag_info":[],"taxonomy_info":{"doc_category":[{"value":136,"label":"Developers"}]},"featured_image_src_large":false,"comment_info":0,"knowledge_base_info":[],"knowledge_base_slug":[],"_links":{"self":[{"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/docs\/19716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/comments?post=19716"}],"version-history":[{"count":4,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/docs\/19716\/revisions"}],"predecessor-version":[{"id":21123,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/docs\/19716\/revisions\/21123"}],"wp:attachment":[{"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/media?parent=19716"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/doc_category?post=19716"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/couponaffiliates.com\/de\/wp-json\/wp\/v2\/doc_tag?post=19716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}