{"id":197247,"date":"2016-01-26T10:06:53","date_gmt":"2016-01-26T10:06:53","guid":{"rendered":""},"modified":"2016-07-31T01:45:01","modified_gmt":"2016-07-31T01:45:01","slug":"woocommerce-if-%d1%81%d1%83%d0%bc%d0%bc%d0%b0-%d0%b7%d0%b0%d0%ba%d0%b0%d0%b7%d0%b0-%d1%8e%d0%b7%d0%b5%d1%80%d0%b0-n-%d1%82%d0%be-%d1%81%d0%bc%d0%b5%d0%bd%d0%b8%d1%82%d1%8c-%d1%80%d0%be%d0%bb%d1%8c","status":"publish","type":"topic","link":"https:\/\/ru.wordpress.org\/support\/topic\/woocommerce-if-%d1%81%d1%83%d0%bc%d0%bc%d0%b0-%d0%b7%d0%b0%d0%ba%d0%b0%d0%b7%d0%b0-%d1%8e%d0%b7%d0%b5%d1%80%d0%b0-n-%d1%82%d0%be-%d1%81%d0%bc%d0%b5%d0%bd%d0%b8%d1%82%d1%8c-%d1%80%d0%be%d0%bb%d1%8c\/","title":{"rendered":"Woocommerce &#8212; If \u0441\u0443\u043c\u043c\u0430 \u0437\u0430\u043a\u0430\u0437\u0430 \u044e\u0437\u0435\u0440\u0430 &gt;N, \u0442\u043e \u0441\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u043e\u043b\u044c \u043d\u0430 S"},"content":{"rendered":"<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442, \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0438\u043d\u044f\u044e\u0441\u044c, \u043e\u043f\u044b\u0442\u0430 \u0432 php \u043c\u0430\u043b\u043e. \u0415\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0440\u043e\u043b\u0435\u0439 (Silver, Gold) \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u044e\u0442 \u0437\u0430\u043a\u0430\u0437\u044b \u043d\u0430 \u043e\u0431\u0449\u0443\u044e \u0441\u0443\u043c\u043c\u0443 5000 \u0438 10000 \u0440\u0443\u0431\u043b\u0435\u0439. (\u0437\u0430 \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f)<\/p>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430<br \/>\n&#8212; \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0440\u043e\u043b\u044c Customer \u0438 \u0441\u0443\u043c\u043c\u0430 \u043e\u0442 0 \u0434\u043e 4999 \u0440\u0443\u0431\u043b\u0435\u0439 (\u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c)<br \/>\n&#8212; \u041f\u0440\u0438 \u0441\u0443\u043c\u043c\u0435 \u043e\u0442 5000 \u0434\u043e 9999 \u0440\u0443\u0431\u043b\u0435\u0439 \u0435\u043c\u0443 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u043b\u044c Silver<br \/>\n&#8212; \u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u043c\u043c\u0430 \u043f\u0435\u0440\u0435\u0432\u0430\u043b\u0438\u0442 \u0437\u0430 10 000 \u0440\u0443\u0431\u043b\u0435\u0439, \u0440\u043e\u043b\u044c Silver \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u0440\u043e\u043b\u044c Gold<\/p>\n<p>\u041f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u0440\u043e\u043b\u0438, \u043d\u043e \u0434\u0430\u0436\u0435 \u043e\u043d\u0430 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f. \u041f\u043e\u0434\u0441\u043a\u0430\u0436\u0438\u0442\u0435 \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0447\u0442\u043e \u043d\u0435 \u0442\u0430\u043a? \u041a\u043e\u0434 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0432 functions.php<\/p>\n<pre><code>add_action(&#039;woocommerce_payment_complete&#039;, function($order_id) {\n\n  $order = new WC_Order($order);\n  $user_id = $order-&gt;user_id;\n\n  if($user_id) {\n    customer_change_role($user_id, 5000, &#039;silver&#039;);\n  }\n\n});\n\n\/**\n * Gets the amount an email has spent\n * @param  string $email Users email\n * @return float\n *\/\nfunction woocommerce_get_amount_spent($email) {\n\n  $orders = get_posts( array(\n      &#039;meta_key&#039;        =&gt; &#039;_billing_email&#039;,\n      &#039;meta_value&#039;      =&gt; $email,\n      &#039;post_type&#039;       =&gt; &#039;shop_order&#039;,\n      &#039;posts_per_page&#039;  =&gt;-1\n  ) );\n\n  $spent = 0;\n  foreach($orders as $shop_order) {\n    $order = new WC_Order($shop_order-&gt;ID);\n    $spent += $order-&gt;get_total();\n  }\n\n  return $spent;\n\n}\n\n\/**\n * Change a customers role when they spend big money\n * @param  integer  $user_id    The ID of the user. Current user when none supplied\n * @param  integer  $threshold  How much money do we need?\n * @param  string   $role     Which role to give\n * @return void\n *\/\nfunction customer_change_role($user_id = null, $threshold = 5000, $role = &#039;silver&#039;) {\n\n  \/\/ when the user_id was not supplied we&#039;ll try and figure it out\n  if($user_id === null) {\n    $user_id = get_current_user_id();\n  }\n\n  \/\/ this probably means the user is not logged in\n  if( !$user_id ) {\n    return;\n  }\n\n  \/\/ get the user object\n  $user = get_user_by(&#039;id&#039;, $user_id);\n\n  \/\/ nope, something went wrong\n  if(!$user) {\n    return;\n  }\n\n  \/\/ the user is a big spender, lets give him a role\n  if(woocommerce_get_amount_spent($user-&gt;data-&gt;user_email) &gt;= $threshold) {\n    $user-&gt;add_role($role);\n  }\n  \/\/ this mofo ain&#039;t using money. take it back!\n  else {\n    $user-&gt;remove_role($role);\n  }\n\n}<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-197247","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/197247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/197247\/revisions"}],"wp:attachment":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=197247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}