PluginProbe ʕ •ᴥ•ʔ
WP Chat App / 3.6.1
WP Chat App v3.6.1
3.8.1 3.8.2 trunk 2.6 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5 3.6 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0
wp-whatsapp / includes / Support / Woocommerce.php
wp-whatsapp / includes / Support Last commit date
WPML.php 2 years ago Woocommerce.php 2 years ago
Woocommerce.php
140 lines
1 <?php
2 namespace NTA_WhatsApp\Support;
3
4 use NTA_WhatsApp\Fields;
5 use NTA_WhatsApp\PostType;
6
7
8 defined('ABSPATH') || exit;
9
10 class Woocommerce
11 {
12 protected static $instance = null;
13 protected static $isInserted = false;
14 private $activeAccounts = array();
15
16 public static function getInstance()
17 {
18 if (null == self::$instance) {
19 self::$instance = new self;
20 self::$instance->doHooks();
21 }
22 return self::$instance;
23 }
24
25 public function __construct()
26 {
27 }
28
29
30 public function doHooks() {
31 add_action('init', [$this, 'init']);
32 }
33
34 public function isActiveWoocommerce(){
35 if ( class_exists( 'woocommerce' ) ) { return true; }
36 return false;
37 }
38
39 public function init() {
40 if ( !$this->isActiveWoocommerce() ) return;
41
42 $postType = PostType::getInstance();
43 $this->activeAccounts = $postType->get_active_woocommerce_accounts();
44 $setting = Fields::getWoocommerceSetting();
45
46 add_filter('njt_whatsapp_is_page_or_shop_filter', array($this, 'isPageOrShop'), 10, 1);
47 add_filter('njt_whatsapp_get_post_id_filter', array($this, 'getPostId'), 10, 1);
48
49 if (count($this->activeAccounts) === 0 || $setting['isShow'] === 'OFF') return;
50
51 if ($setting['position'] == 'after_atc') {
52 add_action('woocommerce_after_add_to_cart_button', [$this, 'insert_button']);
53 } elseif ($setting['position'] == 'before_atc') {
54 add_action('woocommerce_before_add_to_cart_button', [$this, 'insert_button']);
55 } elseif ($setting['position'] == 'after_short_description') {
56 add_filter('woocommerce_short_description', [$this, 'showAfterShortDescription']);
57 } elseif ($setting['position'] == 'after_long_description') {
58 add_filter('the_content', [$this, 'showAfterLongDescription']);
59 }
60
61 add_filter('woocommerce_get_stock_html', [$this, 'woocommerce_get_stock_html'], 10, 2);
62 }
63
64 public function woocommerce_get_stock_html( $html, $product){
65 $shouldDisplay = apply_filters('njt_wa_out_of_stock_display', false);
66
67 if ($product->is_in_stock() || !$shouldDisplay) {
68 return $html;
69 }
70
71 if (!self::$isInserted) {
72 self::$isInserted = true;
73 } else {
74 return $html;
75 }
76
77 foreach ($this->activeAccounts as $row) {
78 $html .= '<div class="nta-woo-products-button">' . do_shortcode('[njwa_button id="' . esc_attr($row->ID) . '"]') . '</div>';
79 }
80
81 return $html;
82 }
83
84 public function getPostId($postId){
85 if (is_shop()) return wc_get_page_id('shop');
86 return $postId;
87 }
88
89 public function isPageOrShop($isPage){
90 if ($isPage === false && is_shop()) return true;
91 return $isPage;
92 }
93
94 public function showAfterShortDescription($post_excerpt)
95 {
96 if (!is_single() || empty($post_excerpt)) {
97 return $post_excerpt;
98 }
99 if (!self::$isInserted) {
100 self::$isInserted = true;
101 } else {
102 return $post_excerpt;
103 }
104
105 $btnContent = '';
106 foreach ($this->activeAccounts as $row) {
107 $btnContent .= '<div class="nta-woo-products-button">' . do_shortcode('[njwa_button id="' . $row->ID . '"]') . '</div>';
108 }
109
110 return $post_excerpt . $btnContent;
111 }
112
113 public function showAfterLongDescription($content)
114 {
115 if ('product' !== get_post_type() || !is_single()) {
116 return $content;
117 }
118
119 $btnContent = '';
120 foreach ($this->activeAccounts as $row) {
121 $btnContent .= '<div class="nta-woo-products-button">' . do_shortcode('[njwa_button id="' . $row->ID . '"]') . '</div>';
122 }
123
124 return $content . $btnContent;
125 }
126
127 public function insert_button()
128 {
129 if (!self::$isInserted) {
130 self::$isInserted = true;
131 } else {
132 return;
133 }
134
135 foreach ($this->activeAccounts as $row) {
136 echo '<div class="nta-woo-products-button">' . do_shortcode('[njwa_button id="' . esc_attr($row->ID) . '"]') . '</div>';
137 }
138 }
139 }
140