{"id":38677,"date":"2020-06-17T10:23:11","date_gmt":"2020-06-17T10:23:11","guid":{"rendered":"https:\/\/d689c4c32f5390333.temporary.link\/?p=38677"},"modified":"2023-08-03T16:46:44","modified_gmt":"2023-08-03T15:46:44","slug":"developer-guide-for-addons","status":"publish","type":"post","link":"https:\/\/www.orionorigin.com\/developer-guide-for-addons\/","title":{"rendered":"Developer guide for addons"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>The purpose of this developer guide for addons is to show how to add your own discount to the <a href=\"https:\/\/www.orionorigin.com\">Conditional Discounts for WooCommerce plugin<\/a>. It is important to understand the use or operation of the Conditional Discounts for WooCommerce plugin before reading and practicalizing this documentation.<\/p>\n<p>Before starting it is also essential to define what a hook is. In the <a href=\"https:\/\/www.wordpress.org\" target=\"_blank\" rel=\"noopener\">WordPress<\/a> ecosystem, a <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/\" target=\"_blank\" rel=\"noopener\">hook<\/a> is a means of modifying and\/or interacting with another code at a specific time. There are two types of hooks: <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/actions\/\" target=\"_blank\" rel=\"noopener\">actions<\/a> that allow you to add data or modify the way WordPress works; and <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/filters\/\" target=\"_blank\" rel=\"noopener\">filters<\/a> that allow you to modify the data (variables) of specific functions.<\/p>\n<p>The Conditional Discounts for WooCommerce plugin contains several hooks which you can add to, or manipulate, to create your own discounts. We are going to study them (filters in this case) in the following order:<\/p>\n<ul>\n<li>wad_get_discounts_conditions<\/li>\n<li>wad_fields_values_match<\/li>\n<li>Wad_operators_fields_match<\/li>\n<li>wad_is_rule_valid<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong>wad_get_discounts_conditions<\/strong>: allows you to modify the list of existing conditions or to add new ones.<\/li>\n<li><strong>wad_fields_values_match<\/strong>: is used to define the list of values \u200b\u200bavailable for the condition created using wad_get_discounts_conditions.<\/li>\n<li><strong>wad_operators_fields_match<\/strong>: allows you to define the type of operator which will be used for your discount.<br \/>\nFor example, to verify that a condition belongs to a predefined list we use the operators \u201cIN\u201d and \u201cNOT IN\u201d. In the case of a numerical comparison, we expect the use of operators like superior (&gt;), superior or equal (&gt; =), inferior or equal (&lt;=), inferior or (&lt;), equal ( =) &#8230;<br \/>\nThese are the default operators of our Conditional Discounts for WooCommerce plugin.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>How to add Operators<\/h2>\n<p>We can add our own operators. The format for creating an operator is simple and is as follows:<\/p>\n<p>[php]<br \/>\n$arrays_operators = array(<br \/>\n&#8216;KEY1&#8242;     =&gt;&#8217;VALUE SHOW1&#8217;,<br \/>\n&#8216;KEY2&#8242;     =&gt;&#8217;VALUE SHOW2&#8217;,<br \/>\n);<\/p>\n<p>[\/php]<\/p>\n<p>We create an associative array in which we put the key of our operator and the value that will be displayed for this operator. An application of the previous rule will give the following:<\/p>\n<h4>Example 1:<\/h4>\n<p>[php]<br \/>\n$arrays_operators        = array(<br \/>\n&#8216;IN&#8217;     =&gt; __( &#8216;IN&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;NOT IN&#8217; =&gt; __( &#8216;NOT IN&#8217;, &#8216;wad&#8217; ),<br \/>\n);<\/p>\n<p>[\/php]<\/p>\n<h4>Example 2:<\/h4>\n<p>[php]<br \/>\n$number_operators        = array(<br \/>\n&#8216;&lt;&#8216;  =&gt; __( &#8216;is less than&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;&lt;=&#8217; =&gt; __( &#8216;is less or equal to&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;==&#8217; =&gt; __( &#8216;equals&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;&gt;&#8217;  =&gt; __( &#8216;is more than&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;&gt; =&#8217; =&gt; __( &#8216;is more or equal to&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;%&#8217;  =&gt; __( &#8216;is a multiple of&#8217;, &#8216;wad&#8217; ),<br \/>\n);<\/p>\n<p>[\/php]<\/p>\n<p>So we can create our own operators by following the same rule thanks to the \u201c<strong>wad_operators_fields_match<\/strong>\u201d filter. In the application example (the second part of this document) a complete use of the filter was made and demonstrated.<\/p>\n<figure id=\"attachment_38717\" aria-describedby=\"caption-attachment-38717\" style=\"width: 766px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-38717 size-full\" src=\"https:\/\/www.orionorigin.com\/wp-content\/uploads\/2020\/06\/wad-98-dev-addon-tut-03.png\" alt=\"Developer guide for addons\" width=\"766\" height=\"141\" title=\"\"><figcaption id=\"caption-attachment-38717\" class=\"wp-caption-text\">Rules explained<\/figcaption><\/figure>\n<p>Another simple example, the \u201cif customer role in\u201d rule is associated with a field which contains a dropdown containing the list of existing roles on WordPress. The following image illustrates the example:<\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #ff0000;\">1<\/span>: We have added the condition &#8216;if customer role&#8217; thanks to the &#8216;wad_get_discounts_conditions&#8217; filter.<br \/>\n<span style=\"color: #ff0000;\">2<\/span>: We added the type of operator that we want to use, it is &#8216;IN&#8217; and &#8216;NOT IN&#8217; thanks to the &#8216;wad_operators_fields_match&#8217; filter.<br \/>\n<span style=\"color: #ff0000;\">3<\/span>: These are the values \u200b\u200bthat will be associated with our condition, we added them using the &#8216;wad_fields_values_match&#8217; filter.<\/p>\n<ol start=\"4\">\n<li>&nbsp;<strong>wad_is_rule_valid<\/strong>: this filter allows you to evaluate a condition before applying it or not. &nbsp;For example, to check if an expression \u201cA = B\u201d is true before applying our reduction in price, we need the &#8216;wad_is_rule_valid&#8217; filter. \u2018A\u2019 represents the condition defined by the administrator in the administration space while \u2018B\u2019 represents the second expression which will be compared with \u2018A\u2019 and therefore \u2018B\u2019 will be recovered thanks to a function that we will write ourselves and \u2018A\u2019 will be recovered by the parameter &#8216;$ rule&#8217; of &#8216;wad_is_rule_valid&#8217;.<\/li>\n<\/ol>\n<h2>Application<\/h2>\n<p>We are going to create a discount which lets us reduce the price for a user based on whether he already benefited from certain discounts, which we will define in a list.<\/p>\n<h3>Steps<\/h3>\n<ol>\n<li>We will first add our discount to the list of rules, using the \u201c<span style=\"color: #ff0000;\">wad_get_discounts_conditions<\/span>\u201d filter.<br \/>\nFirst, we add the filter:<\/p>\n<p>[php]<br \/>\nadd_filter( &#8216;wad_get_discounts_conditions&#8217;, &#8216;add_my_custom_condition&#8217;, 10 );<\/p>\n<p>[\/php]<\/p>\n<p>Then we add the callback function linked to this filter:<\/p>\n<p>[php]<\/p>\n<p>\/ **<br \/>\n* This function allows us to add our condition.<br \/>\n*<br \/>\n* @param type $ arrays contains all conditions.<br \/>\n* @return type<br \/>\n* \/<br \/>\nfunction add_my_custom_condition( $arrays ) {<br \/>\n$arrays[&#8216;has-discount&#8217;] = __( &#8216;If Customer has benefited a discount&#8217;, &#8216;wad&#8217; );<br \/>\nreturn $arrays;<br \/>\n}<\/p>\n<p>[\/php]<\/p>\n<p>By so doing, we have added our \u201chas-discount\u201d discount to the list of rules.<\/li>\n<li>The next step is to add the list of values that can be associated with the discount we just created. We do this by using the \u2018<span style=\"color: #ff0000;\"><em>wad_fields_values_match<\/em><\/span>\u2019 filter.<br \/>\nFirst, we call the filter:<\/p>\n<p>[php]<br \/>\nadd_filter(&#8216;wad_fields_values_match&#8217;, &#8216;add_available_option_for_my_custom_condition&#8217;, 10, 3);<\/p>\n<p>[\/php]<\/p>\n<p>Then we call the callback function linked to the filter:<\/p>\n<p>[php]<\/p>\n<p>\/ **<br \/>\n* This function adds the options that are available for our condition.<br \/>\n* @param array $ arr contains all available options for each condition.<br \/>\n* @param type  $ condition contains the condition (or the designation of the discount).<br \/>\n* @param type  $ selected_value contains options that have been selected for the condition ???.<br \/>\n* @return type<br \/>\n* \/<br \/>\nfunction add_available_option_for_my_custom_condition( $arr, $condition, $selected_value ) {<br \/>\n$field_name = &#8216;o-discount [rules] [{rule-group}] [{rule-index}] [value]&#8217;;\/\/ the value of the name attribute of the select field<br \/>\n$discounts_title      = wad_get_all_discounts_post_title();<br \/>\n$discount_get_select = get_wad_html_select( $field_name . &#8216;[]&#8217;, false, &#8221;, $discounts_title, $selected_value, true );\/\/ get_wad_html_select allows you to create a select type field with the parameters given to it; the square brackets ([]) mean that it is a multiple field, if they are not put then a single field will be rendered.<\/p>\n<p>$arr[&#8216;has-discount&#8217;] = $discount_get_select;<br \/>\nreturn $arr;<br \/>\n}<\/p>\n<p>\/ **<br \/>\n* This function return all discounts title<br \/>\n* @return array<br \/>\n* \/<br \/>\nfunction wad_get_all_discounts_post_title() {<br \/>\n$all_discounts   = wad_get_all_discounts();\/\/ we retrieve all discounts. it is a wad&#8217;s function.<br \/>\n$discounts_title = array();<br \/>\nforeach ( $all_discounts as $key =&gt; $value ) {<br \/>\n$discounts_title[ $value-&gt;ID ] = $value-&gt;post_title;<br \/>\n}<br \/>\nreturn $discounts_title;<br \/>\n}<\/p>\n<p>[\/php]<\/li>\n<li>The next step is to add our operators using the \u2018wad_operators_fields_match\u2019 filter. These operators will link our discount to the values we added previously:[php]<br \/>\n&nbsp;     add_filter(&#8216;wad_operators_fields_match&#8217;, &#8216;add_comparison_operators_for_my_custom_condition&#8217;, 10, 3);<\/p>\n<p>[\/php]<\/p>\n<p>Then<\/p>\n<p>[php]<\/p>\n<p>\/ **<br \/>\n* This function allows us to associate comparison operators to our condition.<br \/>\n*<br \/>\n* @param type $ arrays contains the operators associated with each condition.<br \/>\n* @param type $ condition contains the condition (or the designation of the discount).<br \/>\n* @param type $ selected_value contains the operator currently active.<br \/>\n* @return type<br \/>\n* \/<br \/>\nfunction add_comparison_operators_for_my_custom_condition( $arrays, $condition, $selected_value ) {<br \/>\n$field_name  = &#8216;o-discount [rules] [{rule-group}] [{rule-index}] [operator]&#8217;;<br \/>\n$arrays_operators   = array(<br \/>\n&#8216;IN&#8217;     =&gt; __( &#8216;IN&#8217;, &#8216;wad&#8217; ),<br \/>\n&#8216;NOT IN&#8217; =&gt; __( &#8216;NOT IN&#8217;, &#8216;wad&#8217; ),<br \/>\n);<br \/>\n$arrays_operators_select = get_wad_html_select( $field_name, false, &#8221;, $arrays_operators, $selected_value );\/\/ get_wad_html_select allows you to create a select type field with the parameters given to it. it&#8217;s wad&#8217;s function.<br \/>\n$arrays[&#8216;has-discount&#8217;] = $arrays_operators_select;<br \/>\nreturn $arrays;<br \/>\n}<br \/>\n[\/php]<\/li>\n<li>Finally, we will put in measures to apply our discount after checking that the rules defined the match. We will use the \u2018<span style=\"color: #ff0000;\">wad_is_rule_valid<\/span>\u2019 filter to achieve this:[php]<br \/>\nadd_filter(&#8216;wad_is_rule_valid&#8217;, &#8216;is_rule_valid&#8217;, 10, 2);<\/p>\n<p>[\/php]<\/p>\n<p>Then<\/p>\n<p>[php]<br \/>\n\/ **<br \/>\n* Description<br \/>\n*<br \/>\n* @param type $ is_valid after having compared the rules defined in the dashboard and the evaluable condition this boolean allows to say if the rule is valid or not.<br \/>\n* @param type $ rule comment contains the rules defined in the dashboard.<br \/>\n* @return type<br \/>\n* \/<br \/>\nfunction is_rule_valid( $is_valid, $rule ) {<br \/>\n$condition = get_all_discounts_customer_has_benefited(); \/\/ we recover the discounts from which the customer has benefited and we compare it to the rules defined in the dashboard according to the operators.<\/p>\n<p>if ( &#8216;has-discount&#8217; === $rule[&#8216;condition&#8217;]) {<\/p>\n<p>$intersection = array_intersect( $rule[&#8216;value&#8217;], $condition );<br \/>\nif (( &#8216;NOT IN&#8217; == $rule[&#8216;operator&#8217;] &amp;&amp; $rule[&#8216;value&#8217;]! == $intersection ) ||<br \/>\n( &#8216;IN&#8217; === $rule[&#8216;operator&#8217;] &amp;&amp; $rule[&#8216;value&#8217;] === $intersection )) {<br \/>\n$is_valid = true;<br \/>\n}<br \/>\n}<br \/>\nreturn $is_valid;<br \/>\n}<\/p>\n<p>\/ **<br \/>\n* This function recovers all the reductions from which the client has benefited, this will be the evaluable condition that will be used in is_rule_valid function to evaluate our discount.<br \/>\n*<br \/>\n* @return array<br \/>\n* \/<br \/>\nfunction get_all_discounts_customer_has_benefited() {<br \/>\n\/\/ Get all customer orders.<br \/>\n$customer_orders = get_posts(<br \/>\narray(<br \/>\n&#8216;numberposts&#8217; =&gt; -1,<br \/>\n&#8216;meta_key&#8217;    =&gt; &#8216;_customer_user&#8217;,<br \/>\n&#8216;orderby&#8217;     =&gt; &#8216;date&#8217;,<br \/>\n&#8216;order&#8217;       =&gt; &#8216;DESC&#8217;,<br \/>\n&#8216;meta_value&#8217;  =&gt; get_current_user_id(),<br \/>\n&#8216;post_type&#8217;   =&gt; wc_get_order_types(),<br \/>\n&#8216;post_status&#8217; =&gt; array_keys( wc_get_order_statuses()),<br \/>\n&#8216;post_status&#8217; =&gt; array( &#8216;wc-processing&#8217; ),<br \/>\n)<br \/>\n);<\/p>\n<p>\/\/ get a discount that user have benefitted.<br \/>\n$customer_discount_id = array();<br \/>\nforeach ( $customer_orders as $customer_order ) {<br \/>\n$order_data                 = wc_get_order( $customer_order );\/\/ we get the data on each customer&#8217;s order.<br \/>\n$customer_discount_id[] = get_post_meta( $order_data-&gt;get_id(), $key = &#8216;wad_used_discount&#8217;, true );\/\/ get discount that customer has benefited id.<br \/>\n}<br \/>\nreturn $customer_discount_id;<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>In reference to the example scenario mentioned earlier in this tutorial, we have been able to achieve the equation: A = B.<br \/>\nIn this context, the function allows us to recover B (all the discounts from which the user has benefited) via the \u2018get_my_evaluable_condition\u2019 function, and A (the condition established in the dashboard by the administrator) via the \u2018is_rule_valid\u2019 function using the \u2018$rule\u2019 parameter.<br \/>\nGot any questions about this developer guide for addons to our <a href=\"https:\/\/www.orionorigin.com\/product\/conditional-discounts-for-woocommerce\/\">Conditional Discounts for WooCommerce<\/a> plugin? Please reach out to us via Chat!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The purpose of this developer guide for addons is to show how to add your own discount to the Conditional Discounts for WooCommerce plugin. It is important to understand the use or operation of the Conditional Discounts for WooCommerce plugin before reading and practicalizing this documentation. Before starting it is also essential to define [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":149499,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[448],"tags":[258,183,259,36],"class_list":["post-38677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices","tag-addon-developer-guide","tag-conditional-discounts-for-woocommerce","tag-developer","tag-woocommerce"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/posts\/38677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/comments?post=38677"}],"version-history":[{"count":1,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/posts\/38677\/revisions"}],"predecessor-version":[{"id":134471,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/posts\/38677\/revisions\/134471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/media\/149499"}],"wp:attachment":[{"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/media?parent=38677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/categories?post=38677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orionorigin.com\/service\/wp\/v2\/tags?post=38677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}