Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
mahdi-alavi / gist:bb7133e68209d731e6c1
Last active June 15, 2017 20:21
Woocommerce Display Product Attribute Variations
<?php
global $product;
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ) {
$terms = wp_get_post_terms( $product->id, $attribute['name'], 'all' );
foreach ( $terms as $term ) {
echo $term->name .'<br>';
}
}
?>
@mahdi-alavi
mahdi-alavi / gist:a81ae57a18d4927dcfeb
Last active November 15, 2022 08:13
Woocommerce Converts Variations in to Radio Buttons
/*
* create in your theme path: your-theme/js/add-to-cart-variation.min.js
*/
jQuery(document).ready(function(d){d("form.variations_form").on("click",".reset_variations",function(f){return false}).on("change",".variations input:radio",function(f){$variation_form=d(this).closest("form.variations_form");$variation_form.find("input[name=variation_id]").val("").change();$variation_form.trigger("woocommerce_variation_radio_change").trigger("check_variations",["",false]);d(this).blur();if(d().uniform&&d.isFunction(d.uniform.update)){d.uniform.update()}}).on("focusin",".variations input:radio",function(f){$variation_form=d(this).closest("form.variations_form");$variation_form.trigger("woocommerce_variation_radio_focusin").trigger("check_variations",[d(this).attr("name"),true])}).on("check_variations",function(f,g,r){var l=true;var o=false;var p=false;var q={};var k=d(this);var n=k.find(".reset_variations");k.find(".variations input:radio:checked").each(function(){if(d(this).val().length==0){l=false}else{o=true}if
@mahdi-alavi
mahdi-alavi / gist:84698c40706874663cd1
Last active September 22, 2016 19:29
Woocommerce Display all Product Variations Values
/*
* for see all value use var_dump($product_variations);
*/
$product_variations = $product->get_available_variations();
foreach ( $product_variations as $product_variation ) {
echo $product_variation['variation_id'];
echo $product_variation['display_regular_price'];
echo $product_variation['variation_description'];
}
@mahdi-alavi
mahdi-alavi / gist:9d7752572fed9cedc8b10c6876d1e86a
Last active July 24, 2022 19:12
replace WooCommerce add-to-cart button with download link when product is downloadable & free
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'itl_woocommerce_template_single_add_to_cart', 30 );
/*
* replace WooCommerce add-to-cart button with download link when product is downloadable & free
*/
function itl_woocommerce_template_single_add_to_cart() {
global $product;
if ( $product->is_downloadable('yes') ) {
@mahdi-alavi
mahdi-alavi / gist:d153c63e3099b7c083f8bcfc51ba6374
Created September 25, 2016 15:55
WooCommerce Add Special Class to Free Products
function itl_add_class_free_products( $price ) {
$price = '<span class="amount free-product">' . __( 'Free!', 'woocommerce' ) . '</span>';
return $price;
}
add_filter( 'woocommerce_variable_free_price_html', 'itl_add_class_free_products' );
add_filter( 'woocommerce_free_price_html', 'itl_add_class_free_products' );
add_filter( 'woocommerce_variation_free_price_html', 'itl_add_class_free_products' );
@mahdi-alavi
mahdi-alavi / gist:62a9b3dbfea7666d49e515fe02633a2e
Created October 19, 2016 07:42
Add a message to buyers in single product page
/*
* Add a message to buyers in single product page
*/
function itl_display_note_buyers() {
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->email, $current_user->ID, $product->id ) ) {
_e( 'your message', 'text-domain' );
@mahdi-alavi
mahdi-alavi / gist:1be04ad4247c7f0e4afb51284c627dfc
Created April 1, 2017 04:33
Woocommerce: limit customer to only one category per transaction
// original source http://stackoverflow.com/a/35072168
function itl_is_product_the_same_cat( $valid, $product_id, $quantity ) {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); // get the current items
@mahdi-alavi
mahdi-alavi / gist:6bcb6a24805107f2d127fea083050dea
Last active June 28, 2017 06:00
Piwik & WooCommerce - tracking code and woocommerce order tracking
// in line 61 insert your piwik url
// source code: wordpress.org/plugins/woocommerce-piwik-integration/
/* Get encoded categories by product.
=========================================================================== */
function getEncodedCategoriesByProduct( $product ) {
$categories = get_the_terms( $product->get_id(), 'product_cat' );
if ( ! $categories ) {
$categories = array();
@mahdi-alavi
mahdi-alavi / gist:b50e89e9d1564e581a7b228c91872428
Created July 3, 2017 12:54
WP_Query orderby include Argument for tax_query (taxonomy)
// Let’s presume we have a ‘product’ post type, with a ‘names’ taxonomy.
/* The posts_orderby filter.
=========================================================================== */
function itl_edit_posts_orderby( $orderby_statement, $wp_query ) {
if ( isset( $wp_query->query['orderby'] ) && 'include' == $wp_query->query['orderby'] ) {
$include = implode( ',', array_map( 'absint', $wp_query->query['include'] ) );
$orderby_statement = "FIELD( term_taxonomy_id, $include )";
}
@mahdi-alavi
mahdi-alavi / gist:d2c89d0c62156688d0c2459640220c60
Created January 18, 2018 18:43
Add Custom Fee to Woocommerce Cart for Physical Products
/* Add Custom Fee to Woocommerce Cart for Physical Products.
=========================================================================== */
function itl_woocommerce_custom_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$is_physical = false;