Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.07 KB | None | 0 0
  1. <?php
  2. /**
  3. * Single Product Accessories
  4. *
  5. * This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
  6. *
  7. * @version 1.0
  8. */
  9.  
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13.  
  14. global $product;
  15. $current_product_id = electro_wc_get_product_id( $product );
  16.  
  17. $columns = apply_filters( 'electro_accessories_loop_columns', 3 );
  18. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
  19. global $woocommerce_loop;
  20. $woocommerce_loop['columns'] = $columns;
  21. } else {
  22. $temp_cols = wc_get_loop_prop( 'columns' );
  23. wc_set_loop_prop( 'columns', $columns );
  24. }
  25.  
  26. $accessories = Electro_WC_Helper::get_accessories( $product );
  27. array_unshift( $accessories, $current_product_id );
  28.  
  29. if ( sizeof( $accessories ) === 0 && !array_filter( $accessories ) ) {
  30. return;
  31. }
  32.  
  33. $meta_query = WC()->query->get_meta_query();
  34.  
  35. $args = apply_filters( 'electro_accessories_query_args', array(
  36. 'post_type' => 'product',
  37. 'ignore_sticky_posts' => 1,
  38. 'no_found_rows' => 1,
  39. 'posts_per_page' => 3,
  40. 'orderby' => 'post__in',
  41. 'post__in' => $accessories,
  42. 'meta_query' => $meta_query
  43. ) );
  44.  
  45. unset( $args['meta_query'] );
  46.  
  47. $products = new WP_Query( $args );
  48.  
  49. $add_to_cart_checkbox = '';
  50. $total_price = 0;
  51. $total_price_suffix = 0;
  52. $count = 0;
  53.  
  54. if ( $products->have_posts() ) : ?>
  55.  
  56. <div class="accessories">
  57.  
  58. <div class="electro-wc-message"></div>
  59. <div class="row">
  60. <div class="col-xs-12 col-sm-9 col-left">
  61.  
  62. <?php woocommerce_product_loop_start(); ?>
  63.  
  64. <?php while ( $products->have_posts() ) : $products->the_post(); ?>
  65.  
  66. <?php wc_get_template_part( 'content', 'product' ); ?>
  67.  
  68. <?php
  69. global $product;
  70. $product_id = electro_wc_get_product_id( $product );
  71.  
  72. $price_html = '';
  73. $display_price = 0;
  74. $price_suffix = 0;
  75.  
  76. if ( $price_html = $product->get_price_html() ) {
  77. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.7', '<' ) ) {
  78. $display_price = $product->get_display_price();
  79. } else {
  80. $display_price = wc_get_price_to_display( $product );
  81. if ( ( $suffix = get_option( 'woocommerce_price_display_suffix' ) ) && wc_tax_enabled() && 'taxable' === $product->get_tax_status() ) {
  82. if ( strpos( $suffix, '{price_excluding_tax}' ) !== false ) {
  83. $price_suffix = wc_get_price_excluding_tax( $product );
  84. } elseif ( strpos( $suffix, '{price_including_tax}' ) !== false ) {
  85. $price_suffix = wc_get_price_including_tax( $product );
  86. }
  87. }
  88. }
  89. if( $display_price > 0 ) {
  90. $price_html = '- <span class="accessory-price">' . wc_price( $display_price ) . $product->get_price_suffix() . '</span>';
  91. }
  92. }
  93.  
  94. if( $current_product_id == $product_id ) {
  95. $total_price += empty( $display_price ) ? 0 : $display_price;
  96. $total_price_suffix += empty( $price_suffix ) ? 0 : $price_suffix;
  97. $count++;
  98. }
  99.  
  100. $prefix = ( $current_product_id == $product_id ) ? '<strong>' . esc_html__( 'This product: ', 'electro' ) . '</strong>' : '';
  101. $checked = ( $current_product_id == $product_id ) ? 'checked disabled' : '';
  102.  
  103. $add_to_cart_checkbox .= '<div class="checkbox accessory-checkbox"><label><input ' . $checked . ' type="checkbox" class="product-check" data-price="' . $display_price . '" data-price-suffix="' . $price_suffix . '" data-product-id="' . electro_wc_get_product_id( $product ) . '" data-product-type="' . electro_wc_get_product_type( $product ) . '" /> <span class="product-title">' . $prefix . get_the_title() . '</span> ' . $price_html . '</label></div>';
  104. ?>
  105.  
  106. <?php endwhile; // end of the loop. ?>
  107.  
  108. <?php woocommerce_product_loop_end(); ?>
  109.  
  110. <div class="check-products">
  111. <?php echo $add_to_cart_checkbox; ?>
  112. </div>
  113.  
  114. </div>
  115.  
  116. <div class="col-xs-12 col-sm-3 col-right">
  117. <?php if( $total_price > 0 ) : ?>
  118. <div class="total-price">
  119. <?php
  120. $total_price_suffix_html = '';
  121. if ( $total_price_suffix ) {
  122. $suffix = get_option( 'woocommerce_price_display_suffix' );
  123. if ( strpos( $suffix, '{price_excluding_tax}' ) !== false ) {
  124. $total_price_suffix_html = str_replace( '{price_excluding_tax}', wc_price( $total_price_suffix ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' );
  125. } elseif ( strpos( $suffix, '{price_including_tax}' ) !== false ) {
  126. $total_price_suffix_html = str_replace( '{price_including_tax}', wc_price( $total_price_suffix ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' );
  127. }
  128. }
  129. $total_price_html = '<span class="total-price-html">' . wc_price( $total_price ) . wp_kses_post( $total_price_suffix_html ) . '</span>';
  130. $total_products_html = '<span class="total-products">' . $count . '</span>';
  131. $total_price = sprintf( __( '%s for %s item(s)', 'electro' ), $total_price_html, $total_products_html );
  132. echo wp_kses_post( $total_price );
  133. ?>
  134. </div>
  135. <?php if( electro_get_shop_catalog_mode() == false ) : ?>
  136. <div class="accessories-add-all-to-cart">
  137. <button type="button" class="button btn btn-primary add-all-to-cart"><?php echo esc_html__( 'Add all to cart', 'electro' ); ?></button>
  138. </div>
  139. <?php endif; ?>
  140. <?php endif; ?>
  141. </div>
  142. </div>
  143. <?php
  144. $alert_message = apply_filters( 'electro_accessories_product_cart_alert_message', array(
  145. 'success' => sprintf( '<div class="woocommerce-message">%s <a class="button wc-forward" href="%s">%s</a></div>', esc_html__( 'Products was successfully added to your cart.', 'electro' ), wc_get_cart_url(), esc_html__( 'View Cart', 'electro' ) ),
  146. 'empty' => sprintf( '<div class="woocommerce-error">%s</div>', esc_html__( 'No Products selected.', 'electro' ) ),
  147. 'no_variation' => sprintf( '<div class="woocommerce-error">%s</div>', esc_html__( 'Product Variation does not selected.', 'electro' ) ),
  148. 'not_available' => sprintf( '<div class="woocommerce-error">%s</div>', esc_html__( 'Sorry, this product is unavailable.', 'electro' ) ),
  149. ) );
  150.  
  151. ob_start(); ?>
  152. jQuery(document).ready(function($) {
  153.  
  154. /**
  155. * Check if a node is blocked for processing.
  156. *
  157. * @param {JQuery Object} $node
  158. * @return {bool} True if the DOM Element is UI Blocked, false if not.
  159. */
  160. var is_blocked = function( $node ) {
  161. return $node.is( '.processing' ) || $node.parents( '.processing' ).length;
  162. };
  163.  
  164. /**
  165. * Block a node visually for processing.
  166. *
  167. * @param {JQuery Object} $node
  168. */
  169. var block = function( $node ) {
  170. if ( ! is_blocked( $node ) ) {
  171. $node.addClass( 'processing' ).block( {
  172. message: null,
  173. overlayCSS: {
  174. background: '#fff url( "<?php echo get_template_directory_uri() . '/assets/images/ajax-loader.gif'; ?>" ) no-repeat center',
  175. opacity: 0.6
  176. }
  177. } );
  178. }
  179. };
  180.  
  181. /**
  182. * Unblock a node after processing is complete.
  183. *
  184. * @param {JQuery Object} $node
  185. */
  186. var unblock = function( $node ) {
  187. $node.removeClass( 'processing' ).unblock();
  188. };
  189.  
  190. function accessory_checked_count(){
  191. var product_count = 0;
  192. $('.accessory-checkbox .product-check').each(function() {
  193. if( $(this).is(':checked') ) {
  194. if( $(this).data( 'price' ) !== '' ) {
  195. product_count++;
  196. }
  197. }
  198. });
  199. return product_count;
  200. }
  201.  
  202. function accessory_checked_total_price(){
  203. var total_price = 0;
  204. $('.accessory-checkbox .product-check').each(function() {
  205. if( $(this).is(':checked') ) {
  206. if( $(this).data( 'price' ) !== '' ) {
  207. total_price += parseFloat( $(this).data( 'price' ) );
  208. }
  209. }
  210. });
  211. return total_price;
  212. }
  213.  
  214. function accessory_checked_total_price_suffix(){
  215. var total_price_suffix = 0;
  216. $('.accessory-checkbox .product-check').each(function() {
  217. if( $(this).is(':checked') ) {
  218. if( $(this).data( 'price-suffix' ) !== '' ) {
  219. total_price_suffix += parseFloat( $(this).data( 'price-suffix' ) );
  220. }
  221. }
  222. });
  223. return total_price_suffix;
  224. }
  225.  
  226. function accessory_checked_product_ids(){
  227. var product_ids = [];
  228. $('.accessory-checkbox .product-check').each(function() {
  229. if( $(this).is(':checked') ) {
  230. product_ids.push( $(this).data( 'product-id' ) );
  231. }
  232. });
  233. return product_ids;
  234. }
  235.  
  236. function accessory_unchecked_product_ids(){
  237. var product_ids = [];
  238. $('.accessory-checkbox .product-check').each(function() {
  239. if( ! $(this).is(':checked') ) {
  240. product_ids.push( $(this).data( 'product-id' ) );
  241. }
  242. });
  243. return product_ids;
  244. }
  245.  
  246. function accessory_checked_variable_product_ids(){
  247. var variable_product_ids = [];
  248. $('.accessory-checkbox .product-check').each(function() {
  249. if( $(this).is(':checked') && $(this).data( 'product-type' ) == 'variable' ) {
  250. variable_product_ids.push( $(this).data( 'product-id' ) );
  251. }
  252. });
  253. return variable_product_ids;
  254. }
  255.  
  256. function accessory_is_variation_selected(){
  257. if( $(".single_add_to_cart_button").is(":disabled") ) {
  258. return false;
  259. }
  260. return true;
  261. }
  262.  
  263. function accessory_is_variation_available(){
  264. if( $(".single_add_to_cart_button").length === 0 || $(".single_add_to_cart_button").hasClass("disabled") || $(".single_add_to_cart_button").hasClass("wc-variation-is-unavailable") ) {
  265. return false;
  266. }
  267. return true;
  268. }
  269.  
  270. function accessory_is_product_available(){
  271. if( $(".single_add_to_cart_button").length === 0 || $(".single_add_to_cart_button").hasClass("disabled") ) {
  272. return false;
  273. }
  274. return true;
  275. }
  276.  
  277. function accessory_refresh_fragments( response ){
  278. var this_page = window.location.toString();
  279. var fragments = response.fragments;
  280. var cart_hash = response.cart_hash;
  281.  
  282. // Block fragments class
  283. if ( fragments ) {
  284. $.each( fragments, function( key ) {
  285. $( key ).addClass( 'updating' );
  286. });
  287. }
  288.  
  289. // Replace fragments
  290. if ( fragments ) {
  291. $.each( fragments, function( key, value ) {
  292. $( key ).replaceWith( value );
  293. });
  294. }
  295.  
  296. // Cart page elements
  297. $( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
  298.  
  299. $( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
  300.  
  301. $( document.body ).trigger( 'cart_page_refreshed' );
  302. });
  303.  
  304. $( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
  305. $( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
  306. });
  307. }
  308.  
  309. $( 'body' ).on( 'found_variation', function( event, variation ) {
  310. $('.accessory-checkbox .product-check').each(function() {
  311. if( $(this).is(':checked') && $(this).data( 'product-type' ) == 'variable' ) {
  312. $(this).data( 'price', variation.display_price );
  313. $(this).siblings( 'span.accessory-price' ).html( $(variation.price_html).html() );
  314. }
  315. });
  316. });
  317.  
  318. $( 'body' ).on( 'woocommerce_variation_has_changed', function( event ) {
  319. block( $( 'div.accessories' ) );
  320. var total_price = accessory_checked_total_price();
  321. var total_price_suffix = accessory_checked_total_price_suffix();
  322. $.post( "<?php echo admin_url( 'admin-ajax.php' ); ?>", { 'action': "electro_accessories_total_price", 'price': total_price, 'price_suffix': total_price_suffix } )
  323. .done( function( response ) {
  324. $( 'span.total-price-html' ).html( response );
  325. $( 'span.total-products' ).html( accessory_checked_count() );
  326. unblock( $( 'div.accessories' ) );
  327. })
  328. .fail(function() {
  329. unblock( $( 'div.accessories' ) );
  330. })
  331. .always(function() {
  332. unblock( $( 'div.accessories' ) );
  333. });
  334. });
  335.  
  336. $( '.accessory-checkbox .product-check' ).on( "click", function() {
  337. block( $( 'div.accessories' ) );
  338. var total_price = accessory_checked_total_price();
  339. var total_price_suffix = accessory_checked_total_price_suffix();
  340. $.ajax({
  341. type: "POST",
  342. async: false,
  343. url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
  344. data: { 'action': "electro_accessories_total_price", 'price': total_price, 'price_suffix': total_price_suffix },
  345. success : function( response ) {
  346. $( 'span.total-price-html' ).html( response );
  347. $( 'span.total-products' ).html( accessory_checked_count() );
  348.  
  349. var unchecked_product_ids = accessory_unchecked_product_ids();
  350. $( '.accessories ul.products > li' ).each(function() {
  351. $(this).show();
  352. for (var i = 0; i < unchecked_product_ids.length; i++ ) {
  353. if( $(this).hasClass( 'post-'+unchecked_product_ids[i] ) ) {
  354. $(this).hide();
  355. }
  356. }
  357. });
  358. },
  359. complete: function() {
  360. unblock( $( 'div.accessories' ) );
  361. }
  362. })
  363. });
  364.  
  365. $('.accessories-add-all-to-cart .add-all-to-cart').click(function() {
  366. var accerories_all_product_ids = accessory_checked_product_ids();
  367. var accerories_variable_product_ids = accessory_checked_variable_product_ids();
  368. var accerories_alert_msg = '';
  369. if( accerories_all_product_ids.length === 0 ) {
  370. accerories_alert_msg = '<?php echo wp_kses_post( $alert_message['empty'] ); ?>';
  371. } else if( accerories_variable_product_ids.length > 0 && accessory_is_variation_selected() === false ) {
  372. accerories_alert_msg = '<?php echo wp_kses_post( $alert_message['no_variation'] ); ?>';
  373. } else if( accerories_variable_product_ids.length > 0 && accessory_is_variation_available() === false ) {
  374. accerories_alert_msg = '<?php echo wp_kses_post( $alert_message['not_available'] ); ?>';
  375. } else if( accerories_variable_product_ids.length === 0 && accessory_is_product_available() === false ) {
  376. accerories_alert_msg = '<?php echo wp_kses_post( $alert_message['not_available'] ); ?>';
  377. } else {
  378. var add_to_cart_error = false;
  379. for (var i = 0; i < accerories_all_product_ids.length; i++ ) {
  380. if( ! $.inArray( accerories_all_product_ids[i], accerories_variable_product_ids ) ) {
  381. var variation_id = $('.variations_form .variations_button').find('input[name^=variation_id]').val();
  382. var variation = {};
  383. if( $( '.variations_form' ).find('select[name^=attribute]').length ) {
  384. $( '.variations_form' ).find('select[name^=attribute]').each(function() {
  385. var attribute = $(this).attr("name");
  386. var attributevalue = $(this).val();
  387. variation[attribute] = attributevalue;
  388. });
  389.  
  390. } else {
  391.  
  392. $( '.variations_form' ).find('.select').each(function() {
  393. var attribute = $(this).attr("data-attribute-name");
  394. var attributevalue = $(this).find('.selected').attr('data-name');
  395. variation[attribute] = attributevalue;
  396. });
  397.  
  398. }
  399. $.ajax({
  400. type: "POST",
  401. async: false,
  402. url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
  403. data: { 'action': "electro_variable_add_to_cart", 'product_id': accerories_all_product_ids[i], 'variation_id': variation_id, 'variation': variation },
  404. success : function( response ) {
  405. if( response.error ) {
  406. add_to_cart_error = true;
  407. }
  408. accessory_refresh_fragments( response );
  409. }
  410. })
  411. } else {
  412. $.ajax({
  413. type: "POST",
  414. async: false,
  415. url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
  416. data: { 'action': "electro_variable_add_to_cart", 'product_id': accerories_all_product_ids[i] },
  417. success : function( response ) {
  418. if( response.error ) {
  419. add_to_cart_error = true;
  420. }
  421. accessory_refresh_fragments( response );
  422. }
  423. })
  424. }
  425. }
  426.  
  427. if( add_to_cart_error ) {
  428. location.reload();
  429. } else {
  430. accerories_alert_msg = '<?php echo wp_kses_post( $alert_message['success'] ); ?>';
  431. }
  432. }
  433.  
  434. if( accerories_alert_msg ) {
  435. $( '.electro-wc-message' ).html(accerories_alert_msg);
  436. }
  437. });
  438. });
  439. <?php
  440. $custom_script = ob_get_clean();
  441. wp_add_inline_script( 'electro-js', $custom_script );
  442. ?>
  443.  
  444. </div>
  445.  
  446. <?php endif;
  447.  
  448. wp_reset_postdata();
  449. wc_set_loop_prop( 'columns', $temp_cols );
  450. wc_reset_loop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement