Forum Replies Created

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter pacificshack

    (@pacificshack)

    OK ^ ignore last post, I managed to use 3 x functions to get what I wanted to achieve. Posting this here incase other people may find it useful:

    // Disable FREE Shipping if product in cart has shipping class 
    function ps_hide_free_shipping_for_shipping_class( $rates, $package ) {
    	$shipping_class_target = 74; // shipping class ID (to find it, see screenshot below)
    	$in_cart = false;
    	foreach( WC()->cart->cart_contents as $key => $values ) {
    		if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
    			$in_cart = true;
    			break;
    		} 
    	}
    	if( $in_cart ) {
    		unset( $rates['free_shipping:2'] ); // shipping method with ID (to find it, see screenshot below)
    	}
    	return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'ps_hide_free_shipping_for_shipping_class', 10, 2 );
    
    // Hide shipping rates when free shipping available
    function my_hide_shipping_when_free_is_available( $rates ) {
    	$free = array();
    	foreach ( $rates as $rate_id => $rate ) {
    		if ( 'free_shipping' === $rate->method_id ) {
    			$free[ $rate_id ] = $rate;
    			break;
    		}
    	}
    	return ! empty( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
    
    // Amend Flat Rate amount if over $100 and shipping class
    function wc_ninja_change_flat_rates_cost( $rates, $package ) {
    	// Current value of the shopping cart
    	$cart_subtotal = WC()->cart->subtotal;
    	// Check if the subtotal is greater than $100
    	if ( $cart_subtotal >= 100 ) {
    		// Set the cost minus $11.99 for shipping rate
    		$rates['flat_rate:1']->cost = $rates['flat_rate:1']->cost-11.99;
    	}
    	return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'wc_ninja_change_flat_rates_cost', 10, 2 );
    
    Thread Starter pacificshack

    (@pacificshack)

    OK thanks for your response. I am going to look at using the plugin for the solution.

    Lowering the cost of the shipping class to ‘force’ the correct value won’t work, as the class will be applied to each specific product, so if a customer orders 2 x mirrors, they need to be charged the shipping class twice ($40 x 2). And the value of the mirrors will be over $100, so it’ll need to void out the first $10 charge, giving a total of $80 shipping. This also has to void out the first $10 charge if other items which do not have the shipping class assigned are in the cart.

    Thread Starter pacificshack

    (@pacificshack)

    Hi Frank, thanks for your response. I actually ended up creating my own ‘app’ in the end which sits on a WordPress page. The page has:

    – 12 questions
    – Each question is answered by choosing one of 10 radio buttons (choosing 1-10)
    – The user enters name and email to get results.
    – Form is sent to a php form using ajax. Details are sent to a database for reference. Details are then sent out on a HTML email using phpMailer to the user. Each question has a copy of their score with the answer.
    – Questions and answers set up in an external database.
    – If the user answered ‘3’ they are given an answer which has a range of ‘1-3’.
    – Email has call to action to encourage user to get in touch.

    Could add more to it, but better to keep simple. Cheers.

    Thread Starter pacificshack

    (@pacificshack)

    Cheers Brandon, i’ll look at updating the queries when I get it finished!

    Next issue I am having is getting Fancybox to work on the images. I am using Easy Fancybox which fires the images open, no problems there. I am then trying to pass through the ID of the post through the alt tag in jQuery. Then I need to pass it to php and get the post from the DB. Once I have that I can pull in the tags and other data the post has which I need to display.

    Also, the code above, I have since made a heap of changes and found putting in the jQuery in the footer the better option. So keeping in the php at the top of the page, the rest of the html looks like:

    <div id="pageRowMasonry" class="pageRow js-masonry" id="contentScroller" data-masonry-options='{ "columnWidth": 200, "itemSelector": ".pageGalleryContainer" }'>
    
    <?php
    $count = 0;
    
    // Loop first wp_posts
    while ($row = mysql_fetch_array($query)):
    $count++;
    	// Look for featured image in WP_postmeta
    	$sqlMETA1 = "SELECT * FROM wp_postmeta WHERE post_id = '".$row['ID']."'";
    	$queryMETA1 = mysql_query($sqlMETA1);
    	$rowMETA1 = mysql_fetch_array($queryMETA1);
    
    	// Look for _thumbnail_id
    	if ($rowMETA1['meta_key'] == '_thumbnail_id') {
    		$sqlMETA2 = "SELECT * FROM wp_postmeta WHERE post_id = '".$rowMETA1['meta_value']."'";
    		$queryMETA2 = mysql_query($sqlMETA2);
    		$rowMETA2 = mysql_fetch_array($queryMETA2);
    
    		// Look for categories through WP
    		$sqlTERMS = "SELECT * FROM wp_terms WHERE name = 'Gallery (Double)'";
    		$queryTERMS = mysql_query($sqlTERMS);
    		$rowTERMS = mysql_fetch_array($queryTERMS);
    
    		$sqlTAX = "SELECT * FROM wp_term_taxonomy WHERE term_id = '".$rowTERMS['term_id']."'";
    		$queryTAX = mysql_query($sqlTAX);
    		$rowTAX = mysql_fetch_array($queryTAX);
    
    		$sqlRELS = "SELECT * FROM wp_term_relationships WHERE object_id = '".$row['ID']."' AND term_taxonomy_id = '".$rowTAX['term_taxonomy_id']."'";
    		$queryRELS = mysql_query($sqlRELS);
    		$rowRELS = mysql_fetch_array($queryRELS);
    
    		if (isset($rowRELS['object_id'])) {
    			$pageGalleryContainerType = 'pageGalleryContainerDouble';
    		} else { $pageGalleryContainerType = 'pageGalleryContainerSingle'; }
    		?>
    		<div class="pageGalleryContainer pageGalleryContainer<?php echo $page.$count; ?> <?php echo $pageGalleryContainerType; ?> masonry-brick" style="position: absolute;">
    			<a tabindex="1" class="fancybox" rel="gallery" href="http://beta.photographersinc.co.nz/wp-content/uploads/<?php echo $rowMETA2['meta_value']; ?>" title="<?php echo $row['ID']; ?>">
    				<div class="pageGalleryContainerText"><h3 class="displayNone"><?php echo $row['post_title']; ?></h3></div>
    				<img width="474" height="316" src="http://beta.photographersinc.co.nz/wp-content/uploads/<?php echo $rowMETA2['meta_value']; ?>" class="attachment-large wp-post-image" alt="<?php echo $row['ID']; ?>" title="<?php echo $row['post_title']; ?>">
    			</a>
    		</div>
    <?php } ?>
    <?php endwhile; ?>
    
    <!--page navigation-->
    <?php if (isset($next)): ?>
    <div class="page-nav">
    	<a href='http://beta.photographersinc.co.nz/gallery.php?page=<?php echo $next?>'>Next</a>
    </div>
    <?php endif?>
    
    </div>

    Then in the footer:

    <script>
    	jQuery(function(){
    
    		var $container = jQuery('#pageRowMasonry');
    
    		$container.imagesLoaded(function(){
    			$container.masonry({
    			itemSelector: '.pageGalleryContainer',
    			columnWidth: 100
    			});
    		});
    
    		$container.infinitescroll({
    		navSelector  : '.page-nav',    // selector for the paged navigation
    		nextSelector : '.page-nav a',  // selector for the NEXT link (to page 2)
    		itemSelector : '.pageGalleryContainer',     // selector for all items you'll retrieve
    			loading: {
    			finishedMsg: 'No more pages to load.',
    			img: 'http://i.imgur.com/6RMhx.gif'
    			}
    		},
    		// trigger Masonry as a callback
    		function( newElements ) {
    			// hide new items while they are loading
    			var $newElems = jQuery( newElements ).css({ opacity: 0 });
    			// ensure that images load before adding to masonry layout
    			$newElems.imagesLoaded(function(){
    				// show elems now they're ready
    				$newElems.animate({ opacity: 1 });
    				$container.masonry( 'appended', $newElems, true );
    			});
    
    			jQuery(newElements).each(function() {
    				// Image hovers
    				jQuery(this).on("hover",function(){
    					jQuery(this).find('img').toggleClass('pageGalleryContainerImageRollover');
    					jQuery(this).find('h3').toggleClass('displayNone');
    				});
    
    				jQuery(this).on("click",function(){
    					jQuery.ajax({
    					type: 'POST',
    					url: 'http://beta.website.com/wp-content/themes/photographersinc/update-session.php',
    					data: {
    						galleryValue: jQuery(this).find('img').attr("alt")
    						}
    					});
    				});
    
    				// Fancybox
    				jQuery("a.fancybox").fancybox({
    					// fancybox API options here
    					'padding': 10,
    					'titlePosition'  : 'over',
    					'titleFromAlt' :  true,
    					'onComplete': function(){
    						jQuery('#fancybox-title-over').append(' test ');
    						jQuery('#fancybox-title-over').append(' <?php echo $_SESSION['SESSIONaltValue']; ?> ');
    					}
    				});
    
    			});
    		});
    	});
    	</script>

    Masonry is now working really well with Infinite Scroll. Fancybox is working fine too. Just need to be able to pass the ID as a jQuery variable via AJAX to php so I can look up the ID in the database. Getting there.

    Thread Starter pacificshack

    (@pacificshack)

    OK, some good news! Infinite scroll on WordPress with custom post types inside Masonry – works! Yippee!

    I set up an includes file called page-gallery.inc – and I point to it if the user is on the Gallery page, then use the following code:

    <?php
    # db configuration
    define('DB_HOST', '<DB_HOST>');
    define('DB_USER', '<DB_USER>');
    define('DB_PASS', '<DB_PASS>');
    define('DB_NAME', '<DB_NAME>');
    $limit = 10; #item per page
    # db connect
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to MySQL DB ') . mysql_error();
    $db = mysql_select_db(DB_NAME, $link);
    $page = (!isset($_GET['page'])) ? 1 : $_GET['page'];
    # sql query
    $sql = "SELECT * FROM wp_posts WHERE post_type = 'gallery' AND post_status = 'publish' ORDER BY post_title ASC";
    # find out query stat point
    $start = ($page * $limit) - $limit;
    # query for page navigation
    if( mysql_num_rows(mysql_query($sql)) > ($page * $limit) ){
    	$next = ++$page;
    }
    $query = mysql_query( $sql . " LIMIT {$start}, {$limit}");
    if (mysql_num_rows($query) < 1) {
    	header('HTTP/1.0 404 Not Found');
    	echo 'Page not found!';
    	exit();
    }
    ?>
    
    <div id="pageRowMasonry" class="pageRow js-masonry" id="contentScroller" data-masonry-options='{ "columnWidth": 200, "itemSelector": ".pageGalleryContainer" }'>
    
    	<?php
    	// Loop first wp_posts
    	while ($row = mysql_fetch_array($query)):
    		// Look for featured image in WP_postmeta
    		$sqlMETA1 = "SELECT * FROM wp_postmeta WHERE post_id = '".$row['ID']."'";
    		$queryMETA1 = mysql_query($sqlMETA1);
    		$rowMETA1 = mysql_fetch_array($queryMETA1);
    
    		// Look for _thumbnail_id
    		if ($rowMETA1['meta_key'] == '_thumbnail_id') {
    			$sqlMETA2 = "SELECT * FROM wp_postmeta WHERE post_id = '".$rowMETA1['meta_value']."'";
    			$queryMETA2 = mysql_query($sqlMETA2);
    			$rowMETA2 = mysql_fetch_array($queryMETA2);
    
    			// Look for categories through WP
    			$sqlTERMS = "SELECT * FROM wp_terms WHERE name = 'Gallery (Double)'";
    			$queryTERMS = mysql_query($sqlTERMS);
    			$rowTERMS = mysql_fetch_array($queryTERMS);
    
    			$sqlTAX = "SELECT * FROM wp_term_taxonomy WHERE term_id = '".$rowTERMS['term_id']."'";
    			$queryTAX = mysql_query($sqlTAX);
    			$rowTAX = mysql_fetch_array($queryTAX);
    
    			$sqlRELS = "SELECT * FROM wp_term_relationships WHERE object_id = '".$row['ID']."' AND term_taxonomy_id = '".$rowTAX['term_taxonomy_id']."'";
    			$queryRELS = mysql_query($sqlRELS);
    			$rowRELS = mysql_fetch_array($queryRELS);
    
    			if (isset($rowRELS['object_id'])) {
    				$pageGalleryContainerType = 'pageGalleryContainerDouble';
    			} else { $pageGalleryContainerType = 'pageGalleryContainerSingle'; }
    			?>
    			<div class="pageGalleryContainer <?php echo $pageGalleryContainerType; ?> masonry-brick" style="position: absolute;">
    				<a href="http://beta.photographersinc.co.nz/wp-content/uploads/<?php echo $rowMETA2['meta_value']; ?>" class="fancybox">
    					<div class="pageGalleryContainerText"><h3 class="displayNone"><?php echo $row['post_title']; ?></h3></div>
    					<img width="474" height="316" src="http://beta.photographersinc.co.nz/wp-content/uploads/<?php echo $rowMETA2['meta_value']; ?>" class="attachment-large wp-post-image" alt="<?php echo $row['post_title']; ?>">
    				</a>
    			</div>
    	<?php } ?>
    	<?php endwhile; ?>
    
    	<!--page navigation-->
    	<?php if (isset($next)): ?>
    	<div class="page-nav">
    		<a href='http://beta.photographersinc.co.nz/gallery.php?page=<?php echo $next?>'>Next</a>
    	</div>
    	<?php endif?>
    
    	<script src="<?php bloginfo('template_directory'); ?>/js/masonry.pkgd.min.js"></script>
    	<script src="<?php bloginfo('template_directory'); ?>/js/jquery.infinitescroll.min.js"></script>
    	<script>
    	  jQuery(function(){
    
    	    var $container = jQuery('#pageRowMasonry');
    
    	    $container.imagesLoaded(function(){
    	      $container.masonry({
    	        itemSelector: '.pageGalleryContainer',
    	        columnWidth: 100
    	      });
    	    });
    
    	    $container.infinitescroll({
    	      navSelector  : '.page-nav',    // selector for the paged navigation
    	      nextSelector : '.page-nav a',  // selector for the NEXT link (to page 2)
    	      itemSelector : '.pageGalleryContainer',     // selector for all items you'll retrieve
    	      loading: {
    	          finishedMsg: 'No more pages to load.',
    	          img: 'http://i.imgur.com/6RMhx.gif'
    	        }
    	      },
    	      // trigger Masonry as a callback
    	      function( newElements ) {
    	        // hide new items while they are loading
    	        var $newElems = jQuery( newElements ).css({ opacity: 0 });
    	        // ensure that images load before adding to masonry layout
    	        $newElems.imagesLoaded(function(){
    	          // show elems now they're ready
    	          $newElems.animate({ opacity: 1 });
    	          $container.masonry( 'appended', $newElems, true );
    	        });
    	      }
    	    );
    
    	  });
    	</script>
    </div>

    Bingo. As the page scrolls down it loads in more posts of the type ‘gallery’ which have been set up as a custom post type.

    Hope this helps other people, it has taken me a long time to resolve.

    Thread Starter pacificshack

    (@pacificshack)

    I am building a beta site for a client at the mo and it’s private so I can’t send a link yet.

    I managed to get JetPack working on Posts which are used on a blog, and I was able to target the second ‘set’ of 10 posts loaded which were duplicates using CSS, and I then turned them off so they don’t appear:

    .pagePhotoshootsContainer:nth-child(10) { display: none; }

    I’ve since abandoned JetPack. For just using Infinite Scroll it’s not worth it. The other issue was using it across custom post types, which I thought I had nailed, but not. I need infinite scroll used on a long list of images.

    So instead I’ve gone for this solution:
    http://www.w3bees.com/2013/09/jquery-infinite-scroll-with-php-mysql.html

    Quite a lightweight infinite scroll and works well. The only drawback is I am having to use direct mysql queries, which isn’t too bad as I find it easier anyway as oppose to using WordPress specific functions.

    I am now able to get it working across custom post types, and using masonry. Only issue is now getting the second set of posts moving which I’m sure i’ll nail today.

    This is the Masonry resource: http://masonry.desandro.com/

    Thread Starter pacificshack

    (@pacificshack)

    Have found a solution for adding a one off fee to cart, depending on the post meta:

    In functions.php file

    // WooCommerce hook to add LogoUpload value to cart
    add_action( 'woocommerce_before_calculate_totals', 'cp_add_custom_price' );
    function cp_add_custom_price( $cart_object ) {
    
        global $woocommerce;
        foreach ( $cart_object->cart_contents as $key => $value ) {
    
            //$proid = $value['product_id'];
            //$variantid = $value['variation_id'];
            //$price = $value['data']->price;
            $cartItemData = $woocommerce->cart->get_item_data($value);
    
            if (preg_match("/Yes, upload logo/i", $cartItemData)) {
    		    $excost = 99;
    			$woocommerce->cart->add_fee( 'Logo set up fee', $excost, $taxable = false, $tax_class = '' );
    		}
    
       }
    }

    This adds the function to the cart. It gets the item data, and uses a php function to scan the content. If it matches a certain term, add the fee, if it doesn’t don’t add the fee.

    Done.

    The easiest way is to:
    1. Create a new file in your theme called app.css
    2. Call in the css file in your head
    3. Start writing CSS in the file.

    For example, you can write:
    .product-image .front-image img { width: 200px !important; }

    The !important will overwrite the width value of your theme with a new value.

    A great app to write CSS is Coda2 from Panic. I started using Dreamweaver in the late 90s, and switched to Coda years back. It’s got brilliant CSS libraries which are easy to learn and play with 😉

    Another good tip, is to use Google Chrome. Right click on the image to Inspect Element. In the developer tools you’ll see a window bottom right where you can play with the CSS to get the right measurements. I use this alot of the time to measure up what I need, and then cut and paste over in to my app.css file in Coda2. Just remember not to refresh or click links in the page in Chrome, otherwise you’ll lose the CSS changes you are making.

    How about overwriting the CSS?

    Look for this class:
    .product-image .front-image img
    and
    .product-image .back-image img

    Change the width value from auto to what px you need.

    Just be aware, all your images are different heights, so unless you are going to have a fixed height and the image sits inside vertically, you won’t get it lining up nicely 😉

Viewing 9 replies - 1 through 9 (of 9 total)