| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Buy Now. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Buy Now Class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Buy_Now extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
* |
| 22 |
*/ |
| 23 |
const MODULE_ID = 'buy-now'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is module preview. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static $is_module_preview = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
|
| 38 |
// Module section. |
| 39 |
$this->module_section = 'convert-more'; |
| 40 |
|
| 41 |
// Module id. |
| 42 |
$this->module_id = self::MODULE_ID; |
| 43 |
|
| 44 |
// Module default settings. |
| 45 |
$this->module_default_settings = array( |
| 46 |
'button-text' => __( 'Buy Now', 'merchant' ) |
| 47 |
); |
| 48 |
|
| 49 |
// Module data. |
| 50 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 51 |
$this->module_data['preview_url'] = $this->set_module_preview_url( array( 'type' => 'product' ) ); |
| 52 |
|
| 53 |
// Module options path. |
| 54 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 55 |
|
| 56 |
// Is module preview page. |
| 57 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 58 |
self::$is_module_preview = true; |
| 59 |
|
| 60 |
// Enqueue admin styles. |
| 61 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 62 |
|
| 63 |
// Admin preview box. |
| 64 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 65 |
|
| 66 |
// Custom CSS. |
| 67 |
// The custom CSS should be added here as well due to ensure preview box works properly. |
| 68 |
add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// Return early if it's on admin but not in the respective module settings page. |
| 77 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// Enqueue styles. |
| 82 |
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); |
| 83 |
|
| 84 |
// Buy now listener. |
| 85 |
add_action( 'wp', array( $this, 'buy_now_listener' ) ); |
| 86 |
|
| 87 |
// Render buy now button on single product page. |
| 88 |
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'single_product_buy_now_button' ) ); |
| 89 |
|
| 90 |
// Render buy now button on shop archive products. |
| 91 |
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'shop_archive_product_buy_now_button' ), 20 ); |
| 92 |
|
| 93 |
// Custom CSS. |
| 94 |
add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) ); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Admin enqueue CSS. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function admin_enqueue_css() { |
| 104 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 105 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; |
| 106 |
|
| 107 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 108 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/buy-now.min.css', [], MERCHANT_VERSION ); |
| 109 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Enqueue CSS. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function enqueue_css() { |
| 119 |
|
| 120 |
// Specific module styles. |
| 121 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/buy-now.min.css', array(), MERCHANT_VERSION ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Render admin preview |
| 126 |
* |
| 127 |
* @param Merchant_Admin_Preview $preview |
| 128 |
* @param string $module |
| 129 |
* |
| 130 |
* @return Merchant_Admin_Preview |
| 131 |
*/ |
| 132 |
public function render_admin_preview( $preview, $module ) { |
| 133 |
if ( self::MODULE_ID === $module ) { |
| 134 |
// HTML. |
| 135 |
$preview->set_html( array( $this, 'admin_preview_content' ), $this->get_module_settings() ); |
| 136 |
|
| 137 |
// Button Text. |
| 138 |
$preview->set_text( 'button-text', '.merchant-buy-now-button' ); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
return $preview; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Admin preview content. |
| 147 |
* |
| 148 |
* @param array $settings The module settings |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function admin_preview_content( $settings ) { |
| 153 |
?> |
| 154 |
|
| 155 |
<div class="mrc-preview-single-product-elements"> |
| 156 |
<div class="mrc-preview-left-column"> |
| 157 |
<div class="mrc-preview-product-image-wrapper"> |
| 158 |
<div class="mrc-preview-product-image"></div> |
| 159 |
<div class="mrc-preview-product-image-thumbs"> |
| 160 |
<div class="mrc-preview-product-image-thumb"></div> |
| 161 |
<div class="mrc-preview-product-image-thumb"></div> |
| 162 |
<div class="mrc-preview-product-image-thumb"></div> |
| 163 |
</div> |
| 164 |
</div> |
| 165 |
</div> |
| 166 |
<div class="mrc-preview-right-column"> |
| 167 |
<div class="mrc-preview-text-placeholder"></div> |
| 168 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 169 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 170 |
<div class="mrc-preview-text-placeholder mrc-mw-40"></div> |
| 171 |
<a href="#" class="merchant-buy-now-button"><?php echo esc_html( $settings[ 'button-text' ] ); ?></a> |
| 172 |
</div> |
| 173 |
</div> |
| 174 |
|
| 175 |
<?php |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Buy now listener. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public function buy_now_listener() { |
| 184 |
$product_id = ( isset( $_REQUEST['merchant-buy-now'] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['merchant-buy-now'] ) ) : ''; |
| 185 |
|
| 186 |
if ( $product_id ) { |
| 187 |
$variation_id = ( isset( $_REQUEST['variation_id'] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['variation_id'] ) ) : ''; |
| 188 |
if ( $variation_id ) { |
| 189 |
WC()->cart->add_to_cart( $product_id, 1, $variation_id ); |
| 190 |
} else { |
| 191 |
WC()->cart->add_to_cart( $product_id, 1 ); |
| 192 |
} |
| 193 |
|
| 194 |
wp_safe_redirect( wc_get_checkout_url() ); |
| 195 |
|
| 196 |
exit; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Single product buy now button. |
| 202 |
* TODO: Render the output trough template files. |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function single_product_buy_now_button() { |
| 207 |
global $post, $product; |
| 208 |
|
| 209 |
if ( ! empty( $product ) ) { |
| 210 |
if ( 'yes' == get_post_meta( $post->ID, '_is_pre_order', true ) && strtotime( get_post_meta( $post->ID, '_pre_order_date', true ) ) > time() ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
$text = Merchant_Admin_Options::get( 'buy-now', 'button-text', esc_html__( 'Buy Now', 'merchant' ) ); |
| 216 |
|
| 217 |
?> |
| 218 |
|
| 219 |
<button type="submit" name="merchant-buy-now" value="<?php echo esc_attr( $product->get_ID() ); ?>" class="single_add_to_cart_button button alt wp-element-button merchant-buy-now-button"><?php echo esc_html( $text ); ?></button> |
| 220 |
|
| 221 |
<?php |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Shop archive product buy now button. |
| 226 |
* TODO: Render the output trough template files. |
| 227 |
* |
| 228 |
* @return void |
| 229 |
*/ |
| 230 |
public function shop_archive_product_buy_now_button() { |
| 231 |
global $post, $product; |
| 232 |
|
| 233 |
if ( ! $product->is_type( 'simple' ) ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! empty( $product ) ) { |
| 238 |
if ( 'yes' == get_post_meta( $post->ID, '_is_pre_order', true ) && strtotime( get_post_meta( $post->ID, '_pre_order_date', true ) ) > time() ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
$text = Merchant_Admin_Options::get( 'buy-now', 'button-text', esc_html__( 'Buy Now', 'merchant' ) ); |
| 244 |
|
| 245 |
?> |
| 246 |
|
| 247 |
<a href="<?php echo esc_url( add_query_arg( array( 'merchant-buy-now' => $product->get_ID() ), wc_get_checkout_url() ) ); ?>" class="button alt wp-element-button product_type_simple add_to_cart_button merchant-buy-now-button"><?php echo esc_html( $text ); ?></a> |
| 248 |
|
| 249 |
<?php |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Custom CSS. |
| 254 |
* |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
public function get_module_custom_css() { |
| 258 |
$css = ''; |
| 259 |
|
| 260 |
// Text Color. |
| 261 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'text-color', '#ffffff', '.merchant-buy-now-button', '--mrc-buy-now-text-color' ); |
| 262 |
|
| 263 |
// Text Color (hover). |
| 264 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'text-hover-color', '#ffffff', '.merchant-buy-now-button', '--mrc-buy-now-text-hover-color' ); |
| 265 |
|
| 266 |
// Border Color. |
| 267 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'border-color', '#212121', '.merchant-buy-now-button', '--mrc-buy-now-border-color' ); |
| 268 |
|
| 269 |
// Border Color (hover). |
| 270 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'border-hover-color', '#414141', '.merchant-buy-now-button', '--mrc-buy-now-border-hover-color' ); |
| 271 |
|
| 272 |
// Background Color. |
| 273 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'background-color', '#212121', '.merchant-buy-now-button', '--mrc-buy-now-background-color' ); |
| 274 |
|
| 275 |
// Background Color (hover). |
| 276 |
$css .= Merchant_Custom_CSS::get_variable_css( 'buy-now', 'background-hover-color', '#414141', '.merchant-buy-now-button', '--mrc-buy-now-background-hover-color' ); |
| 277 |
|
| 278 |
return $css; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Admin custom CSS. |
| 283 |
* |
| 284 |
* @param string $css The custom CSS. |
| 285 |
* @return string $css The custom CSS. |
| 286 |
*/ |
| 287 |
public function admin_custom_css( $css ) { |
| 288 |
$css .= $this->get_module_custom_css(); |
| 289 |
|
| 290 |
return $css; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Frontend custom CSS. |
| 295 |
* |
| 296 |
* @param string $css The custom CSS. |
| 297 |
* @return string $css The custom CSS. |
| 298 |
*/ |
| 299 |
public function frontend_custom_css( $css ) { |
| 300 |
$css .= $this->get_module_custom_css(); |
| 301 |
|
| 302 |
return $css; |
| 303 |
} |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
// Initialize the module. |
| 308 |
add_action( 'init', function() { |
| 309 |
new Merchant_Buy_Now(); |
| 310 |
} ); |
| 311 |
|