Display coupon description woocommerce - wordpress

I am trying to display coupon description once the coupon is applied (10%) in cart page.
To display Total I am using $woocommerce->cart->cart_contents_total
How do I display coupon description?

As you have not mentioned where do you want to have coupon description, I have printed it just before Cart Total.
If you want to have it on different place, you can modify action. You can find it from here.
Code:
add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
$my_coupon = $woocommerce->cart->get_coupons() ;
foreach($my_coupon as $coupon){
if ( $post = get_post( $coupon->id ) ) {
if ( !empty( $post->post_excerpt ) ) {
echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
}
}
}
}
Let me know if you have any doubts.

This plugin create shortcode to display coupons info. One of them is [coupon_description].
https://wordpress.org/plugins/woocommerce-coupon-shortcodes/

Related

How to conditionally use a shortcode on WooCommerce single product pages

How is it possible to hide a shortcode conditionally on WooCommerce single product pages if stock is empty?
For example if a product is out of stock:
The shortcode [scale-prices] should disappear
(A green traffic light picture changes to a yellow traffic light picture)
Your question is quite broad. But you could add a CSS body class to the product page if a product (or a variation) is out of stock. You can then use that to manipulate your page for instance via CSS or JavaScript.
The following code snippet will add out-of-stock as a body class for a simple out of stock product, and a {variation-id}-out-of-stock body class for each variation that is out of stock:
add_filter( 'body_class', 'add_class_if_product_is_out_of_stock', 10, 1 );
function add_class_if_product_is_out_of_stock( $classes ) {
if ( is_product() ) {
global $post;
if ( $product = wc_get_product( $post->ID ) ) {
if ( $product->is_type( 'variable' ) ) {
$children = $product->get_children();
foreach ( $children as $child_id ) {
if ( $product = wc_get_product( $child_id ) ) {
if ( $product->is_in_stock() == false ) {
$classes[] = sprintf( '%s-out-of-stock', $child_id );
}
}
}
} elseif ( $product->is_in_stock() == false ) {
$classes[] = 'out-of-stock';
}
}
}
return $classes;
}
This snippet should be added to the functions.php of your child theme or via a plugin like Code Snippets.
You can use any of these single product page hooks and add a condition of your need.
https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
Like you can use woocommerce_before_single_product and check for product availablity and based on that result you can use add_shortcode, remove_shortcode. Also you can add script using wp_footer script if required.
You can share further details/scenario for exact solution.

How to check if a coupon has granted free shipping in Woocommerce?

ORIGINAL QUESTION:
We need to know if a coupon has specifically granted free shipping.
In my test scenario I have two coupons applied to the cart, one with free shipping and one without. The var_dump clearly shows one coupon like this:
["free_shipping"]=>
bool(true)
However, when using
echo "<p>Free shipping: ".(int)$coupon->free_shipping."</p>";
It always displays 0.
I can't figure out what I am doing wrong.
Here's the code:
add_action('woocommerce_before_cart_totals', 'check_free_shipping_on_coupon');
function check_free_shipping_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
$my_coupon = $woocommerce->cart->get_coupons() ;
foreach($my_coupon as $coupon){
if ( $post = get_post( $coupon->id ) ) {
echo "<p>ID: ".$coupon->id . "</p>";
echo "<p>Code: ".$coupon->code."</p>";
echo "<p>Amount: ".$coupon->amount."</p>";
if ( !empty( $post->post_excerpt ) ) {
echo "<p>Excerpt: ".$post->post_excerpt."</p>";
}
echo "<p>Free shipping: ".(int)$coupon->free_shipping."</p>";
if ( (int)$coupon->free_shipping == '1'){//I want to do stuff here;
}
//Dump everything for reference
echo '<pre>' , var_dump($coupon) , '</pre>';
}
}
}
}
UPDATE:
I found another question here: Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce that had a code snippet that solved my question, but in a very different way. I am still curious why my original code did not work.
Working code:
//Check if a coupon has granted free shipping
add_action('woocommerce_before_cart_totals', 'rnr_check_free_shipping_on_coupon');
function rnr_check_free_shipping_on_coupon() {
$applied_coupons = WC()->cart->get_applied_coupons();
foreach( $applied_coupons as $coupon_code ){
$coupon = new WC_Coupon($coupon_code);
if($coupon->get_free_shipping()){
echo 'You have a free shipping coupon!';
}
}
}

Wordpress variable, user created, woocommerce product

