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!';
}
}
}
Related
Hello I am trying to create custom payment links for various payment methods. I would like the link to be generated on WooCommerce based on a cart or order total at the checkout page. Here is what I have been trying in my functions php file.
function customhaywoo(){
global $woocommerce, $total, $amount ;
$woocommerce->cart->get_cart();
$total = $woocommerce->cart->get_total();
$amount = $woocommerce->cart->total;
if ( ! $amount ) {
return;
}
extract(shortcode_atts(array(
"href" => 'https://venmo.com/hayden-55?txn=pay&amount=',
esc_attr( wp_kses_post( $amount ) ),
), $atts));
return ''.$content.'';
}
add_shortcode ('haywoo','customhaywoo');
I have tried other combinations as well however as you can probably tell I have absolutely no experience and have no idea what I am doing. I got the idea of what I should include in the code from "checkout with Venmo" by the African boss plugin. I would just use the plugin however I need this for a few other Payment methods. For many of the Payment methods you can just add the number amount at the end of the url and it will automatically have the amount inserted into the payment method. Basically I somehow need to get the cart or order total to appear at the end of a link without the currency symbol.
Edit- I tried this for displaying on the thank you page. This just shows up as nothing at all. not even the [haywoo]. Any help on this would be awesome!
function customhaywoo()
{
$order = new WC_Order( $order_id );
$total = $order->get_total();
$order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_total()) );
if (!$order_total) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-595?txn=pay&amount='.$order_total;
$payment_text = __('Click here to pay '.$order_total, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents; ''.$content.'';
}
add_shortcode('haywoo', 'customhaywoo');
If you want the shortcode to output the payment link with the total amount of cart then try out below edited code.
function customhaywoo()
{
global $woocommerce, $total, $amount;
$woocommerce->cart->get_cart();
$total_amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
if (!$total_amount) {
return;
}
ob_start();
$pay_link = 'https://venmo.com/hayden-55?txn=pay&amount='.$total_amount;
$payment_text = __('Click here to pay '.$total_amount, 'text_domain');
echo '' . $payment_text . '';
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_shortcode('haywoo', 'customhaywoo');
I Hope it helps.
I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.
I want to show a "free delivery" badge at every product with a price above 50$.
It should be visible on the product page and in the loop.
The problem is, that there could be more than one price. If you think about variations and sales (or even variations with variants on sale).
So I need to check the type of the product and have to search for the cheapest price to calculate with.
At the moment I'm using the following code.
It works ok sometimes. But on products with no active stock management it produces a timeout on the product page and doesn't work on archives (shows no message).
Also it produces some notices about not using the ID directly.
I don't feel safe with that code... Is there a better way to archieve it?
I tried a lot of ways but I'm not sure if I think about every possibilities about price, sales, stock or product types?!
<?php add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery( ) {
if (is_product()):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
$sale_price = get_post_meta( $product->id, '_price', true);
$regular_price = get_post_meta( $product->id, '_regular_price', true);
if (empty($regular_price)){ //then this is a variable product
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variation= new WC_Product_Variation( $variation_id );
$regular_price = $variation ->regular_price;
$sale_price = $variation ->sale_price;
}
if ( $sale_price >= 50 && !empty( $regular_price ) ) :
echo 'free delivery!';
else :
echo 'NO free delivery!';
endif;
endif;
} ?>
As you are using a custom hook, is difficult to test it for real (the same way as you)… Now this revisited code should work in a much better way than yours (solving error notices):
add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery() {
// On single product pages and archives pages
if ( is_product() || is_shop() || is_product_category() || is_product_tag() ):
global $post, $product;
if ( ! $product->is_in_stock() ) return;
// variable products (get min prices)
if ( $product->is_type('variable') ) {
$sale_price = $product->get_variation_sale_price('min');
$regular_price = $product->get_variation_regular_price('min');
}
// Other products types
else {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
}
$price = $sale_price > 0 ? $sale_price : $regular_price;
if ( $price >= 50 ) {
echo __('free delivery!');
} else {
echo __('NO free delivery!');
}
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should work better.
I am using a function to add custom meta to products. I have used the following hooks for SHOW on Product Loop woocommerce_after_shop_loop_item and Product Single Page woocommerce_product_meta_end.
So, when applying to get the same results on CART PAGE product/item by using the hook
woocommerce_after_cart_item_name it doesn’t work with no results.
Why isn’t the hook woocommerce_after_cart_item_name working if it works with the other previous hooks mentioned?
This the code I am using. I just change the hook to make it to show in Product Loop and Product Single Page, need it to show on cart Products as well. I was just wondering why it doesn't work with cart item hook..
public function woocommerce_after_cart_item_name()
{
global $product;
if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
return;
$product_id = $product->get_id();
$season = get_post_meta( $product_id, 'season', true );
$car_type = get_post_meta( $product_id, 'car_type', true );
$tips_seasons = $this->ui_tips_season();
$tips_car_types = $this->ui_tips_car_types();
?>
<div class="tyre-details tyre-tips">
<?php if ( PluginOptions::value('shop_season') && $season ): ?>
<?php echo $tips_seasons[ strtolower($season) ]; ?>
<?php endif ?>
<?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
<?php echo $tips_car_types[ strtolower($car_type) ]; ?>
<?php endif ?>
</div>
<?php
}
It is from a plugin. I was just given this code from woocommerce support but i do not know how to complete it. He says to replace with my meta_keys in the code which I will post below here. Can you help me finish this? or tell me where I need to replace. My meta_keys are $season and $car-type but i don't know how to apply with the code provided by woocommerce.
// Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
$meta_key = 'PR CODE';
if ( isset($cart_item['add_size']) && isset($cart_item['add_size'] [$meta_key]) ) {
$item_data[] = array(
'key' => $meta_key,
'value' => $cart_item['add_size'][$meta_key],
);
}
return $item_data;
}
// Save cart item custom meta as order item meta data and display it everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
$item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}
I located the ClassName (class FeatureTyreTips) and added that to the code you provided. I entered this in functions.php and it said fatal error when loading the cart page. I also tried placing it in the cart.php with same results...and I tried in the same plugin file where this code came from originally. All 3 locations did not work...only difference was that it did not show fatal error when adding and activating your new code in the plugin file when loading cart page. Any ideas? grazie Vdgeatano
The "Fatal Error" is most likely due to the fact that "the non-static method cannot be called statically": PHP - Static methods
I have now corrected the code.
Considering that the class name that was missing in your code is FeatureTyreTips, the correct code to add some text after the product name is:
add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
// create an instance of the "PluginOptions" class
$PluginOptions = new PluginOptions();
if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
return;
}
$product = $cart_item['data'];
$season = get_post_meta( $product->get_id(), 'season', true );
$car_type = get_post_meta( $product->get_id(), 'car_type', true );
// create an instance of the "FeatureTyreTips" class
$FeatureTyreTips = new FeatureTyreTips();
$tips_seasons = $FeatureTyreTips->ui_tips_season();
$tips_car_types = $FeatureTyreTips->ui_tips_car_types();
if ( ! $PluginOptions->value('shop_season') || ! $season ) {
$season = '';
}
if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
$car_type = '';
}
$html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
echo $html;
}
The code has been tested (where possible) and works.It needs to be added to your theme's functions.php file or in your plugin.
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/