Woocommerce.php
144 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 | if ( null === self::$instance ) { |
| 18 | self::$instance = new self(); |
| 19 | self::$instance->doHooks(); |
| 20 | } |
| 21 | return self::$instance; |
| 22 | } |
| 23 | |
| 24 | public function __construct() { |
| 25 | } |
| 26 | |
| 27 | |
| 28 | public function doHooks() { |
| 29 | add_action( 'init', array( $this, 'init' ) ); |
| 30 | } |
| 31 | |
| 32 | public function isActiveWoocommerce() { |
| 33 | if ( class_exists( 'woocommerce' ) ) { |
| 34 | return true; } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | public function init() { |
| 39 | if ( ! $this->isActiveWoocommerce() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $postType = PostType::getInstance(); |
| 44 | $this->activeAccounts = $postType->get_active_woocommerce_accounts(); |
| 45 | $setting = Fields::getWoocommerceSetting(); |
| 46 | |
| 47 | add_filter( 'njt_whatsapp_is_page_or_shop_filter', array( $this, 'isPageOrShop' ), 10, 1 ); |
| 48 | add_filter( 'njt_whatsapp_get_post_id_filter', array( $this, 'getPostId' ), 10, 1 ); |
| 49 | |
| 50 | if ( count( $this->activeAccounts ) === 0 || $setting['isShow'] === 'OFF' ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if ( $setting['position'] === 'after_atc' ) { |
| 55 | add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'insert_button' ) ); |
| 56 | } elseif ( $setting['position'] === 'before_atc' ) { |
| 57 | add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'insert_button' ) ); |
| 58 | } elseif ( $setting['position'] === 'after_short_description' ) { |
| 59 | add_filter( 'woocommerce_short_description', array( $this, 'showAfterShortDescription' ) ); |
| 60 | } elseif ( $setting['position'] === 'after_long_description' ) { |
| 61 | add_filter( 'the_content', array( $this, 'showAfterLongDescription' ) ); |
| 62 | } |
| 63 | |
| 64 | add_filter( 'woocommerce_get_stock_html', array( $this, 'woocommerce_get_stock_html' ), 10, 2 ); |
| 65 | } |
| 66 | |
| 67 | public function woocommerce_get_stock_html( $html, $product ) { |
| 68 | $shouldDisplay = apply_filters( 'njt_wa_out_of_stock_display', false ); |
| 69 | |
| 70 | if ( $product->is_in_stock() || ! $shouldDisplay ) { |
| 71 | return $html; |
| 72 | } |
| 73 | |
| 74 | if ( ! self::$isInserted ) { |
| 75 | self::$isInserted = true; |
| 76 | } else { |
| 77 | return $html; |
| 78 | } |
| 79 | |
| 80 | foreach ( $this->activeAccounts as $row ) { |
| 81 | $html .= '<div class="nta-woo-products-button">' . do_shortcode( '[njwa_button id="' . esc_attr( $row->ID ) . '"]' ) . '</div>'; |
| 82 | } |
| 83 | |
| 84 | return $html; |
| 85 | } |
| 86 | |
| 87 | public function getPostId( $postId ) { |
| 88 | if ( is_shop() ) { |
| 89 | return wc_get_page_id( 'shop' ); |
| 90 | } |
| 91 | return $postId; |
| 92 | } |
| 93 | |
| 94 | public function isPageOrShop( $isPage ) { |
| 95 | if ( $isPage === false && is_shop() ) { |
| 96 | return true; |
| 97 | } |
| 98 | return $isPage; |
| 99 | } |
| 100 | |
| 101 | public function showAfterShortDescription( $post_excerpt ) { |
| 102 | if ( ! is_single() || empty( $post_excerpt ) ) { |
| 103 | return $post_excerpt; |
| 104 | } |
| 105 | if ( ! self::$isInserted ) { |
| 106 | self::$isInserted = true; |
| 107 | } else { |
| 108 | return $post_excerpt; |
| 109 | } |
| 110 | |
| 111 | $btnContent = ''; |
| 112 | foreach ( $this->activeAccounts as $row ) { |
| 113 | $btnContent .= '<div class="nta-woo-products-button">' . do_shortcode( '[njwa_button id="' . $row->ID . '"]' ) . '</div>'; |
| 114 | } |
| 115 | |
| 116 | return $post_excerpt . $btnContent; |
| 117 | } |
| 118 | |
| 119 | public function showAfterLongDescription( $content ) { |
| 120 | if ( 'product' !== get_post_type() || ! is_single() ) { |
| 121 | return $content; |
| 122 | } |
| 123 | |
| 124 | $btnContent = ''; |
| 125 | foreach ( $this->activeAccounts as $row ) { |
| 126 | $btnContent .= '<div class="nta-woo-products-button">' . do_shortcode( '[njwa_button id="' . $row->ID . '"]' ) . '</div>'; |
| 127 | } |
| 128 | |
| 129 | return $content . $btnContent; |
| 130 | } |
| 131 | |
| 132 | public function insert_button() { |
| 133 | if ( ! self::$isInserted ) { |
| 134 | self::$isInserted = true; |
| 135 | } else { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | foreach ( $this->activeAccounts as $row ) { |
| 140 | echo '<div class="nta-woo-products-button">' . do_shortcode( '[njwa_button id="' . esc_attr( $row->ID ) . '"]' ) . '</div>'; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 |