| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Complementary Products Module. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Complementary Products Module. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Complementary_Products extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
*/ |
| 22 |
const MODULE_ID = 'complementary-products'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Module template path. |
| 26 |
*/ |
| 27 |
const MODULE_TEMPLATES_PATH = 'modules/' . self::MODULE_ID; |
| 28 |
|
| 29 |
/** |
| 30 |
* Is module preview. |
| 31 |
* |
| 32 |
*/ |
| 33 |
public static $is_module_preview = false; |
| 34 |
|
| 35 |
/** |
| 36 |
* Set the module as having analytics. |
| 37 |
* |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
protected $has_analytics = true; |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize the module's option group definitions. |
| 44 |
* |
| 45 |
* @return array<int, array<string, mixed>> Array of option group arrays. |
| 46 |
*/ |
| 47 |
protected function init_option_groups() { |
| 48 |
return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor. |
| 53 |
* |
| 54 |
*/ |
| 55 |
public function __construct() { |
| 56 |
// Module id. |
| 57 |
$this->module_id = self::MODULE_ID; |
| 58 |
|
| 59 |
// WooCommerce only. |
| 60 |
$this->wc_only = true; |
| 61 |
|
| 62 |
// Parent construct. |
| 63 |
parent::__construct(); |
| 64 |
|
| 65 |
// Module data. |
| 66 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 67 |
|
| 68 |
// Module section. |
| 69 |
$this->module_section = $this->module_data['section']; |
| 70 |
|
| 71 |
// AI prompt examples. |
| 72 |
$this->ai_examples = array( |
| 73 |
'blurb' => esc_html__( 'See what AI can do for your complementary product offers, from creating a new one to adjusting its discount or where it appears.', 'merchant' ), |
| 74 |
'prompts' => array( |
| 75 |
esc_html__( 'Explore the abilities WPVibe gives you access to, then create a complementary offer that suggests a Yoga Block and Resistance Band alongside the Yoga Mat, at 10% off the bundle', 'merchant' ), |
| 76 |
esc_html__( 'Check what abilities you have available through WPVibe, then set up a complementary offer for the Camping category that adds a Headlamp and Water Filter, with the cheapest item free', 'merchant' ), |
| 77 |
esc_html__( "Look at your available WPVibe abilities, then change the offer title on the Yoga Mat complementary offer to 'Complete Your Home Studio' and move it to show after the product summary", 'merchant' ), |
| 78 |
esc_html__( 'See what abilities WPVibe exposes, then run a complementary offer across all products but exclude anything in the Clearance category', 'merchant' ), |
| 79 |
), |
| 80 |
); |
| 81 |
|
| 82 |
// Module options path. |
| 83 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 84 |
|
| 85 |
// Is module preview page. |
| 86 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 87 |
self::$is_module_preview = true; |
| 88 |
|
| 89 |
// Enqueue admin styles. |
| 90 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 91 |
|
| 92 |
// Enqueue admin scripts. |
| 93 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) ); |
| 94 |
|
| 95 |
// Admin preview box. |
| 96 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( $this->is_module_active() && is_admin() ) { |
| 100 |
// Init translations. |
| 101 |
$this->init_translations(); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! $this->is_module_active() ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
// actions and filters goes here. |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Init translations. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function init_translations() { |
| 116 |
$settings = $this->get_module_settings(); |
| 117 |
if ( ! empty( $settings['offers'] ) ) { |
| 118 |
foreach ( $settings['offers'] as $offer ) { |
| 119 |
if ( ! empty( $offer['product_single_page']['offer-title'] ) ) { |
| 120 |
Merchant_Translator::register_string( $offer['product_single_page']['offer-title'] ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! empty( $offer['product_single_page']['offer-description'] ) ) { |
| 124 |
Merchant_Translator::register_string( $offer['product_single_page']['offer-description'] ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! empty( $offer['cart_page']['title'] ) ) { |
| 128 |
Merchant_Translator::register_string( $offer['cart_page']['title'] ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $offer['cart_page']['button_text'] ) ) { |
| 132 |
Merchant_Translator::register_string( $offer['cart_page']['button_text'] ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! empty( $offer['checkout_page']['title'] ) ) { |
| 136 |
Merchant_Translator::register_string( $offer['checkout_page']['title'] ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! empty( $offer['checkout_page']['offer_description'] ) ) { |
| 140 |
Merchant_Translator::register_string( $offer['checkout_page']['offer_description'] ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! empty( $offer['checkout_page']['button_text'] ) ) { |
| 144 |
Merchant_Translator::register_string( $offer['checkout_page']['button_text'] ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! empty( $offer['thank_you_page']['title'] ) ) { |
| 148 |
Merchant_Translator::register_string( $offer['thank_you_page']['title'] ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! empty( $offer['thank_you_page']['discount_text'] ) ) { |
| 152 |
Merchant_Translator::register_string( $offer['thank_you_page']['discount_text'] ); |
| 153 |
} |
| 154 |
|
| 155 |
if ( ! empty( $offer['thank_you_page']['button_text'] ) ) { |
| 156 |
Merchant_Translator::register_string( $offer['thank_you_page']['button_text'] ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Admin enqueue CSS. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function admin_enqueue_css() { |
| 168 |
if ( $this->is_module_settings_page() ) { |
| 169 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Admin enqueue scripts. |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function admin_enqueue_js() { |
| 179 |
if ( $this->is_module_settings_page() ) { |
| 180 |
wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array( 'jquery' ), |
| 181 |
MERCHANT_VERSION, true ); |
| 182 |
|
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Render admin preview |
| 188 |
* |
| 189 |
* @param Merchant_Admin_Preview $preview |
| 190 |
* @param string $module |
| 191 |
* |
| 192 |
* @return Merchant_Admin_Preview |
| 193 |
*/ |
| 194 |
public function render_admin_preview( $preview, $module ) { |
| 195 |
if ( $module === self::MODULE_ID ) { |
| 196 |
// HTML. |
| 197 |
$preview->set_html( array( $this, 'admin_preview_content' ), $this->get_module_settings() ); |
| 198 |
|
| 199 |
// Simple product add to cart text |
| 200 |
$preview->set_text( 'simple_product_shop_label', '.merchant-preview-add-to-cart-simple' ); |
| 201 |
|
| 202 |
// Variable product add to cart text |
| 203 |
$preview->set_text( 'variable_product_shop_label', '.merchant-preview-add-to-cart-variable' ); |
| 204 |
|
| 205 |
// Out of stock add to cart text |
| 206 |
$preview->set_text( 'out_of_stock_shop_label', '.merchant-preview-add-to-cart-out-of-stock' ); |
| 207 |
|
| 208 |
// Out of stock add to cart text |
| 209 |
$preview->set_class( 'out_of_stock_custom_label', '.merchant-preview-product-out-of-stock', array(), 'display' ); |
| 210 |
} |
| 211 |
|
| 212 |
return $preview; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Admin preview content. |
| 217 |
* |
| 218 |
* @param array $settings |
| 219 |
* |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public function admin_preview_content( $settings ) { |
| 223 |
merchant_get_template_part( |
| 224 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 225 |
'single-product' |
| 226 |
); |
| 227 |
merchant_get_template_part( |
| 228 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 229 |
'cart' |
| 230 |
); |
| 231 |
merchant_get_template_part( |
| 232 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 233 |
'checkout' |
| 234 |
); |
| 235 |
merchant_get_template_part( |
| 236 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 237 |
'thank-you-page' |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
private function is_module_active() { |
| 242 |
return Merchant_Modules::is_module_active( self::MODULE_ID ); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// Initialize the module. |
| 247 |
add_action( 'init', function () { |
| 248 |
Merchant_Modules::create_module( new Merchant_Complementary_Products() ); |
| 249 |
} ); |
| 250 |
|