WP init
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Add_Remove_From_Compare setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Add_Remove_From_Compare {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// add_action('init', [$this, 'register_compare_cpt']);
|
||||
add_action( 'wp_ajax_add_to_compare',[$this, 'add_to_compare'] );
|
||||
add_action( 'wp_ajax_nopriv_add_to_compare',[$this, 'add_to_compare'] );
|
||||
add_action( 'wp_ajax_remove_from_compare', [$this, 'remove_from_compare'] );
|
||||
add_action( 'wp_ajax_nopriv_remove_from_compare', [$this, 'remove_from_compare'] );
|
||||
}
|
||||
|
||||
// // Register Post Type
|
||||
// function register_compare_cpt() {
|
||||
// $args = array(
|
||||
// 'label' => esc_html__( 'Royal compare', 'wpr-addons' ),
|
||||
// 'public' => true,
|
||||
// 'publicly_queryable' => true,
|
||||
// 'rewrite' => false,
|
||||
// 'show_ui' => true,
|
||||
// 'show_in_menu' => true,
|
||||
// 'show_in_nav_menus' => false,
|
||||
// 'exclude_from_search' => true,
|
||||
// 'capability_type' => 'post',
|
||||
// 'supports' => array( 'title', 'editor', 'elementor' ),
|
||||
// 'hierarchical' => false,
|
||||
// );
|
||||
|
||||
// register_post_type( 'compare', $args );
|
||||
// }
|
||||
|
||||
function add_to_compare() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( ! wp_verify_nonce( $nonce, 'wpr-addons-js' ) || ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
// $compare = get_user_meta( get_current_user_id(), 'wpr_compare', true );
|
||||
// if ( ! $compare ) {
|
||||
// $compare = array();
|
||||
// }
|
||||
// if ( in_array( $product_id, $compare ) ) {
|
||||
// wp_send_json_error( array( 'message' => esc_html__('Product is already in Compare.', 'wpr-addons') ) );
|
||||
// return;
|
||||
// }
|
||||
// $compare[] = $product_id;
|
||||
// update_user_meta( get_current_user_id(), 'wpr_compare', $compare );
|
||||
|
||||
|
||||
// NEW CODE
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta($user_id, 'wpr_compare', true);
|
||||
if (!$compare) {
|
||||
$compare = array();
|
||||
}
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
if (in_array($product_id, $compare)) {
|
||||
wp_send_json_error(array('message' => esc_html__('Product is already in compare.', 'wpr-addons')));
|
||||
return;
|
||||
}
|
||||
|
||||
$compare[] = $product_id;
|
||||
|
||||
if ($user_id > 0) {
|
||||
update_user_meta($user_id, 'wpr_compare', $compare);
|
||||
} else {
|
||||
$this->set_compare_to_cookie($compare);
|
||||
}
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
function remove_from_compare() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( ! wp_verify_nonce( $nonce, 'wpr-addons-js' ) || ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta($user_id, 'wpr_compare', true);
|
||||
if (!$compare) {
|
||||
$compare = array();
|
||||
}
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
$compare = array_diff($compare, array($product_id));
|
||||
|
||||
if ($user_id > 0) {
|
||||
update_user_meta($user_id, 'wpr_compare', $compare);
|
||||
} else {
|
||||
$this->set_compare_to_cookie($compare);
|
||||
}
|
||||
|
||||
// $compare = get_user_meta( get_current_user_id(), 'wpr_compare', true );
|
||||
// if ( ! $compare ) {
|
||||
// $compare = array();
|
||||
// }
|
||||
// $compare = array_diff( $compare, array( $product_id ) );
|
||||
// update_user_meta( get_current_user_id(), 'wpr_compare', $compare );
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
function get_compare_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_compare'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare']), true);
|
||||
} else if ( isset($_COOKIE['wpr_compare_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function set_compare_to_cookie($compare) {
|
||||
if ( is_multisite() ) {
|
||||
setcookie('wpr_compare_'. get_current_blog_id() .'', json_encode($compare), time() + (86400 * 10), '/'); // Expires in 7 days
|
||||
} else {
|
||||
setcookie('wpr_compare', json_encode($compare), time() + (86400 * 10), '/'); // Expires in 7 days
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Add_Remove_From_Compare();
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Add_Remove_From_Wishlist setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Add_Remove_From_Wishlist {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// add_action('init', [$this, 'register_wishlist_cpt']);
|
||||
add_action( 'wp_ajax_add_to_wishlist',[$this, 'add_to_wishlist'] );
|
||||
add_action( 'wp_ajax_nopriv_add_to_wishlist',[$this, 'add_to_wishlist'] );
|
||||
add_action( 'wp_ajax_remove_from_wishlist', [$this, 'remove_from_wishlist'] );
|
||||
add_action( 'wp_ajax_nopriv_remove_from_wishlist', [$this, 'remove_from_wishlist'] );
|
||||
}
|
||||
|
||||
function add_to_wishlist() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( ! wp_verify_nonce($nonce, 'wpr-addons-js') || ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
// NEW CODE
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta( get_current_user_id(), 'wpr_wishlist', true );
|
||||
$wishlist = get_user_meta($user_id, 'wpr_wishlist', true);
|
||||
if (!$wishlist) {
|
||||
$wishlist = array();
|
||||
}
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
if (in_array($product_id, $wishlist)) {
|
||||
wp_send_json_error(array('message' => esc_html__('Product is already in wishlist.', 'wpr-addons')));
|
||||
return;
|
||||
}
|
||||
|
||||
$wishlist[] = $product_id;
|
||||
|
||||
if ($user_id > 0) {
|
||||
update_user_meta($user_id, 'wpr_wishlist', $wishlist);
|
||||
} else {
|
||||
$this->set_wishlist_to_cookie($wishlist);
|
||||
}
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
function remove_from_wishlist() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( ! wp_verify_nonce($nonce, 'wpr-addons-js') || ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta($user_id, 'wpr_wishlist', true);
|
||||
if (!$wishlist) {
|
||||
$wishlist = array();
|
||||
}
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
$wishlist = array_diff($wishlist, array($product_id));
|
||||
|
||||
if ($user_id > 0) {
|
||||
update_user_meta($user_id, 'wpr_wishlist', $wishlist);
|
||||
} else {
|
||||
$this->set_wishlist_to_cookie($wishlist);
|
||||
}
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
function get_wishlist_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_wishlist'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist']), true);
|
||||
} else if ( isset($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function set_wishlist_to_cookie($wishlist) {
|
||||
if ( is_multisite() ) {
|
||||
setcookie('wpr_wishlist_'. get_current_blog_id() .'', json_encode($wishlist), time() + (86400 * 10), '/'); // Expires in 7 days
|
||||
} else {
|
||||
setcookie('wpr_wishlist', json_encode($wishlist), time() + (86400 * 10), '/'); // Expires in 7 days
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Add_Remove_From_Wishlist();
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Check_Product setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Check_Product {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'wp_ajax_check_product_in_wishlist', [$this, 'check_product_in_wishlist'] );
|
||||
add_action( 'wp_ajax_nopriv_check_product_in_wishlist', [$this, 'check_product_in_wishlist'] );
|
||||
add_action( 'wp_ajax_check_product_in_compare', [$this, 'check_product_in_compare'] );
|
||||
add_action( 'wp_ajax_nopriv_check_product_in_compare', [$this, 'check_product_in_compare'] );
|
||||
add_action( 'wp_ajax_check_product_in_wishlist_grid', [$this, 'check_product_in_wishlist_grid'] );
|
||||
add_action( 'wp_ajax_nopriv_check_product_in_wishlist_grid', [$this, 'check_product_in_wishlist_grid'] );
|
||||
add_action( 'wp_ajax_check_product_in_compare_grid', [$this, 'check_product_in_compare_grid'] );
|
||||
add_action( 'wp_ajax_nopriv_check_product_in_compare_grid', [$this, 'check_product_in_compare_grid'] );
|
||||
}
|
||||
|
||||
// Add two new functions for handling cookies
|
||||
public function get_wishlist_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_wishlist'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist']), true);
|
||||
} else if ( isset($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function get_compare_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_compare'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare']), true);
|
||||
} else if ( isset($_COOKIE['wpr_compare_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function check_product_in_wishlist() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ( !isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta( $user_id, 'wpr_wishlist', true );
|
||||
|
||||
if ( ! $wishlist ) {
|
||||
$wishlist = array();
|
||||
}
|
||||
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
wp_send_json(in_array( $_POST['product_id'], $wishlist ));
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
function check_product_in_compare() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ( !isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta( $user_id, 'wpr_compare', true );
|
||||
|
||||
if ( ! $compare ) {
|
||||
$compare = array();
|
||||
}
|
||||
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
wp_send_json(in_array( $_POST['product_id'], $compare ));
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
function check_product_in_wishlist_grid() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta( $user_id, 'wpr_wishlist', true );
|
||||
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
if ( ! $wishlist ) {
|
||||
$wishlist = array();
|
||||
}
|
||||
|
||||
wp_send_json($wishlist);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
function check_product_in_compare_grid() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta( $user_id, 'wpr_compare', true );
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
if ( ! $compare ) {
|
||||
$compare = array();
|
||||
}
|
||||
|
||||
wp_send_json($compare);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Check_Product();
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
use Elementor\Plugin;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Compare_Popup_Action setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Compare_Popup_Action {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_get_page_content', [$this, 'wpr_get_page_content']);
|
||||
add_action('wp_ajax_nopriv_wpr_get_page_content', [$this, 'wpr_get_page_content']);
|
||||
// add_action('rest_api_init', [$this, 'register_compare_custom_routes']);
|
||||
}
|
||||
|
||||
// function register_compare_custom_routes() {
|
||||
// register_rest_route('wpr-addons/v1', '/page-content/(?P<id>\d+)', array(
|
||||
// 'methods' => 'GET',
|
||||
// 'callback' => 'wpr_get_page_content'
|
||||
// ));
|
||||
// }
|
||||
|
||||
function wpr_get_page_content($request) {
|
||||
|
||||
$page_id = $_POST['wpr_compare_page_id'];
|
||||
|
||||
if ( post_password_required($page_id) || 'publish' !== get_post_status($page_id) ) {
|
||||
wp_send_json_error(array(
|
||||
'message' => esc_html__('Security check failed.', 'wpr-addons'),
|
||||
));
|
||||
}
|
||||
|
||||
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wpr-addons-js')) {
|
||||
wp_send_json_error(array(
|
||||
'message' => esc_html__('Security check failed.', 'wpr-addons'),
|
||||
));
|
||||
}
|
||||
|
||||
// $page_id = $request->get_param('id');
|
||||
|
||||
// Check if the page was created with Elementor
|
||||
if (\Elementor\Plugin::$instance->db->is_built_with_elementor($page_id)) {
|
||||
$content = \Elementor\Plugin::$instance->frontend->get_builder_content($page_id);
|
||||
wp_send_json_success(array('content' => $content, 'page_url' => get_page_link( $page_id )));
|
||||
// return new WP_REST_Response(array('content' => $content), 200);
|
||||
} else {
|
||||
$page = get_post($page_id);
|
||||
if ($page) {
|
||||
$content = apply_filters('the_content', $page->post_content);
|
||||
wp_send_json_success(array('content' => $content));
|
||||
// return new WP_REST_Response(array('content' => $content), 200);
|
||||
} else {
|
||||
wp_send_json_error(array('message' => 'Page not found'));
|
||||
// return new WP_Error('page_not_found', 'Page not found', array('status' => 404));
|
||||
}
|
||||
}
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Compare_Popup_Action();
|
||||
+400
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Count_Wishlist_Compare_Items setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Count_Wishlist_Compare_Items {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'wp_ajax_count_wishlist_items',[$this, 'count_wishlist_items'] );
|
||||
add_action( 'wp_ajax_nopriv_count_wishlist_items',[$this, 'count_wishlist_items'] );
|
||||
add_action( 'wp_ajax_count_compare_items',[$this, 'count_compare_items'] );
|
||||
add_action( 'wp_ajax_nopriv_count_compare_items',[$this, 'count_compare_items'] );
|
||||
}
|
||||
|
||||
// Add two new functions for handling cookies
|
||||
public function get_wishlist_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_wishlist'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist']), true);
|
||||
} else if ( isset($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function get_compare_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_compare'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare']), true);
|
||||
} else if ( isset($_COOKIE['wpr_compare_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function count_wishlist_items() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta($user_id, 'wpr_wishlist', true);
|
||||
if (!$wishlist) {
|
||||
$wishlist = array();
|
||||
}
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
$product_data = [];
|
||||
|
||||
$product_data['wishlist_count'] = sizeof($wishlist);
|
||||
$wishlist_product_array = [];
|
||||
|
||||
$settings = [
|
||||
'element_addcart_simple_txt' => isset($_POST['element_addcart_simple_txt']) ? $_POST['element_addcart_simple_txt'] : '',
|
||||
'element_addcart_grouped_txt' => isset($_POST['element_addcart_grouped_txt']) ? $_POST['element_addcart_grouped_txt'] : '',
|
||||
'element_addcart_variable_txt' => isset($_POST['element_addcart_variable_txt']) ? $_POST['element_addcart_grouped_txt'] : ''
|
||||
];
|
||||
|
||||
foreach ($wishlist as $product_id) {
|
||||
$product = wc_get_product($product_id);
|
||||
|
||||
if ($product) {
|
||||
$wishlist_product_array[] = [
|
||||
'product_id' => $product_id,
|
||||
'product_image' => $product->get_image(),
|
||||
'product_title' => $product->get_title(),
|
||||
'product_url' => $product->get_permalink(),
|
||||
'product_price' => $product->get_price_html(),
|
||||
'product_stock' => $product->get_stock_status() == 'instock' ? esc_html__('In Stock', 'wpr-addons') : esc_html__('Out of Stock', 'wpr-addons'),
|
||||
'product_atc' => $this->render_product_add_to_cart($settings, $product)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$product_data['wishlist_items'] = $wishlist_product_array;
|
||||
|
||||
wp_send_json($product_data);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
public function count_compare_items() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta($user_id, 'wpr_compare', true);
|
||||
if (!$compare) {
|
||||
$compare = array();
|
||||
}
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
$product_data = [];
|
||||
|
||||
$product_data['compare_count'] = sizeof($compare);
|
||||
$product_data['compare_table'] = $this->compare_table();
|
||||
|
||||
wp_send_json($product_data);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
// Function to get the attribute taxonomy from the label
|
||||
public function get_taxonomy_from_label( $label ) {
|
||||
global $wpdb;
|
||||
$taxonomy = $wpdb->get_var( $wpdb->prepare("
|
||||
SELECT attribute_name
|
||||
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
|
||||
WHERE attribute_label = %s
|
||||
LIMIT 1
|
||||
", $label) );
|
||||
|
||||
return $taxonomy ? 'pa_' . $taxonomy : '';
|
||||
}
|
||||
|
||||
public function compare_table() {
|
||||
$user_id = get_current_user_id();
|
||||
$notification_hidden = '';
|
||||
$table_hidden = '';
|
||||
|
||||
$settings = [
|
||||
'compare_empty_text' => isset($_POST['compare_empty_text']) ? $_POST['compare_empty_text'] : '',
|
||||
'remove_from_compare_text' => isset($_POST['remove_text']) ? sanitize_text_field($_POST['remove_text']) : '',
|
||||
'element_addcart_simple_txt' => isset($_POST['element_addcart_simple_txt']) ? $_POST['element_addcart_simple_txt'] : '',
|
||||
'element_addcart_grouped_txt' => isset($_POST['element_addcart_grouped_txt']) ? $_POST['element_addcart_grouped_txt'] : '',
|
||||
'element_addcart_variable_txt' => isset($_POST['element_addcart_variable_txt']) ? $_POST['element_addcart_grouped_txt'] : ''
|
||||
];
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta( get_current_user_id(), 'wpr_compare', true );
|
||||
|
||||
if ( ! $compare ) {
|
||||
$compare = array();
|
||||
}
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
if ( ! $compare ) {
|
||||
$table_hidden = 'wpr-hidden-element';
|
||||
} else {
|
||||
$notification_hidden = 'wpr-hidden-element';
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
echo '<p class="wpr-compare-empty '. $notification_hidden .'">'. $settings['compare_empty_text'] .'</p>';
|
||||
|
||||
// Start the table
|
||||
echo '<div class="wpr-compare-products '. $table_hidden .'">';
|
||||
echo '<table class="wpr-compare-table">';
|
||||
|
||||
// Create the first row of table headers
|
||||
echo '<tr>';
|
||||
echo '<th></th>'; // Blank space for top-left corner
|
||||
foreach ( $compare as $product_id ) {
|
||||
$product = wc_get_product( $product_id );
|
||||
if ( ! $product ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$remove_text = $settings['remove_from_compare_text'];
|
||||
|
||||
// Translators: %s will be replaced by the text for removing from compare
|
||||
$remove_text_tr = sprintf( __( '%s', 'wpr-addons' ), $remove_text );
|
||||
|
||||
echo '<th data-product-id="' . esc_attr( $product->get_id() ) . '">
|
||||
<button class="wpr-compare-remove" data-product-id="' . esc_attr( $product->get_id() ) . '">' . esc_html( $remove_text_tr ) . '</button>
|
||||
</th>';
|
||||
|
||||
}
|
||||
echo '</tr>';
|
||||
|
||||
// Create the remaining rows of the table
|
||||
$table_data = array(
|
||||
['label' => esc_html__('Image', 'wpr-addons'), 'type' => 'image'],
|
||||
['label' => esc_html__('Name', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Rating', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Description', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Price', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('SKU', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Stock Status', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Dimensions', 'wpr-addons'), 'type' => 'text'],
|
||||
['label' => esc_html__('Weight', 'wpr-addons'), 'type' => 'text']
|
||||
);
|
||||
|
||||
|
||||
$all_attributes = array();
|
||||
|
||||
foreach ( $compare as $product_id ) {
|
||||
$product = wc_get_product( $product_id );
|
||||
|
||||
if ( ! $product ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes = $product->get_attributes();
|
||||
|
||||
foreach ( $attributes as $attribute ) {
|
||||
$attribute_name = wc_attribute_label($attribute->get_name());
|
||||
if ( !in_array($attribute_name, $all_attributes) ) {
|
||||
$all_attributes[] = $attribute_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $all_attributes as $attribute_name ) {
|
||||
$table_data[] = array('label' => $attribute_name, 'type' => 'text');
|
||||
}
|
||||
|
||||
foreach ( $table_data as $row ) {
|
||||
echo '<tr>';
|
||||
echo '<th>' . $row['label'] . '</th>';
|
||||
foreach ( $compare as $key => $product_id ) {
|
||||
$product = wc_get_product( $product_id );
|
||||
if ( ! $product ) {
|
||||
continue;
|
||||
}
|
||||
echo '<td data-product-id="' . $product->get_id() . '">';
|
||||
switch ( $row['type'] ) {
|
||||
case 'image':
|
||||
echo '<a class="wpr-compare-img-wrap" href="' . $product->get_permalink() . '">' . $product->get_image() . '</a>';
|
||||
echo '<div class="wpr-compare-product-atc">' . $this->render_product_add_to_cart( $settings, $product ) . '</div>';
|
||||
break;
|
||||
case 'text':
|
||||
if( in_array(strtolower($row['label']), [strtolower(__('Description', 'wpr-addons')), strtolower(__('SKU', 'wpr-addons')) ]) ) {
|
||||
echo $product->get_data()[strtolower($row['label'])];
|
||||
} else if ( strtolower($row['label']) == strtolower(__('Name', 'wpr-addons')) ) {
|
||||
echo '<a class="wpr-compare-product-name" href="' . $product->get_permalink() . '">'. $product->get_data()['name'] .'</a>';
|
||||
} else if ( strtolower($row['label']) == strtolower(__('Price', 'wpr-addons')) ) {
|
||||
echo $product->get_price_html();
|
||||
} else if ( strtolower($row['label']) ==strtolower(__('Rating', 'wpr-addons')) ) {
|
||||
echo $this->render_product_rating($product);
|
||||
} else if ( strtolower($row['label']) == strtolower(__('Stock Status', 'wpr-addons')) ) {
|
||||
$stock_status = $product->get_stock_status();
|
||||
echo $stock_status == 'instock' ? esc_html__('In Stock', 'wpr-addons') : esc_html__('Out of Stock', 'wpr-addons');
|
||||
} else if ( strtolower($row['label']) == strtolower(__('Dimensions', 'wpr-addons')) ) {
|
||||
|
||||
if ( $product->has_dimensions() ) {
|
||||
$dimensions = sprintf(
|
||||
'<span class="wpr-dimensions">%s</span>',
|
||||
wc_format_dimensions( $product->get_dimensions( false ) )
|
||||
);
|
||||
|
||||
echo $dimensions;
|
||||
}
|
||||
|
||||
} else if ( strtolower($row['label']) == strtolower(__('Weight', 'wpr-addons')) ) {
|
||||
|
||||
if ( $product->get_weight() ) {
|
||||
$weight = sprintf(
|
||||
'<span class="wpr-weight">%s %s</span>',
|
||||
$product->get_weight(),
|
||||
get_option( 'woocommerce_weight_unit' )
|
||||
);
|
||||
|
||||
echo $weight;
|
||||
}
|
||||
|
||||
} else {
|
||||
$attributes = $product->get_attributes();
|
||||
$attribute_name = wc_attribute_label(strtolower($row['label']));
|
||||
|
||||
foreach ($product->get_attributes($product_id) as $attr) {
|
||||
|
||||
if ( strtolower($attr['name']) === strtolower($row['label']) ) {
|
||||
echo $attr['value'];
|
||||
}
|
||||
}
|
||||
|
||||
// Product Attributes
|
||||
// $attribute_name = $this->format_variable($attribute_name);
|
||||
$attribute_name = $this->get_taxonomy_from_label($attribute_name);
|
||||
|
||||
if (isset($attributes[$attribute_name])) {
|
||||
// Get the value(s) of the 'dimensions' attribute for the product
|
||||
$attributes_value = $attributes[$attribute_name]->get_options();
|
||||
$attributes_value_array = [];
|
||||
|
||||
// Loop through the values and output them
|
||||
foreach ($attributes_value as $value) {
|
||||
$term = get_term($value);
|
||||
$attributes_value_array[] = $term->name;
|
||||
}
|
||||
|
||||
echo implode(' | ', $attributes_value_array);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
echo '</td>';
|
||||
}
|
||||
echo '</tr>';
|
||||
}
|
||||
|
||||
// Close the table
|
||||
echo '</table>';
|
||||
echo '</div>';
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
public function format_variable($variable) {
|
||||
$variable = strtolower($variable); // Convert to lowercase
|
||||
$variable = str_replace(' ', '-', $variable); // Replace spaces with hyphens
|
||||
return $variable;
|
||||
}
|
||||
|
||||
public function render_product_rating($product) {
|
||||
|
||||
// $rating_count = $product->get_rating_count();
|
||||
// $rating_amount = floatval( $product->get_average_rating() );
|
||||
// $round_rating = (int)$rating_amount;
|
||||
// $rating_icon = '☆';
|
||||
|
||||
// echo '<div class="wpr-woo-rating">';
|
||||
|
||||
// for ( $i = 1; $i <= 5; $i++ ) {
|
||||
// if ( $i <= $rating_amount ) {
|
||||
// echo '<i class="wpr-rating-icon-full">'. $rating_icon .'</i>';
|
||||
// } elseif ( $i === $round_rating + 1 && $rating_amount !== $round_rating ) {
|
||||
// echo '<i class="wpr-rating-icon-'. ( $rating_amount - $round_rating ) * 10 .'">'. $rating_icon .'</i>';
|
||||
// } else {
|
||||
// echo '<i class="wpr-rating-icon-empty">'. $rating_icon .'</i>';
|
||||
// }
|
||||
// }
|
||||
|
||||
// echo '</div>';
|
||||
|
||||
// Another option
|
||||
$rating = $product->get_average_rating();
|
||||
$count = $product->get_rating_count();
|
||||
return wc_get_rating_html( $rating, $count );
|
||||
}
|
||||
|
||||
// Render Add To Cart
|
||||
public function render_product_add_to_cart( $settings, $product ) {
|
||||
|
||||
// If NOT a Product
|
||||
if ( is_null( $product ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
// Get Button Class
|
||||
$button_class = implode( ' ', array_filter( [
|
||||
'product_type_'. $product->get_type(),
|
||||
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
|
||||
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
|
||||
] ) );
|
||||
|
||||
$attributes = [
|
||||
'rel="nofollow"',
|
||||
'class="'. esc_attr($button_class) .' wpr-button-effect '. (!$product->is_in_stock() && 'simple' === $product->get_type() ? 'wpr-atc-not-clickable' : '').'"',
|
||||
'aria-label="'. esc_attr($product->add_to_cart_description()) .'"',
|
||||
'data-product_id="'. esc_attr($product->get_id()) .'"',
|
||||
'data-product_sku="'. esc_attr($product->get_sku()) .'"',
|
||||
];
|
||||
|
||||
$button_HTML = '';
|
||||
$page_id = get_queried_object_id();
|
||||
|
||||
// Button Text
|
||||
if ( 'simple' === $product->get_type() ) {
|
||||
$button_HTML .= $settings['element_addcart_simple_txt'];
|
||||
|
||||
if ( 'yes' === get_option('woocommerce_enable_ajax_add_to_cart') ) {
|
||||
array_push( $attributes, 'href="'. esc_url( get_permalink( $page_id ) .'/?add-to-cart='. get_the_ID() ) .'"' );
|
||||
} else {
|
||||
array_push( $attributes, 'href="'. esc_url( get_permalink() ) .'"' );
|
||||
}
|
||||
} elseif ( 'grouped' === $product->get_type() ) {
|
||||
$button_HTML .= $settings['element_addcart_grouped_txt'];
|
||||
array_push( $attributes, 'href="'. esc_url( $product->get_permalink() ) .'"' );
|
||||
} elseif ( 'variable' === $product->get_type() ) {
|
||||
$button_HTML .= $settings['element_addcart_variable_txt'];
|
||||
array_push( $attributes, 'href="'. esc_url( $product->get_permalink() ) .'"' );
|
||||
} else {
|
||||
array_push( $attributes, 'href="'. esc_url( $product->get_product_url() ) .'"' );
|
||||
$button_HTML .= get_post_meta( get_the_ID(), '_button_text', true ) ? get_post_meta( get_the_ID(), '_button_text', true ) : 'Buy Product';
|
||||
}
|
||||
|
||||
// Button HTML
|
||||
echo '<a '. implode( ' ', $attributes ) .'><span>'. $button_HTML .'</span></a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
return \ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Count_Wishlist_Compare_Items();
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Update_Mini_Compare setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Update_Mini_Compare {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// add_action('init', [$this, 'register_compare_cpt']);
|
||||
add_action( 'wp_ajax_update_mini_compare',[$this, 'update_mini_compare'] );
|
||||
add_action( 'wp_ajax_nopriv_update_mini_compare',[$this, 'update_mini_compare'] );
|
||||
}
|
||||
|
||||
// // Register Post Type
|
||||
// function register_compare_cpt() {
|
||||
// $args = array(
|
||||
// 'label' => esc_html__( 'Royal Compare', 'wpr-addons' ),
|
||||
// 'public' => true,
|
||||
// 'publicly_queryable' => true,
|
||||
// 'rewrite' => false,
|
||||
// 'show_ui' => true,
|
||||
// 'show_in_menu' => true,
|
||||
// 'show_in_nav_menus' => false,
|
||||
// 'exclude_from_search' => true,
|
||||
// 'capability_type' => 'post',
|
||||
// 'supports' => array( 'title', 'editor', 'elementor' ),
|
||||
// 'hierarchical' => false,
|
||||
// );
|
||||
|
||||
// register_post_type( 'compare', $args );
|
||||
// }
|
||||
|
||||
// Add two new functions for handling cookies
|
||||
public function get_compare_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_compare'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare']), true);
|
||||
} else if ( isset($_COOKIE['wpr_compare_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_compare_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function update_mini_compare() {
|
||||
if ( ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
|
||||
if ($user_id > 0) {
|
||||
$compare = get_user_meta($user_id, 'wpr_compare', true);
|
||||
if (!$compare) {
|
||||
$compare = array();
|
||||
}
|
||||
} else {
|
||||
$compare = $this->get_compare_from_cookie();
|
||||
}
|
||||
|
||||
$product = wc_get_product( $product_id );
|
||||
$product_data = [];
|
||||
if ( $product ) {
|
||||
$product_data['product_url'] = $product->get_permalink();
|
||||
$product_data['product_image'] = $product->get_image();
|
||||
$product_data['product_title'] = $product->get_title();
|
||||
$product_data['product_price'] = $product->get_price_html();
|
||||
$product_data['product_id'] = $product->get_id();
|
||||
$product_data['compare_count'] = sizeof($compare);
|
||||
}
|
||||
|
||||
wp_send_json($product_data);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Update_Mini_Compare();
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Update_Mini_Wishlist setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Update_Mini_Wishlist {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// add_action('init', [$this, 'register_wishlist_cpt']);
|
||||
add_action( 'wp_ajax_update_mini_wishlist',[$this, 'update_mini_wishlist'] );
|
||||
add_action( 'wp_ajax_nopriv_update_mini_wishlist',[$this, 'update_mini_wishlist'] );
|
||||
}
|
||||
|
||||
// Add two new functions for handling cookies
|
||||
public function get_wishlist_from_cookie() {
|
||||
if (isset($_COOKIE['wpr_wishlist'])) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist']), true);
|
||||
} else if ( isset($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']) ) {
|
||||
return json_decode(stripslashes($_COOKIE['wpr_wishlist_'. get_current_blog_id() .'']), true);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
function update_mini_wishlist() {
|
||||
if ( ! isset( $_POST['product_id'] ) ) {
|
||||
return;
|
||||
}
|
||||
$product_id = intval( $_POST['product_id'] );
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
|
||||
if ($user_id > 0) {
|
||||
$wishlist = get_user_meta($user_id, 'wpr_wishlist', true);
|
||||
if (!$wishlist) {
|
||||
$wishlist = array();
|
||||
}
|
||||
} else {
|
||||
$wishlist = $this->get_wishlist_from_cookie();
|
||||
}
|
||||
|
||||
$product = wc_get_product( $product_id );
|
||||
$product_data = [];
|
||||
if ( $product ) {
|
||||
$product_data['product_url'] = $product->get_permalink();
|
||||
$product_data['product_image'] = $product->get_image();
|
||||
$product_data['product_title'] = $product->get_title();
|
||||
$product_data['product_price'] = $product->get_price_html();
|
||||
$product_data['product_id'] = $product->get_id();
|
||||
$product_data['wishlist_count'] = sizeof($wishlist);
|
||||
}
|
||||
|
||||
wp_send_json($product_data);
|
||||
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Update_Mini_Wishlist();
|
||||
Reference in New Issue
Block a user