Skip to content

Instantly share code, notes, and snippets.

@mustafix
Last active September 20, 2021 05:29
Show Gist options
  • Save mustafix/85609c620f1e21504da0e2ebe4eb48f5 to your computer and use it in GitHub Desktop.
Save mustafix/85609c620f1e21504da0e2ebe4eb48f5 to your computer and use it in GitHub Desktop.
global $product;
global $woocommerce;
$idd = get_the_ID();
//Displaying a WooCommerce product price:
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
// Displaying a WooCommerce price Currency:
$price_currency = get_woocommerce_currency_symbol();
// Product add to cart url:
$add_cat_url = $product->add_to_cart_url();
//Displaying product ratings / Displaying the number of product reviews:
$review_count = $product->get_review_count();
$average_rating = $product->get_average_rating();
// Displaying a WooCommerce On Sale Product:
$onsale = $product->is_on_sale();
//Displaying Simple Product or Variable Product
$simple_product = $product->is_type( 'simple' );
$variable_product = $product->is_type( 'variable' );
//Displaying Product Regular Price and Sale Price
if($simple_product):
if($sale_price):
$list.='
<div class="product-price">
<span class="price">'.$price_currency.' '.$sale_price.' </span>
<span class="price-before-discount">'.$price_currency.' '.$regular_price.'</span>
</div>
';
else:
$list.='
<div class="product-price">
<span class="price">'.$price_currency.' '.$regular_price.' </span>
</div>
';
endif;
endif;
// Displaying Product Add-to-cart Button:
if($simple_product):
$list.='<a data-product_id="'.$idd.'" class="button product_type_simple add_to_cart_button ajax_add_to_cart" href="?post_type=product&amp;add-to-cart='.$idd.'"><i class="fa fa-shopping-cart"></i> &nbsp; Add Cart</a> ';
elseif($variable_product):
$list.='<a data-product_id="'.$idd.'" class="button product_type_variable add_to_cart_button" href="?product='.get_the_title().'"><i class="fa fa-shopping-cart"></i> &nbsp; Select OPT</a> ';
endif;
Creating an Add to Cart button :
<form class="add-cart" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="'.esc_attr($product->id).'">
<button type="submit"> <i class="fa fa-shopping-cart"></i> '.$product->single_add_to_cart_text().' </button>
<div class="quantity">
<input type="number" step="1" min="1" max="9" name="quantity" value="1" title="" class="input-text qty text" size="4">
</div>
</form>
/*====================================================================
Breadcumbs custom style(div or ul)
======================================================================*/
add_filter( 'woocommerce_breadcrumb_defaults', 'flipmart_woocommerce_breadcrumbs' );
function flipmart_woocommerce_breadcrumbs() {
return array(
'delimiter' => ' / ',
'wrap_before' => '<div class="breadcrumb-inner"><ul class="list-inline list-unstyled">',
'wrap_after' => '</ul></div>',
'before' => '',
'after' => '',
'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
);
}
/*====================================================================
Change number or products per row to 3
======================================================================*/
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
return 4; // 3 products per row
}
}
/*====================================================================
woocommerce_template_loop_add_to_cart
======================================================================*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add Cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select PDT', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
/*====================================================================
Product item show per-page
======================================================================*/
function product_show_per_page() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
echo '<div class="woocommerce-perpage">';
echo '<span><b>Per Page:</b></span>';
echo '<select onchange="if (this.value) window.location.href=this.value">';
$orderby_options = array(
'6' => '6 items',
'16' => '16 items',
'24' => '24 items',
'32' => '32 items',
'-1' => 'All items'
);
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
}
echo '</select>';
echo '</div>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', $per_page );
}
}
<?php product_show_per_page(); ?>
/*====================================================================
Custom Catalog Ordering
======================================================================*/
//WooCommerce Default Catelog ordering remove
remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering',30,0);
add_filter( 'woocommerce_catalog_orderby', 'woocommerce_custom_catalog_orderby' );
function woocommerce_custom_catalog_orderby( $sortby ) {
echo '<span class="orderby" style="padding:7px 0px;font-weight:700;">Sort By:</span>';
$sortby['menu_order'] ='Default sorting';
$sortby['date'] ='Newness';
$sortby['popularity'] ='Popularity';
$sortby['price'] = 'Price: low to high';
$sortby['price-desc'] = 'Price: high to low';
unset($sortby['rating']);
return $sortby;
}
<?php woocommerce_catalog_ordering(); ?>
/*====================================================================
Custom Pagination
======================================================================*/
//WooCommerce Default pagination remove
remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination',10,0);
function woocommerce_custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'type' => 'array',
'prev_next' => true,
'prev_text' => __('&nbsp;'.'<i class="fa fa-angle-left"></i>'.'&nbsp;'),
'next_text' => __('&nbsp;'.'<i class="fa fa-angle-right"></i>'.'&nbsp;'),
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<div class="pagination-container floatright"><ul class="list-inline list-unstyled">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul></div>';
}
}
<?php woocommerce_custom_pagination(); ?>
/*====================================================================
Stock Product abailability
======================================================================*/
function product_availbility(){
global $product;
$numleft = $product->get_stock_quantity();
if($numleft==0) {
// out of stock
echo "<p style='font-style:italic;color:#DD4E42;margin-bottom:5px;padding:10px 0;'><b>Sorry, Out of stock.</b></p>";
}
else if($numleft==1) {
echo '<span class="stock_in" style="margin-bottom:5px;color:orange;"><b style="color:#555;">Availability :</b> &nbsp;'." Only ".$numleft ." item left.".'</span>';
}
else if($numleft<10){
echo '<span class="stock_in" style="margin-bottom:5px;"><b style="color:#555;">Availability :</b> &nbsp;'.$numleft ." items in stock.".'</span>';
}
else {
echo '<span class="stock_in" style="margin-bottom:5px;"><b style="color:#555;">Availability :</b> &nbsp;'.$numleft ." items left.".'</span>';
}
}
add_action('woocommerce_single_product_summary', 'product_availbility', 25);
/*====================================================================
Add cart total Ajax load (function.php)
======================================================================*/
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?> <a class="cart-customlocation" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a><?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
//index.php
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
/*====================================================================
Product/SKU ID
======================================================================*/
// default sku id (single-product/meta.php)
function add_custom_sku(){
global $product;
$sku = $product->get_sku();
$sku_num = ($sku ? $sku : 'N/A');
?>
<div class="custom-sku">
<?php _e('<b>Product ID</b> : ', 'eshop'); echo $sku_num; ?>
</div>
<?php
}
add_action('woocommerce_single_product_summary', 'add_custom_sku',6);
/*====================================================================
single product page image er bottom gallrey image remove
======================================================================*/
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
add_action('woocommerce_product_thumbnails','carousel_single_product');
function carousel_single_product(){
global $post, $product;
$attachment_ids = $product->get_gallery_image_ids();
if($attachment_ids):
?>
<div class="product-item-holder size-big single-product-gallery small-gallery">
<div id="owl-single-product">
<?php
$i=1;
foreach($attachment_ids as $attachment_id):
$image_link = wp_get_attachment_url($attachment_id);
?>
<div class="single-product-gallery-item" id="slide-<?php echo $i; ?>">
<a data-lightbox="image" data-title="Gallery" href="<?php echo $image_link; ?>" data-echo="<?php echo $image_link; ?>">
<img class="img-responsive" alt="" src="<?php echo $image_link; ?>" />
</a>
</div><!-- /.single-product-gallery-item -->
<?php $i++; endforeach; ?>
</div><!-- /.single-product-slider -->
<div class="single-product-gallery-thumbs gallery-thumbs">
<div id="owl-single-product-thumbnails">
<?php
foreach($attachment_ids as $attachment_id):
$image_link = wp_get_attachment_url($attachment_id);
?>
<div class="item">
<a data-lightbox="picture" data-title="Product Image" class="horizontal-thumb" data-slide="<?php echo $image_link; ?>" href="<?php echo $image_link; ?>">
<img class="img-responsive" width="85" alt="" src="<?php echo $image_link; ?>" />
</a>
</div>
<?php endforeach; ?>
</div><!-- /#owl-single-product-thumbnails -->
</div><!-- /.gallery-thumbs -->
</div>
<?php endif; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment