I am trying to make a shortcode that displays a url link for a payment gateway that includes the order total amount at the end of the url without the currency symbol. Here is the code I have been trying to use and edit to work. The actual shortcode is used in a payment gateway instructions that displays on the thank you page when that payment method is selected. This is put into my theme functions.php file however it displays as completely nothing, not even [haywoo]
function customhaywoo()
{
$order = new WC_Order( $order_id );
$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');
I am not very experienced in code and have no idea what i'm doing. Any help or advise or editing on the code would be very appreciated! Thank you.
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 wondered if anyone can help me?
I am using a Wordpress site with Woocommerce plugin.
I am using a piece of code to avoid adding to cart for non logged in customers which I found on this site, it works great apart from one issue. It doesn't work on the product page. When you click the add to cart button, it doesn't redirect to the custom login page like it does if you press the button on the category view page. Instead the page just refreshes.
I put the code in to the functions.php file. I've then tried putting it into a few other places but that hasn't worked. Could anyone help me with this and let me know if there is another location I should be putting the code in? Thanks in advance, I'd really appreciate the help!
Here's the link to the question and the code is below: WooCommerce Avoid add to cart for non logged user
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Firstly, your function hook for woocommerce_loop_add_to_cart_link is incorrect. You are using conditionally_change_loop_add_to_cart_link rather than quantity_inputs_for_woocommerce_loop_add_to_cart_link.
Secondly, your URL for the link is using the current product page ID, which is going to point you at the current product page URL and not another page.
Other than that, you had it mostly correct with woocommerce_add_to_cart_validation.
EDIT:
For product single pages, if you look at content-single-product.php in Woocommerce, the action woocommerce_template_single_add_to_cart seems to handle what the "add to cart" form looks like. If you'd like to not show the add to cart form, you'll want to first remove the action from the woocommerce_single_product_summary hook.
if(!is_user_logged_in()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
Then add your own action in at that priority to put in your message:
add_action('woocommerce_single_product_summary', function() {
global $product_id;
if(!is_user_logged_in()) {
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
echo $message;
}
});
yes, you can do it by just adding following code into your active theme function.php file.
add_filter('woocommerce_get_price_html','login_before_addtocart');
function login_before_addtocart($price){
if(is_user_logged_in() ){
return $price;
}
else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
$response .= $price;
$response .= '<br> Login to add product into cart';
return $response;
}
}
I am trying to add user name to the footer for customer emails.
I have other code which adds user name. as below.
Its worth noting that I am using functions.php from within a child theme.
add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order )
{
$subject = sprintf( 'New Customer Order (# %s) from %s %s - %s', $order->id, $order->billing_first_name, $order->billing_last_name, $order->order_date );
return $subject;
}
now this works fine, however when I try to use similiar for the footer , I simply get errors or nothing at all.
add_action( 'woocommerce_email_footer', 'update_footer_for_emails');
function update_footer_for_emails( $email, $order )
{
$customer = $order->get_billing_first_name();
echo '<h1>' . $customer . ', please stay in touch</h1>';
}
is anybody able to explain why this isn't working, I have been breaking my head for 2 days on this now ..
Thanks :-)
well it seems I fixed this in the footer, by simply applying the following change.
// function to add a keep in touch note, as well as facebook and insta icons to emails
add_action( 'woocommerce_email_footer', 'update_footer_for_emails');
function update_footer_for_emails( $order )
{
$user_id = $order->object->get_billing_first_name();
$order_id = $order->object->id;
$email_greeting = sprintf( '<h1>Hi %s, please stay in touch</h1>', $user_id );
}
Although its fixed, I actually don't understand why this works ? is this because the object is passed into the footer but for the other templates, the object is already in scope ?
I am using the below function to change the page title in wordpress. It is working on a single page (page.php), but is not working on my static home page or individual posts (single.php).
What do I need to change in order to make this work across the entire site?
<?php
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'wp_title', 'wpse46249_filter_wp_title' );
?>
function wpse46249_filter_wp_title( $title ) {
$some_custom_title_content = 'This is added to title';
$custom_title = $title . $some_custom_title_content;
return $custom_title;
}
add_filter( 'the_title', 'wpse46249_filter_wp_title' );
please note the title is saved in wp_posts table. The filter you used above will change all new posts titles being saved. This filter just modifys the title after pulling it from the db and doesn't actually change the db value.
Also will only work on pages where the_title() is called.
I want to add the product link in the Processing Order email which User receives when order. When an order is placed an Order Email sent, I want to get the product link there when user clicks redirects to the detail product page when clicked. Is there any way, I get the product link or the Product Title would Hyperlink.
THANKS
The answer from gunbunnysoulja works great but needs two little updates:
get_product needs to be wc_get_product
$_product->id needs to be $_product->get_id()
The updated answer is as follows:
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$link = get_permalink( $_product->get_id() );
return ''. $item_name .'';
}
I am currently using this solution, which I found in the comments on another page. This is not my code.
http://www.vanbodevelops.com/tutorials/add-a-link-back-to-the-order-in-woocommerce-new-order-notifications-email#comment-636
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$link = get_permalink( $_product->id );
return ''. $item_name .'';
}
To link a product name with its product page in your order emails, open the functions.php file of your child theme and add the following snippet of code:
* Product Links in WooCommerce Order Emails
*/
add_filter('woocommerce_order_item_name', 'woocommerce_order_item_link', 10, 3);
function woocommerce_order_item_link( $item_name, $item, $bool ) {
$url = get_permalink( $item['product_id'] ) ;
return ''.$item_name .'';
}
I have been wondering how this works as well. There is little info available anywhere - at least not much with detailed step by step instructions.
The best solution I have came up with is to edit the customer-processing-order.php.
All I did was open it up in a text editor and added a few lines of text to:
"Your order has been received and is now being processed. Your order details are shown below for your reference. Please visit "http://www.youlinkurl".
The end user will have to copy and paste that link unfortunately but at least it works.