Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization of Quick View #378

Merged
merged 25 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9efa9b1
feat(quick-view): initial module structure
satyajittalukder Jan 17, 2024
7fb07b2
Merge branch 'fix/project/issue#373' into feat/quick-view/issue#372
satyajittalukder Jan 17, 2024
636c7c3
feat(quick-view): modal integration
satyajittalukder Jan 17, 2024
d0d4a77
test(quick-view): experimental code is added
satyajittalukder Jan 19, 2024
9484eca
feat(quick-view): the data loading in the qucik view tempalte is bein…
satyajittalukder Jan 22, 2024
ae8bcd7
feat(quick-view): the product template is being added.
satyajittalukder Jan 23, 2024
04e90cb
feat(quick-view): multiple settings fields being added.
arwebcrafts Jan 27, 2024
1e6810e
feat(quick-view): initial setting addition
satyajittalukder Feb 5, 2024
9977581
feat(quick-view): multiple settings field integrated
satyajittalukder Feb 5, 2024
91967f7
feat(quick-view): content controller, general settings controller
satyajittalukder Feb 6, 2024
e0381ff
feat(quick-view): icon and pro feature added.
satyajittalukder Feb 7, 2024
2287dcd
feat(quick_view): the icon and the pro feature is being added
satyajittalukder Feb 15, 2024
95fd202
feat(quick-view): product image is being added
satyajittalukder Feb 15, 2024
52b64d2
feat(quick-view): the the icon and fallback image updated
satyajittalukder Feb 22, 2024
c85dd62
fix(quick view): phpcs ssue fix
satyajittalukder Feb 23, 2024
37b6734
fix(quick-view): ajax referer removed from enques script
satyajittalukder Feb 23, 2024
4916ae9
feat(Quick-view): module compatibility
satyajittalukder Feb 29, 2024
68c82cd
Merge branch 'develop' into feat/quick-view/issue#372
satyajittalukder Mar 13, 2024
16106e7
fix(Quick-view): method runnig issue
satyajittalukder Mar 14, 2024
b3b4930
Merge branch 'develop' into feat/quick-view/issue#372
satyajittalukder Mar 14, 2024
3a71aba
fix(quick-view): pro and error handle
satyajittalukder Mar 14, 2024
fa7843b
feat(quick-view): cart redirection fucntionality added
satyajittalukder Mar 18, 2024
cda10d8
fix: module doc update
satyajittalukder Mar 18, 2024
396def4
fix(quick-view): preview redesign
satyajittalukder Mar 18, 2024
9153e31
update version and description
code-with-mehedi Mar 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions Includes/Modules/QuickView/Includes/Ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
/**
* Ajax class for `Stock Bar` module.
*
* @package SBFW
*/

namespace STOREGROWTH\SPSB\Modules\QuickView\Includes;

