Problems with a custom pay link shortcode on WooCommerce/wordpress - wordpress

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.

Related

WooCommerce Hook - woocommerce_after_cart_item_name

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.

WooCommerce: Display SKU number in admin side Order listing page custom column

I have added a custom column in Woocommerce admin side order listing page to display Ordered item Products name and total Quantity like this -> [http://prntscr.com/p11rdl] by using the following code:
add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
$order_columns['order_products'] = "Qty-Products Name-SKU";
return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
echo $order_item['quantity'] .' × '. $order_item['name'] .'<br />';
}
}
}
}
Now I also want to display ordered product SKU number after the product name. So anyone have solution for this then please provide help me.
Thank you,
Ketan
Just replace with follows code snippets to add SKU -
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
global $the_order; // the global order object
if( $colname == 'order_products' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
$product = $order_item->get_product();
// product checking
$sku = ( $product && $product->get_sku() ) ? ' - ' . $product->get_sku() : '';
echo $order_item['quantity'] .' × '. $order_item['name'] . ''. $sku . '<br />';
}
}
}
}
Thank you so much I've found this thread extremely helpful, I couldn't thank the original poster enough as well the help from #itzmekhokan and various other contributors, this community is awesome.
What I'm needing now is to add product shelve location straight after SKU.
I have the below code for location fields but not sure how to amend to the code above to make it work. I'm new to coding in general, but have found snippets of code and made it work so far.
Your help and guidance would be greatly appreciated.
// Add Location field
add_action( 'woocommerce_product_options_stock_fields', 'product_shelf' );
function product_shelf() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => 'product_shelf',
'placeholder' => 'Enter product shelf location here',
'label' => 'Shelf Location',
'description' => 'Shelf location of product',
'desc_tip' => 'true',
)
);
}
// Save location data
add_action( 'woocommerce_process_product_meta', 'product_shelf_data' );
function product_shelf_data( $post_id ) {
// grab the location from $_POST
$product_shelf = isset( $_POST[ 'product_shelf' ] ) ? sanitize_text_field( $_POST[ 'product_shelf' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the location using WooCommerce built-in functions
$product->update_meta_data( 'product_shelf', $product_shelf_data );
$product->save();
}

woocommerce display last purchase and cross sells current user

I use specific thank you pages depend of product
I want to display the last purchase (product's name) of the current user on this page and fetch cross sell of the specific product too.
Any help would be apreciate
OF course i made some research and found tutorial like this one
https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/#comment-703884
But i am not enaugh good at php to modify it for my needs
Thank you
If you want to display 50 last purchased product for example, go to function.php in Appearance>Editor>function.php then past the code below:
// Recently Sold Products shortcode for WooCommerce
add_shortcode('sold_products', 'sold_products');
function sold_products($attributes) {
global $wpdb, $woocommerce;
$defaults = array('max_products' => 50, 'title' => "Last purchased");
$parameters = shortcode_atts( $defaults, $attributes);
$max = absint($parameters['max_products']); // number of products to show
$title = sanitize_text_field($parameters['title']);
$html = '<div class="sold_products">'.PHP_EOL;
if ($title) {
$html .= '<h3>Last purchased:</h3>'.PHP_EOL;
}
$table = $wpdb->prefix.'woocommerce_order_items';
$my_query = $wpdb->prepare("SELECT * FROM $table WHERE `order_item_type`='line_item' ORDER BY `order_id` DESC LIMIT %d", $max);
$nr_rows = $wpdb->query($my_query);
if (!$nr_rows) {
$html .= '<p>Aucun produit n\'est acheté encore!</p>';
} else {
$html .= '<ul>'.PHP_EOL;
for ($offset = 0; $offset < $nr_rows; $offset++) {
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
}
$row = $wpdb->get_row($my_query, OBJECT, $offset);
$product_name = $row->order_item_name;
$product = get_page_by_title($product_name, OBJECT, 'product');
$url = get_permalink($product->ID);
$order_id = $row->order_id;
$order = new WC_Order($order_id);
$user = $order->get_user();
if ($user) {
$user_id = $user->ID;
} else {
$user_id = 'Guest';
}
if($user_info->ID == $user_id){
$unix_date = strtotime($order->order_date);
$date = date('d/m/y', $unix_date);
$html .= '<li>'.$product_name.' acheté le '.$date.'</li>'.PHP_EOL;
}
}
$html .= '</ul>'.PHP_EOL;
}
$html .= '</div>'.PHP_EOL;
return $html;
} // end function
you can dislay this result anywhere in your pages just past this shotcode where you want it to be displayed:
[sold_products max_products="50" title="Last purchased"]

Saving add_meta_box data to multiple post_types

I have the following code to create and save custom meta boxes within my Wordpress site. This solution works fine for my 'work' post_type, but will not save on a standard 'post'. The meta boxes appear fine on both post_types, but will only save on 'work'. Therefore I believe the issue lies within the save function, but I cannot decipher any reason why this works on only the one post_type?
I have pared the below code down to what I believe is the essential code.
Thanks.
// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_custom_metaboxes' );
// Register the Project Meta Boxes
function add_custom_metaboxes() {
$post_types = array ( 'post', 'work' );
foreach( $post_types as $post_type )
{
add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
add_meta_box('xx_more', 'More', 'xx_more', 'work', 'normal', 'high');
}
}
// Add Introduction Metabox
function xx_introduction() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="introductionmeta_noncename" id="introductionmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the introduction data if its already been entered
$introduction = get_post_meta($post->ID, '_introduction', true);
// Echo out the field
echo '<textarea name="_introduction" class="widefat" rows="5">' . $introduction . '</textarea>';
}
// Add More Metabox
function xx_more() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="moremeta_noncename" id="moremeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the more data if its already been entered
$more = get_post_meta($post->ID, '_more', true);
// Echo out the field
echo '<textarea name="_more" class="widefat" rows="5">' . $more . '</textarea>';
}
// Save the Metabox Data
function xx_save_custom_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['introductionmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$introduction_meta['_introduction'] = $_POST['_introduction'];
$more_meta['_more'] = $_POST['_more'];
// Add values of $introduction_meta as custom fields
foreach ($introduction_meta as $key => $value) { // Cycle through the $testimonial_meta array
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
// Add values of $more_meta as custom fields
foreach ($more_meta as $key => $value) { // Cycle through the $more_meta array
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'xx_save_custom_meta', 1, 2); // save the custom fields
Having experimented with this for a couple of days, I have a semi solution to the issue I was experiencing. In the original code, I was attempting to register three meta boxes, but placing two of them in multiple post types ($post_type) and the other in just a single post type (work). I have not determined the cause, but this is what was causing the meta data not to save in one of the post types.
Whilst this isn't a full answer with a detailed breakdown, it at least locates the source of the issue, and my initial thought that the issue lies within the save function is correct, I just don't know how to fix this myself. This solution works fine for me however so I won't be investing anymore time into this issue.
Hopefully this is of use to others at least as a starting point.
// Register the Project Meta Boxes
function add_custom_metaboxes() {
$post_types = array ( 'post', 'work' );
foreach( $post_types as $post_type )
{
add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
add_meta_box('xx_more', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
}
}

How-to get a menu label via $post-> or $page->ID

Entirely Revised Please Reread
Hello,
The theme I am using displays the page's title as opposed to it's menu label in the breadcrumbs. I am trying to get the breadcrumbs to instead display the associated menu label if it is available and if not then default to the page_title.
I have come up with some code that I think is close. Line 4/// $menu_items = wp_get_nav_menu_items( $slug ); returns null and it should return the nav item that contains $slug of the current post. Obviously, there is something I do not understand.
What I am attempting to do is get the slug of the current post, then using the slug get the nav item post. Then extract the title of the nav item and use that in place of the page title in the breadcrumbs. If the page was not in the nav system then it should default to the page title, as might be the case for a ppc campaign landing page.
if ( is_page() && !$post->post_parent ) {
$title = null;
$slug = mpactMEDIA_get_the_slug( get_the_ID() );
$menu_items = wp_get_nav_menu_items( $slug );
//var_dump((array)$menu_items);
foreach ( (array)$menu_items as $key => $menu_item ) {
$title = $menu_item->post_title;
}
if ( $title ) { echo $delimiter . ' ' . $before . $title . $after; }
else { echo $delimiter . ' ' . $before . get_the_title() . $after; }
}
I'm my functions.php file I have the following function
function mpactMEDIA_get_the_slug( $id=null ){
if( empty($id) ) global $post;
if( empty($post) ) return '';
$id = $post->ID;
endif;
$slug = basename( get_permalink($id) );
return $slug;
}
Thank you in advance,
Tim
I read the question a few times, I got here searching for an answer, ended up making my own.
function get_menu_label_by_post_id($post_id, $menu) {
$menu_title = '';
$nav = wp_get_nav_menu_items($menu);
foreach ( $nav as $item ) {
if ( $post_id == $item->object_id ) {
$menu_title = $item->post_title;
break;
}
}
return ($menu_title !== '') ? $menu_title : get_the_title($post_id);
}
Example usage:
echo get_menu_label_by_post_id($post->ID, 'Primary Nav');
This will return what the menu label is if it finds it, otherwise just the title of the post ID.
Check the documentation for wp_get_nav_menu_items. It doesn't take a page slug as a parameter at all.
If you want to list child pages of a given page, use wp_list_pages and pass a child_of parameter to it.
Also, as a side note, if you know the $post and want the slug, it's just $post->post_name

Resources