WooCommerce Smart Coupon generated from which Item? - wordpress

I am writing a business extension (pdf printing) for the smart coupons.
Now I need a relation from the generated coupon (of the order) and the order item.
For example, which order item has generated the coupon?
Is there a way to get the order item_id from the coupon?
Is use this code to get the coupons:
$coupons = get_post_meta($order_id, 'sc_coupon_receiver_details', true));
Thank you very much

I have found a solution, but i had to put the code inside "woocommerce-smart-coupons.php" file.
Line 379: Extend the Filter Variables
add_filter( 'generate_smart_coupon_action', array( $this, 'generate_smart_coupon_action' ), 1, 10 );
Line 4783: Extend the Variable with the $item_id
if( $this->is_coupon_amount_pick_from_product_price( array( $coupon_title ) ) ) {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $sc_called_credit_details[$item_id] . ':' . $item_id;
} else {
$email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $coupon->amount . ':' . $item_id;
}
Line 4816: Get the $item_id from the details and refer it to the "generate_smart_coupon" method
foreach ( $email_to_credit[$email_id] as $coupon_credit => $qty ) {
$coupon_details = explode( ':', $coupon_credit );
$coupon_title = get_the_title( $coupon_details[0] );
$coupon = new WC_Coupon( $coupon_title );
$credit_amount = $coupon_details[1];
$item_id = $coupon_details[2];
$message_index = array_search( $email_id, $email[$coupon->id], true );
if ( $message_index !== false && isset( $receivers_messages[$coupon->id][$message_index] ) && !empty( $receivers_messages[$coupon->id][$message_index] ) ) {
$message_from_sender = $receivers_messages[$coupon->id][$message_index];
} else {
$message_from_sender = '';
}
for ( $i = 0; $i < $qty; $i++ ) {
if ( $coupon->type != 'smart_coupon' ) continue; // only process smart_coupon here, rest coupon will be processed by function update_coupon
$this->generate_smart_coupon( $email_id, $credit_amount, $order_id, $coupon, 'smart_coupon', $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
$smart_coupon_codes = array();
}
}
Line 5194: Change the Method and extend the variables
public function generate_smart_coupon( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
return apply_filters( 'generate_smart_coupon_action', $email, $amount, $order_id, $coupon, $discount_type, $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
}
Line 5212: Extend the filter method to
public function generate_smart_coupon_action( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
Line 5307: Store the $item_id in the post_meta
update_post_meta( $smart_coupon_id, 'apply_before_tax', $apply_before_tax );
update_post_meta( $smart_coupon_id, 'free_shipping', $free_shipping );
update_post_meta( $smart_coupon_id, 'product_categories', $product_categories );
update_post_meta( $smart_coupon_id, 'exclude_product_categories', $exclude_product_categories );
update_post_meta( $smart_coupon_id, 'generated_from_order_id', $order_id );
update_post_meta( $smart_coupon_id, 'generated_from_item_id', $item_id );

Related

Woocommerce Variable Product Prices prefix

Currently I'm using a this code/plugin to show prefix and lowest price the shop:
function show_only_lowest_prices_in_woocommerce_variable_products_load_plugin_textdomain() {
load_plugin_textdomain( 'show-only-lowest-prices-in-woocommerce-variable-products', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'show_only_lowest_prices_in_woocommerce_variable_products_load_plugin_textdomain' );
//Simple products
function wc_wc20_variation_price_format( $price, $product ) {
// Main prices
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
//Grouped products
// Show product prices in WooCommerce 2.0 format
add_filter( 'woocommerce_grouped_price_html', 'wc_wc20_grouped_price_format', 10, 2 );
function wc_wc20_grouped_price_format( $price, $product ) {
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
$get_price_method = 'get_price_' . $tax_display_mode . 'uding_tax';
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if ( $min_price == $max_price ) {
$display_price = wc_price( $product->$get_price_method( 1, $min_price ) );
} else {
$from = wc_price( $product->$get_price_method( 1, $min_price ) );
$display_price = sprintf( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), $from );
}
return $display_price;
}
But I need to change it ot show the both prices (min and max) with prefix like this
From 10 to 25 lv
I tried to edited but in this code i cant get the max price to show to put prefix.
Try the following shorter function that use the dedicated hook for that:
add_filter( 'woocommerce_format_price_range', 'format_price_range_prefix', 20, 3 );
function format_price_range_prefix( $price, $from, $to ) {
$price = sprintf( _x( 'From %1$s to %2$s %3$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to, __('lv', 'woocommerce') );
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Show price next to the variations in WooCommerce

I am using the below code to display price next to the variations in WooCommerce. It works great if more than 2 variations but if only 2 variations it displays only the first variations price for both variations.
Any help with this would be great.
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( is_product() ) {
if ( $product instanceof WC_Product && $product->is_type( 'variable' ) ) {
foreach ( $product->get_available_variations() as $variation ) {
//$product_variation_id = $variation['variation_id'];
foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE slug = '$variation_attribute_value'" );
if ( $term != $variation_attribute_value && $variation_attribute_value != $result[0] ) continue;
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term . ' - (' . $product_price . ')';
}
}
}
}
return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
This code is tested and works with multiple dropdowns and multiple attributes and product specific attributes like, attribute1 | attribute2 | attribute3.
// Add the prices next to the variations in WooCommerce
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_names' );
function display_price_in_variation_option_names( $term_name ) {
global $wpdb, $product;
if ( is_product() && $product instanceof WC_Product && $product->is_type( 'variable' ) ) {
foreach ( $product->get_available_variations() as $variation ) {
foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE slug = '$variation_attribute_value'" );
$taxonomy = str_replace( 'attribute_', '', $variation_attribute_name );
$term = get_term_by( 'slug', $variation_attribute_value, $taxonomy );
if ( $term_name == $variation_attribute_value ) {
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term_name . ' - (' . $product_price . ')';
} else if( is_object($term) && $term->name == $term_name ) {
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term_name . ' - (' . $product_price . ')';
}
}
}
}
return $term_name;
}

dont allow PO BOX shipping woocommerce not working

add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
}
}
i am getting:
fatal error :call to undefined add_error
when i am pasting on my function .php
add_error() has been renamed to wc_add_notice():
$woocommerce->wc_add_notice( "Sorry, we don't ship to PO BOX addresses." );
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
$address = ( isset( $posted['shipping_address_1'] ) ) ?
$posted['shipping_address_1'] : $posted['billing_address_1'];
$postcode = ( isset( $posted['shipping_postcode'] ) ) ?
$posted['shipping_postcode'] : $posted['billing_postcode'];
$replace = array(" ", ".", ",");
$address = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );
if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
$notice = sprintf( __( '%1$sSorry, we dont ship to PO BOX addresses..' , 'error' ) , '<strong>' , '</strong>' );
if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
$woocommerce->add_error( $notice );
} else {
wc_add_notice( $notice, 'error' );
}
}
}
<?php
add_filter('woocommerce_package_rates',
'shomaris_hide_fedex_for_po_box_shipment',
10, 2);
function shomaris_hide_fedex_for_po_box_shipment($available_shipping_methods,
$package){
$shipping_method_to_hide = 'flat_rate:4';
// $shipping_method_to_hides = 'flat_rate:5';
global $woocommerce;
$address = ( !empty( $woocommerce->customer->get_shipping_address_1() ) ) ?
$woocommerce->customer->get_shipping_address_1() : $woocommerce->customer-
>get_billing_address_1();
$postcode = ( !empty( $woocommerce->customer->get_shipping_postcode() ) ) ?
$woocommerce->customer->get_shipping_postcode() : $woocommerce->customer-
>get_billing_postcode();
$replace = array(" ", ".", ",");
$address2 = strtolower( str_replace( $replace, '', $address ) );
if ( strstr( $address2, 'pobox' ) ) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $shipping_method_to_hide,
$shipping_method_to_hides ) !== false ) {
unset($available_shipping_methods[$shipping_method]);
}
}
}
return $available_shipping_methods;
}
?>