use STOREGROWTH\SPSB\Traits\Singleton;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Add ajax actions inside this class.
*/
class Ajax {

use Singleton;

/**
* Constructor of Ajax class.
*/
private function __construct() {
add_action( 'wp_ajax_sgsb_quick_view_save_settings', array( $this, 'save_settings' ) );
add_action( 'wp_ajax_sgsb_quick_view_get_settings', array( $this, 'get_settings' ) );
add_action( 'wp_ajax_sgsbqcv_quickview', array( $this, 'ajax_quickview_callback' ) );
add_action( 'wp_ajax_nopriv_sgsbqcv_quickview', array( $this, 'ajax_quickview_callback' ) );
add_action( 'wp_ajax_custom_ajax_add_to_cart', array( $this, 'custom_ajax_add_to_cart' ) );
add_action( 'wp_ajax_nopriv_custom_ajax_add_to_cart', array( $this, 'custom_ajax_add_to_cart' ) );
// add_action( 'wp_ajax_load_modal_template', array( $this, 'load_modal_template_callback' ) );
// add_action( 'wp_ajax_nopriv_load_modal_template', array( $this, 'load_modal_template_callback' ) );
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Ajax action for save settings
*/
public function save_settings() {
check_ajax_referer( 'sgsb_ajax_nonce' );

if ( ! isset( $_POST['form_data'] ) ) {
wp_send_json_error();
}

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitizing via `sgsb_sanitize_form_fields`.
$form_data = array_map( 'sgsb_sanitize_form_fields', wp_unslash( $_POST['form_data'] ) );

update_option( 'sgsb_quick_view_settings', $form_data );

wp_send_json_success();
}

/**
* Ajax action for get settings.
*/
public function get_settings() {
check_ajax_referer( 'sgsb_ajax_nonce' );

$form_data = get_option( 'sgsb_quick_view_settings', array() );

wp_send_json_success( $form_data );
}

public function ajax_quickview_callback() {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
check_ajax_referer( 'sgsbqcv-security', 'nonce' );

global $post, $product;
$settings = get_option( 'sgsb_quick_view_settings' );

$product_id = absint( sanitize_key( $_REQUEST['product_id'] ) );
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$product = wc_get_product( $product_id );
$content_image = 'all';
$content_view_details_button = sgsb_find_option_setting( $settings, 'show_view_details_button', false );
$content_image_lightbox = 'no';

if ( $product ) {
$post = get_post( $product_id );
setup_postdata( $post );
$thumb_ids = array();

if ( $content_image === 'product_image' ) {
if ( $product_image = $product->get_image_id() ) {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$thumb_ids[] = $product_image;
}

if ( $product->is_type( 'variable' ) && ( $children = $product->get_visible_children() ) ) {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $children as $child ) {
if ( ( $child_product = wc_get_product( $child ) ) && ( $child_product_image = $child_product->get_image_id() ) ) {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$thumb_ids[] = $child_product_image;
}
}
}
} else {
if ( $content_image === 'all' ) {
if ( $product_image = $product->get_image_id() ) {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$thumb_ids[] = $product_image;
}

if ( $product->is_type( 'variable' ) && ( $children = $product->get_visible_children() ) ) {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $children as $child ) {
if ( ( $child_product = wc_get_product( $child ) ) && ( $child_product_image = $child_product->get_image_id() ) ) {
$thumb_ids[] = $child_product_image;
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

if ( is_a( $product, 'WC_Product_Variation' ) ) {
// get images from WPC Additional Variation Images
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$_images = array_filter( explode( ',', get_post_meta( $product_id, 'wpcvi_images', true ) ) );

if ( ! empty( $_images ) ) {
$thumb_ids = array_merge( $thumb_ids, $_images );
}
} else {
$thumb_ids = array_merge( $thumb_ids, $product->get_gallery_image_ids() );
}
}

$thumb_ids = apply_filters( 'sgsbqcv_thumbnails', $thumb_ids, $product );
$thumb_ids = array_unique( $thumb_ids );

echo '<div id="sgsbqcv-popup" class="sgsbqcv-popup mfp-with-anim ' . esc_attr( $content_view_details_button ? 'view-details' : '' ) . '">';
?>
<div class="woocommerce single-product sgsbqcv-product">
<div id="product-<?php echo esc_attr( $product_id ); ?>" <?php wc_product_class( '', $product ); ?>>
<div class="thumbnails">
<?php
do_action( 'sgsbqcv_before_thumbnails', $product );

echo '<div class="images">';

$image_sz = apply_filters( 'sgsbqcv_image_size', 'default' );

if ( $image_sz === 'default' ) {
$image_size = 'sgsbqcv';
} else {
$image_size = $image_sz;
}

if ( ! empty( $thumb_ids ) ) {
foreach ( $thumb_ids as $thumb_id ) {
if ( $content_image_lightbox !== 'no' ) {
$image_full = wp_get_attachment_image_src( $thumb_id, 'full' );

echo '<div class="thumbnail" data-id="' . $thumb_id . '">' . wp_get_attachment_image(
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$thumb_id,
$image_size,
false,
array(
'data-fancybox' => 'gallery',
'data-src' => esc_url( $image_full[0] ),
)
) . '</div>';
} else {
echo '<div class="thumbnail" data-id="' . $thumb_id . '">' . wp_get_attachment_image( $thumb_id, $image_size ) . '</div>';
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
echo '<div class="thumbnail">' . wc_placeholder_img( $image_size ) . '</div>';
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
}

echo '</div>';

do_action( 'sgsbqcv_after_thumbnails', $product );
?>
</div>
<div class="summary entry-summary">
<?php do_action( 'sgsbqcv_before_summary', $product ); ?>

<div class="summary-content">
<?php do_action( 'sgsbqcv_product_summary', $product ); ?>
</div>

<?php do_action( 'sgsbqcv_after_summary', $product ); ?>
</div>
</div>
</div><!-- /woocommerce single-product -->
<?php
$permalink = $product->get_permalink();
do_action( 'sgsb_quick_view_details_button', $permalink, $content_view_details_button );
echo '</div><!-- #sgsbqcv-popup -->';
wp_reset_postdata();
}

wp_die();
}

public function custom_ajax_add_to_cart() {
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
$product_id = $_POST['product_id'];
$quantity = $_POST['quantity'];
satyajittalukder marked this conversation as resolved.
Show resolved Hide resolved
WC()->cart->add_to_cart( $product_id, $quantity );
wp_die();
}
}
131 changes: 131 additions & 0 deletions Includes/Modules/QuickView/Includes/CommonHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Common_Hooks class for `Stock Bar` module.
*
* @package SBFW
*/

namespace STOREGROWTH\SPSB\Modules\QuickView\Includes;

use STOREGROWTH\SPSB\Traits\Singleton;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Miscellaneous hooks implementation.
*/
class CommonHooks {

use Singleton;

/**
* Constructor of Common_Hooks class.
*/
private function __construct() {
$this->button_positon_hooks();
$this->content_loader_hooks();
}

/**
* Hook for Content Loader.
*
* @since 1.0.0
*/
public function content_loader_hooks() {
$settings = get_option( 'sgsb_quick_view_settings' );

$actions = array(
'show_title' => array(
'action' => 'woocommerce_template_single_title',
'priority' => 5,
),
'show_rating' => array(
'action' => 'woocommerce_template_single_rating',
'priority' => 10,
),
'show_excerpt' => array(
'action' => 'woocommerce_template_single_excerpt',
'priority' => 15,
),
'show_price' => array(
'action' => 'woocommerce_template_single_price',
'priority' => 20,
),
'show_add_to_cart' => array(
'action' => array( $this, 'add_to_cart' ),
'priority' => 25,
),
'show_meta' => array(
'action' => 'woocommerce_template_single_meta',
'priority' => 30,
),
'show_description' => array(
'action' => array( $this, 'show_single_product_description' ),
'priority' => 35,
),
);

foreach ( $actions as $setting => $data ) {
if ( sgsb_find_option_setting( $settings, $setting, true ) ) {
add_action( 'sgsbqcv_product_summary', $data['action'], $data['priority'] );
}
}
}

/**
* Hook for WooCommerce loop add to cart link.
*
* @since 1.0.0
*/
public function show_single_product_description() {
global $product;
$description = $product->get_description();
include __DIR__ . '/../templates/description-template.php';
}
/**
* Hook for WooCommerce loop add to cart link.
*
* @since 1.0.0
*/
public function show_quick_view_button_shop() {
echo esc_html( $this->display_buy_now_button() );
}
/**
* Hook for button postion.
*
* @since 1.1.3
*/
public function button_positon_hooks() {
$settings = get_option( 'sgsb_quick_view_settings' );
$button_position = sgsb_find_option_setting( $settings, 'button_position', 'after_add_to_cart' );
$hook = 'woocommerce_after_shop_loop_item';
$priority = ( 'after_add_to_cart' === $button_position ) ? 15 : 10;

add_action( $hook, array( $this, 'show_quick_view_button_shop' ), $priority );
}

/**
* Function to display the Buy Now button.
*/
private function display_buy_now_button() {
global $product;

$product_id = get_the_ID();
$direct_checkout_button_layout = get_post_meta( $product_id, '_sgsb_direct_checkout_button_layout', true );
$settings = get_option( 'sgsb_quick_view_settings' );

include __DIR__ . '/../templates/quick-view-button.php';
}

/**
* Hook for WooCommerce loop add to cart link.
*
* @since 1.1.3
*/
public function add_to_cart() {
woocommerce_template_single_add_to_cart();
}
}
Loading