How to add the payment-methods endpoint to the account navigation in Woocommerce? - wordpress

I want to add a a link to the payment-methods page so that a logged in user easily can change method. Or add one.
I have set up the correct hook, where i'm doing some other stuff, but cant figure out how to proceed?
add_filter('woocommerce_account_menu_items', array($this, 'shorty_account_menu_items'), 10, 1);
function shorty_account_menu_items($items) {
$items['edit-account'] = 'Settings';
$items['orders'] = 'My History';
unset($items['dashboard']);
return $items;
}

The filter hook you are using is correct. To display a link to payment methods page, push the endpoint into the $items array and output the entire array.
Usage:
add_filter('woocommerce_account_menu_items', 'shorty_account_menu_items');
function shorty_account_menu_items($items) {
$items['payment-methods']='Payment Methods';
return $items;
}
You can also move the payment-methods link to appear above the logout link using this snippet:
add_filter('woocommerce_account_menu_items', function($items) {
$logout = $items['customer-logout'];
unset($items['customer-logout']);
$items['payment-methods'] = "Payment Methods";
$items['customer-logout'] = $logout;
return $items;
});

Related

Customize woocommerce order tracking page

in the default order tracking page for wooocommerce there are two fields { ordreid and email }
.
I want to make email field unrequired
I tried with this code in function.php file but it wasn't successful :(
add_filter( 'woocommerce-order-tracking', 'ts_unrequire_wc_email_field');
function ts_unrequire_wc_email_field( $fields ) {
$fields['order_email']['required'] = false;
return $fields;
}
any help will be useful .. and i hope that you are all safe during the breakout of covid-19 <3**

Add the drupal commerce products to cart without page reload programmatically