add rewrite rule custom post type

I've been working on this for 4 days now and can't figure out what I'm missing. I have searched and tried everything that comes up for wp rewriting.
It works but doesn't save. So if I go to quick edit and save, click view it works. If I refresh and hover over view %property_type% is missing and doesn't work.
Any Idea what I'm missing?
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => str_replace( basename( home_url() ), '', 'investment-property' ) . '/%property_type%',
'with_front' => false
),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => true,
'comment_status'=> 'closed',
'exclude_from_search' =>false,
'supports' => array('title', 'editor', 'thumbnail'),
);
register_post_type( 'property', $args );
I also have this...
function my_flush_rules() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(investment-property/%property_type%/)/(.+)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
...and this
function icn_property_type_link( $permalink, $post, $leavename ) {
global $wp_query, $wpsc_page_titles, $wp_rewrite, $wp_current_filter;
$rewritecode = array(
'%property_type%',
$leavename ? '' : '%postname%',
);
if ( is_object( $post ) ) {
// In wordpress 2.9 we got a post object
$post_id = $post->ID;
} else {
// In wordpress 3.0 we get a post ID
$post_id = $post;
$post = get_post( $post_id );
}
// Only applies to ICN properties, don't stop on permalinks of other CPTs
if ($post->post_type != 'property')
return $permalink;
$permalink_structure = get_option( 'permalink_structure' );
// This may become customize-able later
$our_permalink_structure = str_replace( basename( home_url() ), '', 'investment-property' ) . "/%property_type%/%postname%/";
// Mostly the same conditions used for posts, but restricted to items with a post type of "wpsc-product "
if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
$property_categories = get_the_terms($post_id, 'property_type' );
$property_category_slugs = array( );
foreach ( $property_categories as $property_category ) {
if($property_category->slug == 'featured'){continue;}
$property_category_slugs[] = $property_category->slug;
}
// If the property is associated with multiple categories, determine which one to pick
if ( count( $property_categories ) == 0 || $property_categories == '') {
$category_slug = 'real-estate-for-sale';
} elseif ( count( $property_categories ) > 1 ) {
if ( (isset( $wp_query->query_vars['property'] ) && $wp_query->query_vars['property'] != null) && in_array( $wp_query->query_vars['property'], $property_category_slugs ) ) {
$property_category = $wp_query->query_vars['property'];
} else {
$link = $property_categories[0]->slug;
if ( ! in_array( 'wp_head', $wp_current_filter) && isset( $wp_query->query_vars['property_type'] ) ) {
$current_cat = $wp_query->query_vars['property_type'];
if ( in_array( $current_cat, $property_category_slugs ) ){
$link = $current_cat;
}
}
$property_category = $link;
}
$category_slug = $property_category;
} else {
// If the property is associated with only one category, we only have one choice
if ( !isset( $property_categories[0] ) )
$property_categories[0] = '';
$property_category = $property_categories[0];
if ( !is_object( $property_category ) )
$property_category = new stdClass();
if ( !isset( $property_category->slug ) )
$property_category->slug = null;
$category_slug = $property_category->slug;
}
$post_name = $post->post_name;
if ( get_option( 'property_type_hierarchical_url', 0 ) ) {
$selected_term = get_term_by( 'slug', $category_slug, 'property_type' );
if ( is_object( $selected_term ) ) {
$term_chain = array( $selected_term->slug );
while ( $selected_term->parent ) {
$selected_term = get_term( $selected_term->parent, 'property_type' );
array_unshift( $term_chain, $selected_term->slug );
}
$category_slug = implode( '/', $term_chain );
}
}
if( isset( $category_slug ) && empty( $category_slug ) )
$category_slug = 'property_type';
$category_slug = apply_filters( 'property_type_permalink_cat_slug', $category_slug, $post_id );
$rewritereplace = array(
$category_slug,
$post_name
);
$permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
$permalink = user_trailingslashit( $permalink, 'single' );
$permalink = home_url( $permalink );
}
return apply_filters( 'property_permalink', $permalink, $post_id );
}
add_filter( 'post_type_link', 'icn_property_type_link', 1, 3 );

