{"id":2250,"date":"2022-11-09T14:08:39","date_gmt":"2022-11-09T13:08:39","guid":{"rendered":"https:\/\/www.areaweb.sk\/?p=2250"},"modified":"2022-11-18T13:09:23","modified_gmt":"2022-11-18T12:09:23","slug":"woocomerce-kupony","status":"publish","type":"post","link":"https:\/\/www.areaweb.sk\/cs\/woocomerce-kupony\/","title":{"rendered":"Woocomerce kup\u00f3ny"},"content":{"rendered":"<p class=\"wp-block-paragraph\"><strong>Vytvorenie kup\u00f3nu<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function vytvor_kupon() {\n\n    $coupon_code = 'MOJKUPON'; \/\/ Code\n    $amount = '10'; \/\/ Amount\n    $discount_type = 'fixed_cart'; \/\/ Type: fixed_cart, percent, fixed_product, percent_product\n    $coupon = array(\n    'post_title' =&gt; $coupon_code,\n    'post_content' =&gt; '',\n    'post_status' =&gt; 'publish',\n    'post_author' =&gt; 1,\n    'post_type' =&gt; 'shop_coupon');\n\n    if( !in_array($coupon&#91;'post_title'], coupon_list() ) ) {  \/\/ iba ak kupon este neexistuje\n\n        $new_coupon_id = wp_insert_post( $coupon );\n    }\n        \/\/ Add meta\n        update_post_meta( $new_coupon_id, 'discount_type', $discount_type );\n        update_post_meta( $new_coupon_id, 'coupon_amount', $amount );\n        update_post_meta( $new_coupon_id, 'individual_use', 'no' );\n        update_post_meta( $new_coupon_id, 'product_ids',  vypis_podukty_s_tagom ()) ;\n        update_post_meta( $new_coupon_id, 'exclude_product_ids', '');\n        update_post_meta( $new_coupon_id, 'usage_limit', '' );\n        update_post_meta( $new_coupon_id, 'expiry_date', '' );\n        update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );\n        update_post_meta( $new_coupon_id, 'free_shipping', 'no' );\n  \n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Vr\u00e1ti zoznam v\u0161etk\u00fdch kup\u00f3nov, konkr\u00e9tne ich atrib\u00faty name a id <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function coupon_list() {\n  $coupon_posts = get_posts( array(\n      'posts_per_page'   =&gt; -1,\n      'orderby'          =&gt; 'name',\n      'order'            =&gt; 'asc',\n      'post_type'        =&gt; 'shop_coupon',\n      'post_status'      =&gt; 'publish',\n  ) );\n\n  $coupon_codes = &#91;]; \/\/ Initializing\n\n  \/\/ Push to array\n  foreach ( $coupon_posts as $coupon_post ) {\n      $coupon_codes&#91;$coupon_post-&gt;post_name] = $coupon_post-&gt;ID;   \/\/ vytiahnem meno a id kup\u00f3nu\n  }\n\n  \/\/ Return coupon array\n  return $coupon_codes;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Prejde v\u0161etky produkty a vytvori zoznam ID v\u0161etk\u00fdch produktov ktor\u00e9 maj\u00fa dan\u00fa zna\u010dku<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function moj_kupon(){\n    $tag = 'Hendi';\n    $data_tags = &#91;];\n\n    $wc_query = new WP_Query( array(\n        'post_type' =&gt; 'product',\n        'post_status' =&gt; 'publish',  \n        'posts_per_page' =&gt; 3,\n        'product_tag' =&gt; $tag,\n    ) );\n    $i = 0;\n    ?&gt;\n\n     &lt;?php if ($wc_query-&gt;have_posts()) : ?&gt;\n        &lt;?php while ($wc_query-&gt;have_posts()) :\n            $wc_query-&gt;the_post();\n            $id = get_the_ID();\n           \/\/ $sku = get_post_meta( $id, '_sku', true );\n            array_push($data_tags,  $id . ', ' );\n        ?&gt;\n        &lt;?php endwhile; ?&gt;\n        &lt;?php wp_reset_postdata(); ?&gt;\n\n    &lt;?php else:  ?&gt;\n\n     &lt;?php endif; ?&gt;\n     &lt;?php\n\n        return  implode(\" \",$data_tags) ;  \/\/zmeni pole na string s ID\u010dkami\n \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Hooks ktor\u00e9 zachytia pouzite kup\u00f3nu a vyvolaj\u00fa funkciu<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/filter pre validaciu produktov\nadd_filter(\"woocommerce_coupon_is_valid\",\"plugin_coupon_validation\",10,3);\nfunction plugin_coupon_validation($valid, $coupon, $product) {\n   if( 'hendiskusobne' === strtolower($coupon->code) ) {\n        foreach( WC()->cart->get_cart() as $cart_item ){\n               $id = $cart_item&#91;'product_id'];\n\n               $term_array = &#91;];\n               $terms = wp_get_post_terms( $id, 'product_tag' );\n               foreach ( $terms as $term ) {\n                   array_push( $term_array, $term->name);\n               }\n                  \n               if( in_array(\"Hendi\", $term_array) ){\n                   \/\/ echo 'nie je ';\n               }\n               else {\n                    \/\/ $valid=false;\n                     return false;\n               }\n        }\n   }\n\n   return true; \n}\n\n\n\n\/\/ filter pre valid\u00e1ciu kup\u00f3nu\n\/\/add_filter( 'woocommerce_coupon_is_valid_for_product', 'wphelp_exclude_product_coupons', 9999, 4 );\nfunction wphelp_exclude_product_coupons( $valid, $product, $coupon, $values ) {\n   $id = $product->id;\n   \/\/echo get_post_meta( $id, 'product_tag', true );\n   $terms = wp_get_post_terms( $id, 'product_tag' );\n   foreach ( $terms as $term ) {\n         $term_array&#91;] = $term->name;\n    }\n\n    if( in_array(\"Hendi\", $term_array) ){\n         print_r($term_array);\n    }\n}\n\n\n\n\/\/ hook na aplikovanie kup\u00f3nu\n\/\/add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );\nfunction wc_ninja_apply_coupon( $coupon_code ) {\n\tif ( 'hendiskusobne' === $coupon_code ) {\n           if(current_user_can( 'administrator' )){ \n               echo '&lt;br>hendiskusobne&lt;br>';\n               $coupon_id = 110169;\n               update_post_meta( $coupon_id, 'product_ids', ', ' .  moj_kupon() ) ;\n           }\n\t}\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"310\" src=\"https:\/\/www.areaweb2.pppcreative.sk\/wp-content\/uploads\/2022\/11\/printable-coupons.jpg\" alt=\"\" class=\"wp-image-2253\" srcset=\"https:\/\/www.areaweb.sk\/wp-content\/uploads\/2022\/11\/printable-coupons.jpg 602w, https:\/\/www.areaweb.sk\/wp-content\/uploads\/2022\/11\/printable-coupons-300x154.jpg 300w, https:\/\/www.areaweb.sk\/wp-content\/uploads\/2022\/11\/printable-coupons-583x300.jpg 583w, https:\/\/www.areaweb.sk\/wp-content\/uploads\/2022\/11\/printable-coupons-310x160.jpg 310w, https:\/\/www.areaweb.sk\/wp-content\/uploads\/2022\/11\/printable-coupons-150x77.jpg 150w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Zdroje:<br>https:\/\/quadlayers.com\/exclude-woocommerce-product-from-coupons\/<br>https:\/\/www.phpdevelopment.ca\/2017\/04\/22\/creating-a-custom-woocommerce-coupon\/<br>https:\/\/themes.artbees.net\/blog\/how-to-check-if-a-woocommerce-coupon-is-applied\/<br>https:\/\/quadlayers.com\/edit-woocommerce-coupon-code\/<br>https:\/\/storepro.io\/learn\/how-to-create-a-coupon-programmatically-in-woocommerce\/<br>https:\/\/stackoverflow.com\/questions\/72270940\/get-a-list-of-available-woocommerce-coupons-in-a-select-field<\/p>","protected":false},"excerpt":{"rendered":"<p>Vytvorenie kup\u00f3nu function vytvor_kupon() { $coupon_code = &#8218;MOJKUPON&#8216;; \/\/ Code $amount = &#8217;10&#8216;; \/\/ Amount $discount_type = &#8218;fixed_cart&#8216;; \/\/ Type: fixed_cart, percent, fixed_product, percent_product $coupon = array(\u2026<\/p>","protected":false},"author":1,"featured_media":2251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-2250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocomerce"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/posts\/2250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/comments?post=2250"}],"version-history":[{"count":0,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/posts\/2250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/media\/2251"}],"wp:attachment":[{"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/media?parent=2250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/categories?post=2250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.areaweb.sk\/cs\/wp-json\/wp\/v2\/tags?post=2250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}