Advertisement
Guest User

Untitled

a guest
May 15th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.00 KB | None | 0 0
  1. <?php
  2. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  3.  
  4.  
  5.  
  6.  
  7.  
  8. //add the price and rating out of the affiliate link
  9. remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price');
  10.  
  11. remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating');
  12.  
  13. add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_rating',6);
  14. add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_price',7);
  15.  
  16.  
  17.  
  18. //remove the product title from the original place and add it before the image
  19. remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title');
  20.  
  21. add_action('woocommerce_before_shop_loop_item',function(){
  22.  
  23. if(generatepress_wc_get_setting( 'product_archive_title' ) != false){
  24.  
  25. echo '<a href="' . esc_url( get_the_permalink() ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
  26.  
  27. woocommerce_template_loop_product_title();
  28.  
  29. echo '</a>';
  30. }
  31.  
  32. },2);
  33.  
  34.  
  35.  
  36. function tu_custom_template_loop_product_link_open() {
  37. global $product;
  38.  
  39. $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
  40.  
  41. echo '<a target="_blank" href="' . esc_url( $link ) . '" class="blank_affiliate_link woocommerce-LoopProduct-link woocommerce-loop-product__link">';
  42. }
  43.  
  44. //attach affiliate link to the product image
  45. add_action('woocommerce_before_shop_loop_item',function(){
  46.  
  47. global $product;
  48.  
  49. if($product->get_type() == 'external'){
  50.  
  51. remove_action('woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open');
  52.  
  53. add_action('woocommerce_before_shop_loop_item','tu_custom_template_loop_product_link_open',5);
  54.  
  55. add_filter('woocommerce_loop_product_link',function($product_link,$product){
  56.  
  57. return $product->get_product_url();
  58.  
  59. },3,2);
  60.  
  61. }
  62.  
  63.  
  64. },2);
  65.  
  66.  
  67. //making the link opens in a new tab
  68. add_filter('woocommerce_loop_add_to_cart_link',function($add_to_cart_link,$product,$args){
  69.  
  70.  
  71. if($product->get_type() == 'external'){
  72. $args['attributes']['target'] ='_blank';
  73. }
  74. return sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
  75. esc_url( $product->add_to_cart_url() ),
  76. esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
  77. esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
  78. isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
  79. esc_html( $product->add_to_cart_text() )
  80. );
  81.  
  82. },1,3);
  83.  
  84.  
  85. // limit the characters of the product description
  86. function the_excerpt_max_charlength($charlength,$product_excerpt) {
  87.  
  88. $excerpt = $product_excerpt;
  89. $charlength++;
  90.  
  91. if ( mb_strlen( $excerpt ) > $charlength ) {
  92. $subex = mb_substr( $excerpt, 0, $charlength - 5 );
  93. $exwords = explode( ' ', $subex );
  94. $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
  95. if ( $excut < 0 ) {
  96. return mb_substr( $subex, 0, $excut );
  97. } else {
  98. return $subex;
  99. }
  100. } else {
  101. return $excerpt;
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. //customizing the product short description for styling
  111. add_filter('woocommerce_short_description',function($product_exerpt){
  112.  
  113.  
  114. if( !empty($product_exerpt) ):
  115. $product_exerpt = the_excerpt_max_charlength(400,$product_exerpt);
  116.  
  117. return '<div class="product-short-description-container">'. $product_exerpt . '</div>';
  118. else:
  119. return $product_exerpt;
  120. endif;
  121.  
  122. });
  123.  
  124. //remove the rating from the product loop
  125. remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating',5);
  126.  
  127.  
  128.  
  129. //add the css and js files
  130.  
  131. add_action('wp_enqueue_scripts',function(){
  132.  
  133.  
  134. wp_enqueue_style('frontend-styles', get_template_directory_uri() . '/css/frontend-styles.css');
  135.  
  136. wp_enqueue_script( 'frontend-actions', get_template_directory_uri() . '/js/actions.js', array('jquery'), '1.0.0', true );
  137.  
  138.  
  139. });
  140.  
  141.  
  142.  
  143.  
  144. //adding custom wrapping div for styling
  145.  
  146. add_action('woocommerce_before_shop_loop_item_title',function(){
  147.  
  148. echo '<div class="product-loop-rest-container">';
  149. },5);
  150.  
  151.  
  152.  
  153.  
  154. add_action('woocommerce_after_shop_loop_item',function(){
  155.  
  156. echo '</div>';
  157.  
  158. },100);
  159.  
  160.  
  161. remove_action('woocommerce_before_shop_loop','woocommerce_before_shop_loop');
  162.  
  163.  
  164.  
  165. add_action('woocommerce_after_shop_loop',function(){
  166. //print_r(did_action('woocommerce_before_shop_loop'));
  167.  
  168. });
  169.  
  170. add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ), 20 );
  171.  
  172.  
  173.  
  174. /*
  175. [woof products_per_page=_1 columns=4 is_ajax=0 taxonomies=product_cat:9 custom_tpl='themes/generatepress/woo_tpl_1.php']
  176.  
  177. */
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. add_action('wp_enqueue_scripts',function(){
  186.  
  187. wp_localize_script( 'jquery','special_ajax_data' , array('ajax_url' => admin_url('admin-ajax.php'),'nonce'=>wp_create_nonce('wp_ajax_nonce')));
  188.  
  189. },100);
  190.  
  191. //load more products through ajax
  192. add_action('wp_ajax_nopriv_load_more_products','load_more_products_func');
  193. add_action('wp_ajax_load_more_products','load_more_products_func');
  194.  
  195. function load_more_products_func(){
  196.  
  197. $paged = $_POST['paged']+1;
  198.  
  199. $existProductIds = $_POST['productIds'];
  200. $urlParameters = $_POST['searchParameters'];
  201.  
  202.  
  203. $args = array(
  204. 'post_type' => 'product',
  205. 'paged' => $paged,
  206. 'posts_per_page' => generatepress_wc_get_setting( 'products_per_page' )
  207. );
  208.  
  209.  
  210. $meta_query = array();
  211. $tax_query = array();
  212.  
  213.  
  214.  
  215.  
  216.  
  217. if(!empty($urlParameters['min_price']) && !empty($urlParameters['max_price'])){
  218. $meta_query['relation'] = 'AND' ;
  219.  
  220. $meta_query[] = array('_price_clause' =>
  221.  
  222. array(
  223. 'key' => '_price',
  224. 'value' => array($urlParameters['min_price'],$urlParameters['max_price']),
  225. 'compare' => 'BETWEEN',
  226. 'type' => 'NUMERIC'
  227.  
  228.  
  229. )
  230. );
  231. }
  232.  
  233. if(!empty($urlParameters['orderby'])){
  234.  
  235. $orderDetails = $urlParameters['orderby'];
  236.  
  237. if($orderDetails == 'price'){
  238.  
  239. if(!empty($urlParameters['min_price']))
  240.  
  241. $args['orderby'] = array('_price_clause' => 'ASC');
  242.  
  243. else{
  244.  
  245. $args['meta_key'] ='_price';
  246. $args['orderby'] = 'meta_value_num';
  247. $args['order'] = 'ASC';
  248. }
  249. }
  250. else if($orderDetails == 'popularity'){
  251.  
  252. $args['meta_key'] = 'total_sales';
  253. $args['orderby'] = array('total_sales'=>'DESC','date'=>'DESC');
  254.  
  255. }
  256. else if($orderDetails == 'date'){
  257.  
  258. $args['orderby'] = array('date'=>'DESC');
  259.  
  260.  
  261. }
  262. else if($orderDetails == 'price-desc'){
  263.  
  264. if(!empty($urlParameters['min_price']))
  265.  
  266. $args['orderby'] = array('_price_clause' => 'DESC');
  267.  
  268. else{
  269.  
  270. $args['meta_key'] = '_price';
  271. $args['orderby'] = 'meta_value_num';
  272. $args['order'] = 'DESC';
  273.  
  274. }
  275.  
  276. }
  277. }
  278.  
  279. if(!empty($urlParameters['productCat'])){
  280.  
  281. $tax_query[] = array('taxonomy'=>'product_cat',
  282. 'field' => 'slug',
  283. 'terms' => explode(',',$urlParameters['productCat']),
  284. 'operator' => 'IN'
  285. );
  286.  
  287. if(!isset($args['orderby']))
  288. $args['orderby'] = array('date'=>'DESC');
  289. }
  290.  
  291. if(!empty($urlParameters['textSearch'])){
  292. $args['s'] = $urlParameters['textSearch'];
  293. }
  294.  
  295.  
  296.  
  297.  
  298. if(!empty($meta_query))
  299. $args['meta_query'] = $meta_query;
  300.  
  301.  
  302. if(!empty($tax_query))
  303. $args['tax_query'] = $tax_query;
  304.  
  305. //echo json_encode($args);
  306.  
  307. $moreProducts = new WP_Query($args);
  308.  
  309.  
  310. if($moreProducts->have_posts()):
  311.  
  312. ob_start();
  313. while($moreProducts->have_posts()): $moreProducts->the_post();
  314. if(in_array(get_the_id(),$existProductIds))
  315. continue;
  316.  
  317. wc_get_template_part( 'content', 'product' );
  318.  
  319. endwhile;
  320.  
  321. echo ob_get_clean();
  322. wp_reset_postdata();
  323. else:
  324. echo 'end';
  325.  
  326.  
  327. endif;
  328.  
  329.  
  330. wp_die();
  331.  
  332. }
  333.  
  334.  
  335.  
  336. //////////////// increase external product populatiry ////////////////
  337.  
  338. add_action('wp_ajax_nopriv_increase_external_populatiry','increase_external_populatiry_func');
  339. add_action('wp_ajax_increase_external_populatiry','increase_external_populatiry_func');
  340.  
  341.  
  342. function increase_external_populatiry_func(){
  343.  
  344. $product_id = absint($_POST['product_id']);
  345.  
  346. if($product_id && wc_get_product($product_id)->get_type() == 'external' ){
  347.  
  348. $lastCount = get_post_meta($product_id,'total_sales',true);
  349.  
  350. if(!empty($lastCount)){
  351.  
  352. $updated = update_post_meta($product_id,'total_sales',$lastCount+1);
  353.  
  354. }else{
  355. $updated = update_post_meta($product_id,'total_sales',1);
  356. }
  357.  
  358. if($updated == true)
  359. echo 'success';
  360. else if($updated == false)
  361. echo 'failed';
  362. else
  363. echo 'added';
  364.  
  365. }else{
  366. echo 'not external';
  367. }
  368.  
  369. wp_die();
  370. }
  371.  
  372.  
  373.  
  374.  
  375. remove_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',10);
  376. remove_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',20);
  377.  
  378.  
  379. add_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',20);
  380.  
  381.  
  382. //remove the rating order
  383. add_filter('woocommerce_catalog_orderby',function($order_arr){
  384. unset($order_arr['rating']);
  385. return $order_arr;
  386. },20);
  387.  
  388.  
  389.  
  390. // remove shop pagination
  391. remove_action('woocommerce_after_shop_loop','woocommerce_pagination');
  392.  
  393.  
  394.  
  395. ///////////////////////////////////////////////////////////////////////////////////maintain theme functions //////////////////////////////
  396.  
  397.  
  398.  
  399. $archive_results_count = generatepress_wc_get_setting( 'product_results_count' );
  400. $archive_sorting = generatepress_wc_get_setting( 'product_sorting' );
  401. $archive_image = generatepress_wc_get_setting( 'product_archive_image' );
  402. $archive_sale_flash = generatepress_wc_get_setting( 'product_archive_sale_flash' );
  403. $archive_sale_flash_overlay = generatepress_wc_get_setting( 'product_archive_sale_flash_overlay' );
  404. $archive_rating = generatepress_wc_get_setting( 'product_archive_rating' );
  405. $archive_price = generatepress_wc_get_setting( 'product_archive_price' );
  406. $archive_add_to_cart = generatepress_wc_get_setting( 'product_archive_add_to_cart' );
  407. $archive_title = generatepress_wc_get_setting( 'product_archive_title' );
  408. $product_tabs = generatepress_wc_get_setting( 'product_tabs' );
  409. $product_related = generatepress_wc_get_setting( 'product_related' );
  410. $product_upsells = generatepress_wc_get_setting( 'product_upsells' );
  411. $product_meta = generatepress_wc_get_setting( 'product_meta' );
  412. $product_description = generatepress_wc_get_setting( 'product_description' );
  413. $breadcrumbs = generatepress_wc_get_setting( 'breadcrumbs' );
  414. $page_title = generatepress_wc_get_setting( 'shop_page_title' );
  415. $distraction_free = generatepress_wc_get_setting( 'distraction_free' );
  416. $archive_description = generatepress_wc_get_setting( 'product_archive_description' );
  417.  
  418. if ( false === $archive_sale_flash_overlay ) {
  419. remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
  420. add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 6 );
  421. }
  422.  
  423. if ( false === $page_title ) {
  424. add_filter( 'woocommerce_show_page_title' , '__return_false' );
  425. }
  426.  
  427. if ( false === $archive_results_count ) {
  428. remove_action( 'woocommerce_after_shop_loop', 'woocommerce_result_count', 20 );
  429. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
  430. }
  431.  
  432. if ( false === $archive_sorting ) {
  433. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 10 );
  434. }
  435.  
  436. if ( false === $archive_image ) {
  437. remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
  438. }
  439.  
  440. if ( false === $archive_sale_flash ) {
  441. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 6 );
  442. }
  443.  
  444. if ( false === $archive_rating ) {
  445. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
  446. }
  447.  
  448. if ( false === $archive_price ) {
  449. remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
  450. }
  451.  
  452. if ( false === $archive_add_to_cart ) {
  453. remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  454. }
  455.  
  456. if ( false === $archive_title ) {
  457. remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
  458. }
  459.  
  460. if ( false === $product_tabs ) {
  461. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
  462. }
  463.  
  464. if ( false === $product_related ) {
  465. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
  466. }
  467.  
  468. if ( false === $product_upsells ) {
  469. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  470. }
  471.  
  472. if ( false === $product_meta ) {
  473. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
  474. }
  475.  
  476. if ( false === $product_description ) {
  477. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
  478. }
  479.  
  480. if ( false === $breadcrumbs ) {
  481. remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
  482. }
  483.  
  484. if ( true === $distraction_free ) {
  485. add_filter( 'generate_sidebar_layout','generatepress_wc_checkout_sidebar_layout' );
  486. add_filter( 'generate_footer_widgets','generatepress_wc_checkout_footer_widgets' );
  487. }
  488.  
  489. if ( true === $archive_description && ! is_single() && ! is_cart() ) {
  490. add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_single_excerpt', 5 );
  491. add_action( 'woocommerce_after_subcategory_title', 'generatepress_wc_category_description', 12 );
  492. }
  493.  
  494.  
  495. //keep posts per page on searching too
  496. add_action( 'woocommerce_product_query', function( $q){
  497. $q->set('posts_per_page',generatepress_wc_get_setting( 'products_per_page' ));
  498.  
  499. }, PHP_INT_MAX );
  500.  
  501. //fix gif croppiiing
  502. add_filter('single_product_archive_thumbnail_size',function(){
  503. global $post;
  504. $thumb_url = get_the_post_thumbnail_url();
  505.  
  506. $thumb_low = strtolower($thumb_url);
  507.  
  508. if (strpos($thumb_low, '.gif') === false) {
  509.  
  510. $thumb_size = 'woocommerce_thumbnail';
  511.  
  512. } else {
  513.  
  514. $thumb_size = 'woocommerce_single';
  515.  
  516. }
  517.  
  518. return $thumb_size;
  519.  
  520. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement