Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- //add the price and rating out of the affiliate link
- remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price');
- remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating');
- add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_rating',6);
- add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_price',7);
- //remove the product title from the original place and add it before the image
- remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title');
- add_action('woocommerce_before_shop_loop_item',function(){
- if(generatepress_wc_get_setting( 'product_archive_title' ) != false){
- echo '<a href="' . esc_url( get_the_permalink() ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
- woocommerce_template_loop_product_title();
- echo '</a>';
- }
- },2);
- function tu_custom_template_loop_product_link_open() {
- global $product;
- $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
- echo '<a target="_blank" href="' . esc_url( $link ) . '" class="blank_affiliate_link woocommerce-LoopProduct-link woocommerce-loop-product__link">';
- }
- //attach affiliate link to the product image
- add_action('woocommerce_before_shop_loop_item',function(){
- global $product;
- if($product->get_type() == 'external'){
- remove_action('woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open');
- add_action('woocommerce_before_shop_loop_item','tu_custom_template_loop_product_link_open',5);
- add_filter('woocommerce_loop_product_link',function($product_link,$product){
- return $product->get_product_url();
- },3,2);
- }
- },2);
- //making the link opens in a new tab
- add_filter('woocommerce_loop_add_to_cart_link',function($add_to_cart_link,$product,$args){
- if($product->get_type() == 'external'){
- $args['attributes']['target'] ='_blank';
- }
- return sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
- esc_url( $product->add_to_cart_url() ),
- esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
- esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
- isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
- esc_html( $product->add_to_cart_text() )
- );
- },1,3);
- // limit the characters of the product description
- function the_excerpt_max_charlength($charlength,$product_excerpt) {
- $excerpt = $product_excerpt;
- $charlength++;
- if ( mb_strlen( $excerpt ) > $charlength ) {
- $subex = mb_substr( $excerpt, 0, $charlength - 5 );
- $exwords = explode( ' ', $subex );
- $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
- if ( $excut < 0 ) {
- return mb_substr( $subex, 0, $excut );
- } else {
- return $subex;
- }
- } else {
- return $excerpt;
- }
- }
- //customizing the product short description for styling
- add_filter('woocommerce_short_description',function($product_exerpt){
- if( !empty($product_exerpt) ):
- $product_exerpt = the_excerpt_max_charlength(400,$product_exerpt);
- return '<div class="product-short-description-container">'. $product_exerpt . '</div>';
- else:
- return $product_exerpt;
- endif;
- });
- //remove the rating from the product loop
- remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating',5);
- //add the css and js files
- add_action('wp_enqueue_scripts',function(){
- wp_enqueue_style('frontend-styles', get_template_directory_uri() . '/css/frontend-styles.css');
- wp_enqueue_script( 'frontend-actions', get_template_directory_uri() . '/js/actions.js', array('jquery'), '1.0.0', true );
- });
- //adding custom wrapping div for styling
- add_action('woocommerce_before_shop_loop_item_title',function(){
- echo '<div class="product-loop-rest-container">';
- },5);
- add_action('woocommerce_after_shop_loop_item',function(){
- echo '</div>';
- },100);
- remove_action('woocommerce_before_shop_loop','woocommerce_before_shop_loop');
- add_action('woocommerce_after_shop_loop',function(){
- //print_r(did_action('woocommerce_before_shop_loop'));
- });
- add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ), 20 );
- /*
- [woof products_per_page=_1 columns=4 is_ajax=0 taxonomies=product_cat:9 custom_tpl='themes/generatepress/woo_tpl_1.php']
- */
- add_action('wp_enqueue_scripts',function(){
- wp_localize_script( 'jquery','special_ajax_data' , array('ajax_url' => admin_url('admin-ajax.php'),'nonce'=>wp_create_nonce('wp_ajax_nonce')));
- },100);
- //load more products through ajax
- add_action('wp_ajax_nopriv_load_more_products','load_more_products_func');
- add_action('wp_ajax_load_more_products','load_more_products_func');
- function load_more_products_func(){
- $paged = $_POST['paged']+1;
- $existProductIds = $_POST['productIds'];
- $urlParameters = $_POST['searchParameters'];
- $args = array(
- 'post_type' => 'product',
- 'paged' => $paged,
- 'posts_per_page' => generatepress_wc_get_setting( 'products_per_page' )
- );
- $meta_query = array();
- $tax_query = array();
- if(!empty($urlParameters['min_price']) && !empty($urlParameters['max_price'])){
- $meta_query['relation'] = 'AND' ;
- $meta_query[] = array('_price_clause' =>
- array(
- 'key' => '_price',
- 'value' => array($urlParameters['min_price'],$urlParameters['max_price']),
- 'compare' => 'BETWEEN',
- 'type' => 'NUMERIC'
- )
- );
- }
- if(!empty($urlParameters['orderby'])){
- $orderDetails = $urlParameters['orderby'];
- if($orderDetails == 'price'){
- if(!empty($urlParameters['min_price']))
- $args['orderby'] = array('_price_clause' => 'ASC');
- else{
- $args['meta_key'] ='_price';
- $args['orderby'] = 'meta_value_num';
- $args['order'] = 'ASC';
- }
- }
- else if($orderDetails == 'popularity'){
- $args['meta_key'] = 'total_sales';
- $args['orderby'] = array('total_sales'=>'DESC','date'=>'DESC');
- }
- else if($orderDetails == 'date'){
- $args['orderby'] = array('date'=>'DESC');
- }
- else if($orderDetails == 'price-desc'){
- if(!empty($urlParameters['min_price']))
- $args['orderby'] = array('_price_clause' => 'DESC');
- else{
- $args['meta_key'] = '_price';
- $args['orderby'] = 'meta_value_num';
- $args['order'] = 'DESC';
- }
- }
- }
- if(!empty($urlParameters['productCat'])){
- $tax_query[] = array('taxonomy'=>'product_cat',
- 'field' => 'slug',
- 'terms' => explode(',',$urlParameters['productCat']),
- 'operator' => 'IN'
- );
- if(!isset($args['orderby']))
- $args['orderby'] = array('date'=>'DESC');
- }
- if(!empty($urlParameters['textSearch'])){
- $args['s'] = $urlParameters['textSearch'];
- }
- if(!empty($meta_query))
- $args['meta_query'] = $meta_query;
- if(!empty($tax_query))
- $args['tax_query'] = $tax_query;
- //echo json_encode($args);
- $moreProducts = new WP_Query($args);
- if($moreProducts->have_posts()):
- ob_start();
- while($moreProducts->have_posts()): $moreProducts->the_post();
- if(in_array(get_the_id(),$existProductIds))
- continue;
- wc_get_template_part( 'content', 'product' );
- endwhile;
- echo ob_get_clean();
- wp_reset_postdata();
- else:
- echo 'end';
- endif;
- wp_die();
- }
- //////////////// increase external product populatiry ////////////////
- add_action('wp_ajax_nopriv_increase_external_populatiry','increase_external_populatiry_func');
- add_action('wp_ajax_increase_external_populatiry','increase_external_populatiry_func');
- function increase_external_populatiry_func(){
- $product_id = absint($_POST['product_id']);
- if($product_id && wc_get_product($product_id)->get_type() == 'external' ){
- $lastCount = get_post_meta($product_id,'total_sales',true);
- if(!empty($lastCount)){
- $updated = update_post_meta($product_id,'total_sales',$lastCount+1);
- }else{
- $updated = update_post_meta($product_id,'total_sales',1);
- }
- if($updated == true)
- echo 'success';
- else if($updated == false)
- echo 'failed';
- else
- echo 'added';
- }else{
- echo 'not external';
- }
- wp_die();
- }
- remove_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',10);
- remove_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',20);
- add_action('woocommerce_before_shop_loop_item_title','woocommerce_template_single_excerpt',20);
- //remove the rating order
- add_filter('woocommerce_catalog_orderby',function($order_arr){
- unset($order_arr['rating']);
- return $order_arr;
- },20);
- // remove shop pagination
- remove_action('woocommerce_after_shop_loop','woocommerce_pagination');
- ///////////////////////////////////////////////////////////////////////////////////maintain theme functions //////////////////////////////
- $archive_results_count = generatepress_wc_get_setting( 'product_results_count' );
- $archive_sorting = generatepress_wc_get_setting( 'product_sorting' );
- $archive_image = generatepress_wc_get_setting( 'product_archive_image' );
- $archive_sale_flash = generatepress_wc_get_setting( 'product_archive_sale_flash' );
- $archive_sale_flash_overlay = generatepress_wc_get_setting( 'product_archive_sale_flash_overlay' );
- $archive_rating = generatepress_wc_get_setting( 'product_archive_rating' );
- $archive_price = generatepress_wc_get_setting( 'product_archive_price' );
- $archive_add_to_cart = generatepress_wc_get_setting( 'product_archive_add_to_cart' );
- $archive_title = generatepress_wc_get_setting( 'product_archive_title' );
- $product_tabs = generatepress_wc_get_setting( 'product_tabs' );
- $product_related = generatepress_wc_get_setting( 'product_related' );
- $product_upsells = generatepress_wc_get_setting( 'product_upsells' );
- $product_meta = generatepress_wc_get_setting( 'product_meta' );
- $product_description = generatepress_wc_get_setting( 'product_description' );
- $breadcrumbs = generatepress_wc_get_setting( 'breadcrumbs' );
- $page_title = generatepress_wc_get_setting( 'shop_page_title' );
- $distraction_free = generatepress_wc_get_setting( 'distraction_free' );
- $archive_description = generatepress_wc_get_setting( 'product_archive_description' );
- if ( false === $archive_sale_flash_overlay ) {
- remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
- add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 6 );
- }
- if ( false === $page_title ) {
- add_filter( 'woocommerce_show_page_title' , '__return_false' );
- }
- if ( false === $archive_results_count ) {
- remove_action( 'woocommerce_after_shop_loop', 'woocommerce_result_count', 20 );
- remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
- }
- if ( false === $archive_sorting ) {
- remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 10 );
- }
- if ( false === $archive_image ) {
- remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
- }
- if ( false === $archive_sale_flash ) {
- remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 6 );
- }
- if ( false === $archive_rating ) {
- remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
- }
- if ( false === $archive_price ) {
- remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
- }
- if ( false === $archive_add_to_cart ) {
- remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
- }
- if ( false === $archive_title ) {
- remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
- }
- if ( false === $product_tabs ) {
- remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
- }
- if ( false === $product_related ) {
- remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
- }
- if ( false === $product_upsells ) {
- remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
- }
- if ( false === $product_meta ) {
- remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
- }
- if ( false === $product_description ) {
- remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
- }
- if ( false === $breadcrumbs ) {
- remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
- }
- if ( true === $distraction_free ) {
- add_filter( 'generate_sidebar_layout','generatepress_wc_checkout_sidebar_layout' );
- add_filter( 'generate_footer_widgets','generatepress_wc_checkout_footer_widgets' );
- }
- if ( true === $archive_description && ! is_single() && ! is_cart() ) {
- add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_single_excerpt', 5 );
- add_action( 'woocommerce_after_subcategory_title', 'generatepress_wc_category_description', 12 );
- }
- //keep posts per page on searching too
- add_action( 'woocommerce_product_query', function( $q){
- $q->set('posts_per_page',generatepress_wc_get_setting( 'products_per_page' ));
- }, PHP_INT_MAX );
- //fix gif croppiiing
- add_filter('single_product_archive_thumbnail_size',function(){
- global $post;
- $thumb_url = get_the_post_thumbnail_url();
- $thumb_low = strtolower($thumb_url);
- if (strpos($thumb_low, '.gif') === false) {
- $thumb_size = 'woocommerce_thumbnail';
- } else {
- $thumb_size = 'woocommerce_single';
- }
- return $thumb_size;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement