Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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: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: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>';
}
}
?>