Basically I am interested in using woocommerce to sell a product . This product is a Print Order of a external printing service that I have implemented in a brand new plugin.
What I want now is after the order, is to be able to put that "order" in the buy cart, and buy it normally as just another woocommerce product.
The product has to be created on the fly, manually by a way of some function that I can use to create a product during a certain workflow point.
Can you help me to find a solution?
Using woocommerce or not!
What i understand from your requirement/comments is that you want to be able to dynamically create a product, which is bad idea. But here is my suggestion.
Lets say you have a simple product called "Print Job" with a price of $1 and with an id of 10. And i am supposing that your external printing service would send back a response with the order and the price of printing, lets say $64.
Once you recieve the response you can simply call add_product_to_cart(10), this will automatically add the print job product to your cart.
/************* functions.php ***************/
function add_product_to_cart($product_id) {
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
You also need to add this function in your functions.php, this function will override the price of the product i.e "Print Job" with the actual price of $64.
/******************* Functions.php ****************/
add_action( 'woocommerce_before_calculate_totals', 'override_printing_price' );
function override_printing_price( $cart_object ) {
$custom_price = 64;
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}

Woocommerce product link url for simple products

Using the shortcode [add_to_cart_url sku="#"] will generate a link that will take the shopper to the single product page ONLY IF the product has variations.
What if I want a simple product (a product without variations) to link to its single product page instead of adding it to the cart?
IMO, this seems overlooked by the WooCommerce team.
All I want is a generated link to an item's single product page based on its SKU. Something as simple as implementing [product_url sku="#"] would be great.
Otherwise, searching Google has revealed no success.
Is this what you want ?
Below code is for the shortcode of Product URL only and you can use it with like :
[product_url sku="SKUofProduct"] or with ID [product_url id="productID"]
add_shortcode( 'product_url', 'rohils_product_url_function' );
function rohils_product_url_function($atts){
global $wpdb;
if ( empty( $atts ) ) {
return '';
}
if ( isset( $atts['id'] ) ) {
$product_data = get_post( $atts['id'] );
} elseif ( isset( $atts['sku'] ) ) {
$product_id = wc_get_product_id_by_sku( $atts['sku'] );
$product_data = get_post( $product_id );
} else {
return '';
}
if ( 'product' !== $product_data->post_type ) {
return '';
}
$_product = wc_get_product( $product_data );
return esc_url( get_post_permalink($_product->id) );
}

Woocommerce shopping cart column

How add custom column to woocommerce shopping cart and then add info of some input from this column to order, checkout page and to email?
Actually i need add friends list from buddypress to each product row(price must depends on how many friends checked).
Here i found suggestion, but it`s partly helpfull WooCommerce: Add input field to every item in cart
wp community also keep silence
http://wordpress.org/support/topic/woocommerce-custom-column-in-cart?replies=1
what i do - its just add list of avalaible friends and no idea how can I save data on update cart or proceed.
if ( bp_has_members( 'user_id=' . bp_loggedin_user_id() ) ){
function ggUserFrom(){
$arrUsers = array();
while ( bp_members() ){
bp_the_member();
$arrUsers[ bp_get_member_user_nicename() ] = bp_get_member_user_nicename();
}
return $arrUsers;
}
echo "<div class='friends-holder'>";
foreach ( ggUserFrom() as $friend ){
echo '<p><input type="checkbox" name="cart['.$cart_item_key.'][friendsfromcart]" value="'.$friend.'">
<span>'.$friend.'</span></p>';
}
echo "</div>";
}
Im seek ANY info about this question.
Here, since user may choose multiple checkboxes, it may contain multiple values. Therefore, we have used "serialize" function.
for example,
add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
function wdm_get_cart_items_from_session($item,$values,$key)
{
$item['custom_field_name'] = isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : '';
return $item;
}
}
While you are adding Order meta data, you can fetch individual values and add corresponding keys as follows,
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values)
{
$user_custom_values = unserialize($values['friendsfromcart']);
if(count($user_custom_values) > 0)
{
foreach($user_custom_values as $single_value)
{
wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);
}
}
}
since order meta will be sent in E - mail.
Thank for reply and - yes, variable isset in checkout page, but his empty in
var_dump($_POST)...
...[custom_field_name] =>
...
and ofc is empty in email.
Perhaps i incorrect send it ?
name="friendsfromcart"
or send values its a array and must send:
name="friendsfromcart[]"
or
name="[friendsfromcart]"
or need session key
name="cart['.$cart_item_key.'][friendsfromcart]" ?
It works, array in cart but something wrong with unserialize:
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['friendsfromcart'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['friendsfromcart'] = $cart_totals[ $cart_item_key ]['friendsfromcart'];
}
}
}
}
}
add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key){
$item['friendsfromcart'] = isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : '';
return $item;
}
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values){
$user_custom_values = unserialize($values['friendsfromcart']);
if(count($user_custom_values) > 0){
foreach($user_custom_values as $single_value){
wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);
}
}
}

Resources