Adding weighted tags in wordpress

Is there a way I can add weighted tags to wordpress? I need to customize the tag cloud widget to show the most used tags with different colors
I've stumbled upon this as well. Here's a modified function from the original tag cloud widget:
<?php
function my_colorful_tag_cloud( $args = array() ) {
$defaults = array(
'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
);
$args = wp_parse_args( $args, $defaults );
$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
if ( empty( $tags ) || is_wp_error( $tags ) )
return;
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), $tag->taxonomy );
if ( is_wp_error( $link ) )
return false;
$tags[ $key ]->link = $link;
$tags[ $key ]->id = $tag->term_id;
}
$defaults = array(
'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
'topic_count_text_callback' => 'default_topic_count_text',
'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
);
if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
$body = 'return sprintf (
_n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
number_format_i18n( $count ));';
$args['topic_count_text_callback'] = create_function('$count', $body);
}
$args = wp_parse_args( $args, $defaults );
extract( $args );
if ( empty( $tags ) )
return;
$tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
$tags = $tags_sorted;
unset($tags_sorted);
} else {
if ( 'RAND' == $order ) {
shuffle($tags);
} else {
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
if ( 'name' == $orderby )
uasort( $tags, '_wp_object_name_sort_cb' );
else
uasort( $tags, '_wp_object_count_sort_cb' );
if ( 'DESC' == $order )
$tags = array_reverse( $tags, true );
}
}
if ( $number > 0 )
$tags = array_slice($tags, 0, $number);
$counts = array();
$real_counts = array(); // For the alt tag
foreach ( (array) $tags as $key => $tag ) {
$real_counts[ $key ] = $tag->count;
$counts[ $key ] = $topic_count_scale_callback($tag->count);
}
$min_count = min( $counts );
$spread = max( $counts ) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread < 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
$a = array();
$colors = 6;
foreach ( $tags as $key => $tag ) {
$count = $counts[ $key ];
$real_count = $real_counts[ $key ];
$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
$tag_name = $tags[ $key ]->name;
$class = 'color-' . ( round( ( $smallest + ( ( $count - $min_count ) * $font_step ) ) - ( $smallest - 1 ) ) );
$a[] = "<a href='$tag_link' class='tag-link-$tag_id $class' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
. "$unit;'>$tag_name</a>";
}
$return = join( $separator, $a );
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
}
This code (by default)will generate a tag cloud with 13 possible classes for each tag(since the defaults are min. size=10 and max.size = 22). In order to change the color for each tag weight, simply add those rules to your CSS:
.colorful_tag_cloud a.color-x { color: {color} }
where "x" is a number from 1 to 13
Hope this helps.

Resources