How to implement following task :-
I have 2 div, In first div i have product name and add link, If user click on add link related product should be add in second div.
In second div at bottom i have add to cart button so on click cart button all added products should be add in drupal commerce add to cart page.
Just for reference please check below link :-
http://buildabagpartyfavours.ca/pages/build-your-own-goodie-bag
If anybody use Drupal 8, and want to solve this question, can in this way:
If you have the product variation id $var_id, you can create an "a" tag with "use-ajax" class:
Add to cart
In routing.yml you have to add the following path with controller:
path: '/add/product/{pid}'
_controller: Drupal\MY_MODULE\Controller\ShopProductController::addToCart
And you have to create controller with ajax response:
namespace Drupal\MY_MODULE\Controller;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\Order;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\CssCommand;
class ShopProductController {
public function addToCart($pid) {
$ajax_response = new AjaxResponse();
#### ADD TO CART ####
// variation_id of product.
if (isset($pid) && !empty($pid)) {
$store_id = 1;
$order_type = 'default';
$variation_id = $pid;
$entity_manager = \Drupal::entityManager();
$cart_manager = \Drupal::service('commerce_cart.cart_manager');
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
// Drupal\commerce_store\Entity\Store::load($store_id);
$store = $entity_manager->getStorage('commerce_store')->load($store_id);
$product_variation = $entity_manager->getStorage('commerce_product_variation')->load($variation_id);
// order type and store
$cart = $cart_provider->getCart($order_type, $store);
if (!$cart) {
$cart = $cart_provider->createCart($order_type, $store);
}
//Create new order item
$order_item = $entity_manager->getStorage('commerce_order_item')->create(array(
'type' => 'default',
'purchased_entity' => (string) $variation_id,
'quantity' => 1,
'unit_price' => $product_variation->getPrice(),
));
$order_item->save();
$cart_manager->addOrderItem($cart, $order_item);
$ajax_response->addCommand(new HtmlCommand('.-add-to-cart', '<span class="-added">Added</span>'));
}
return $ajax_response;
}
}
Make your button triggers some AJAX call, pass product id and on other side in your AJAX callback script collect product id, create line item from it and add it to the cart ( https://www.drupal.org/node/1288414 ).
When AJAX call is finished you'll have to call another one, to update cart block.

BuddyPress / Wordpress Get dynamic user ID

I'm trying to make a function that overrides the current users avatar URL with a custom URL that is saved in their user meta. So far the function works except that I'm stumped trying to figure out how to have the function grab the user ID of the user buddypress/wordpress is getting an avatar for.
The code so far looks like this:
// return new URL for avatar
function wpse_49216_my_new_avatar_url() {
// get user_id of user bp/wp is getting avatar for
$user_id = bp_get_member_user_id();
// get avatar name from above user's meta
$avatar_choice = get_user_meta($user_id, "avatar_choice", true);
if($avatar_choice) {
return 'path_to_avatars/'.$avatar_choice.'.png';
} else {
return 'path_to_avatars/default-avatar.png';
}
}
add_filter( 'bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url' );
// Replace image src="" with the new avatar URL
function wpse_49216_filter_bp_avatar( $html ) {
return preg_replace( '/src=".+?"/', 'src="' . wpse_49216_my_new_avatar_url() . '"', $html );
}
add_filter( 'bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar' );
The main problem is if I set the $user_id to the current logged in user's ID, then all avatars will be load the avatar of the current logged in user. And bp_get_member_user_id() only works on member pages. I need something that works universally. Does any Wordpress/Buddypress expert have any idea how I can get the correct user ID?
I need something that works universally
If you look at the function you are filtering, you'll see that it depends on knowing 'where' you are in BP. Since it is driven by context, there is no universal approach.
You can user below functiona for the Global userID:
bp_displayed_user_id()

How do you remove or change the functionality of the Publish button on a custom WordPress post?

I have a custom post type and need keep the post status from getting set to 'Published' when you click the Publish button. Instead, it should work like the Save Draft button. So I either need to figure out how to just remove the Publish button so the user's can only click Save Draft our preferably, update the Publish button functionality so it doesn't set the post to publish.
You can use wordpress action hooks to modify default behaviors.
http://codex.wordpress.org/Function_Reference/add_action
In your case, you want to use the 'publish_post' hook.
So you can do
function dont_publish( $post_ID )
{
if(get_post_type($post_ID) == 'your_custom_type'){
exit;
}
}
//the dont_publish function will be called after the publish button is clicked
add_action( 'publish_post', 'dont_publish' );
The way it is above, nothing will happen at all if the publish button is clicked, but you can play around with the dont_publish function to get the results you want.
#PhoenixWing156 was close but one little change so the the other post types get updated as usual.
function dont_publish( $data , $postarr ) {
if($data['post_type'] == 'custom_post_type') {
$data['post_status'] = 'draft';
}
return $data;
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
The wp_insert_post_data hook is called before information about a post is saved to the database.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
You can try:
function dont_publish( $data , $postarr )
{
if($data['post_type'] == 'custom_post_type'){
$data['post_status'] = 'draft';
return $data;
}
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
WordPress provides the remove_meta_box() function exactly for this purpose.Just add this below code:-
add_action( 'admin_menu', function () {
remove_meta_box( 'submitdiv', 'Your_custom_post_type', 'side' );
} );
You could also disable the default saving metabox and add you own.
This is not documented well in the developer docs of wordpress.
To do this you have to hook into the "add_meta_boxes"-hook and in the hooked function yo have to call remove_meta_box('submitdiv','your-cpt','side');
The code should be something like this:
function your_cpt_metaboxes(){
remove_meta_box('submitdiv','your-cpt','side');
...
}
add_action('add_meta_boxes','function your_cpt_metaboxes');
your-cpt has to be changed to the name of your cpt of course.
I was also searching for this handy snippet and found it in the plugin Awesome Support.
The original saving metabox code can be found in /wp-admin/includes/metaboxes.php .
Just search for post_submit_meta_box (in WP 5.4 on line 22).

Close Overlay and Redirect to Custom URL on Save-Node in Drupal 7?

When my node form saves, I want to close the admin overlay and redirect to a custom URL that is stored with the node. hook_form_alter() is setting $form['#redirect'] but I think that would only work with no admin overlay.
I have never used it before myself, but i think you can call the overlay_close_dialog(...) function from your hook_submit
See http://api.drupal.org/api/drupal/modules--overlay--overlay.module/function/overlay_close_dialog/7 for more info
Kept googling my way here as well.. this is the working solution:
Inside your form:
$form['somebutton']['#submit'] = array('your_custom_callback');
Add a custom callback
function your_custom_callback($form, &$form_state) {
//redirect users to Drupal.org
$url = "http://drupal.org";
if (module_exists('overlay') && overlay_get_mode() == 'child') {
unset($_GET['destination']);
overlay_close_dialog($url, array('external' => TRUE));
$form_state['redirect'] = FALSE;
} else {
$form_state['redirect'] = $url;
}
}
Kept googling my way here and wanted to post a final solution that worked for me on Drupal 7 (got here thanks to jakraska's suggestion). First use hook_form_FORM_ID_alter() to hook into the specific form you want to alter
/**
* Implementation of hook_form_FORM_ID_alter()
*
**/
function mymodule_form_FORM_ID_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_callback';
}
Then write your callback that will render the page. In my case I simply wanted to close the overlay, so that's why I used the $form_state['redirect'] = FALSE;
function mymodule_callback(&$form, &$form_state) {
// Form API will re-render the current page and pass the redirect information to the overlay JavaScript
overlay_close_dialog();
// stay on the same page after all submit callbacks have been processed.
$form_state['redirect'] = FALSE;
}
If you want to redirect to another path,
See https://api.acquia.com/api/drupal/modules!overlay!overlay.module/function/overlay_form_submit/7 - I believe that should get you there.
Make sure to use $form_state['rebuild'] = TRUE; inside your hook_submit function, otherwise it would not close overlay after form is submitted.

Resources