My Woocommerce store with 5 store managers, I use the Local Pickup plugin, it automatically generates custom fields.
How do I correctly assign orders to them and make them invisible to each other's orders?
I have found a similar function, but can't work for custom fields.
function before_checkout_create_order($order, $data) {
$idp = get_post_meta( $order_id, 'pickup_name', true );
$store_manager_id = '';
$belgium_region = ['3'];
$czech_region = ['2'];
$uk_region = ['1'];
if (in_array($idp, $belgium_region)) {
// Manually assigning the _store_manager_id using the user id, yours will differ
$store_manager_id = 7;
} else if (in_array($idp, $czech_region)) {
$store_manager_id = 3;
} else if (in_array($idp, $uk_region)) {
$store_manager_id = 2;
} else {
$store_manager_id = 1;
}
$order->update_meta_data('_store_manager_id', $store_manager_id);
}
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function custom_admin_shop_manager_orders($query) {
global $pagenow;
$qv = &$query->query_vars;
$currentUserRoles = wp_get_current_user()->roles;
$user_id = get_current_user_id();
if (in_array('shop_manager', $currentUserRoles)) {
if ( $pagenow == 'edit.php' &&
isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
// I use the meta key from step 1 as a second parameter here
$query->set('meta_key', '_store_manager_id');
// The value we want to find is the $user_id defined above
$query->set('meta_value', $user_id);
}
}
return $query;
}
add_filter('pre_get_posts', 'custom_admin_shop_manager_orders');
Related
I'm trying to hide the "ship to a different address" on WooCommerce checkout page for 2 user roles, if their cart contains any of the specified product ID's.
My code is working for a single product ID:
/** Shipping Address */
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// The targeted products & roles
$targeted_variation_id = 414;
$targeted_user_role = 'team';
$targeted_user_role2 = 'team2';
// Flag
$found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
// True & user Role is
if ( $found && current_user_can( $targeted_user_role )) {
$needs_shipping = false;
}
// True & only 1 item in cart
if ( $found && current_user_can( $targeted_user_role2 )) {
$needs_shipping = false;
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );
But then when I try to create an array of multiple IDs, I can't achieve the desired result, this is my attempt:
**Multiple Variants**
/** Shipping Address */
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// The targeted products & roles
$targeted_variation_id = array(414,617);
$targeted_user_role = 'team';
$targeted_user_role2 = 'team2';
// Flag
$found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
// True & user Role is
if ( $found && current_user_can( $targeted_user_role )) {
$needs_shipping = false;
}
// True & only 1 item in cart
if ( $found && current_user_can( $targeted_user_role2 )) {
$needs_shipping = false;
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );
Anyone able to help figure out how to get it to work with multiple ID's and not just one?
The mistakes in your code is in the following line:
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) )
$targeted_variation_id has been changed to an array,
while the searched value must be a string when using in_array()
It also seems more logical to check the user role first, before iterate all cart items
So you get:
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// Retrieve the current user object
$user = wp_get_current_user();
// Add your user roles, several can be entered, separated by a comma
$user_roles = array( 'team1', 'team2', 'administrator' );
// Targeted product IDs, several can be entered, separated by a comma
$targeted_product_ids = array( 30, 813, 414, 617 );
// User role is found
if ( array_intersect( $user_roles, (array) $user->roles ) ) {
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// Checks if a value exists in an array
if ( in_array( $product_id, $targeted_product_ids ) ) {
$needs_shipping = false;
break;
}
}
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );
In Woocommerce, i want to hide the credit card payment option if a specific product variation is in the cart. Please help.
Thanks.
This is what i have working now. I assigned a separate shipping class to each variation i want to disable a specific payment method at checkout. But it would be much easier if i coud target specific attribute values, so i don't have to assign a shipping class.
<?php
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$shipping_class_target = 106; // the shipping class ID assigned to specific variations
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset($available_gateways['cod']); // unset 'cod'
}
else {
unset($available_gateways['bacs']); // unset 'bacs'
}
return $available_gateways;
}
If you are looking to check the variations for each item in the cart, you have to lookup the attributes $product->get_attributes() and then loop through those and get the array key and value for each.
In this example, I used
Size (pa_size) and Small
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
// See if there is an attribute called 'pa_size' in the cart
// Replace with whatever attribute you want
if (array_key_exists('pa_size', (array) $values['data']->get_attributes() ) ) {
foreach ($values['data']->get_attributes() as $attribute => $variation);
// Replace 'small' with your value.
if ($variation == 'small') $in_cart = true; //edited
}
}
if ( $in_cart ) {
unset($available_gateways['cod']); // unset 'cod'
}
else {
unset($available_gateways['bacs']); // unset 'bacs'
}
return $available_gateways;
}
Some of my tags have some custom term meta and I'm saving that meta with this function:
add_action ( 'edit_term', 'save_termmeta_tag');
// save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
if ( isset( $_POST['Tag_meta'] ) ) {
$t_id = $term_id;
$tag_meta = get_option( "tag_$t_id");
$tag_keys = array_keys($_POST['Tag_meta']);
foreach ($tag_keys as $key){
if (isset($_POST['Tag_meta'][$key])){
$tag_meta[$key] = $_POST['Tag_meta'][$key];
}
}
//save the option array
update_option( "tag_$t_id", $tag_meta );
}
}
Now, I would like to get all tag with a specific option like 'channel'.
Is this possible, and how?
function get_term_with_option($opt) {
$terms = array();
// load all options
$options = wp_load_alloptions();
foreach($options as $k=>$v) {
// find keys with prefix `tag_`
if (strpos($k, 'tag_') === 0) {
$v = maybe_unserialize($v);
// check if has a meta key $opt
if (is_array($v) && array_key_exists($opt, $v)) {
$term = get_term(intval(str_replace('tag_', '', $k)));
if (!is_wp_error($term)) {
$terms[] = $term;
}
}
}
}
return $terms;
}
print_r(get_term_with_option('channel'));
On Woocommerce, I have an error message on my function but i don't unserstand why it happens.
Uncaught Error: Call to a member function get_cart() on null in ...
The function checks id a category product is in the cart.
The error is displayed in the order detail in backend
It's used in functions.php , executed on checkout page,
called by these hooks:
woocommerce_before_order_notes
woocommerce_checkout_process
woocommerce_checkout_update_order_meta
woocommerce_admin_order_data_after_billing_address
woocommerce_email_order_meta_keys
https://i.stack.imgur.com/v7Atp.png
function is_in_the_cart() {
$found=false;
// Find if product is in the cart price <=40
foreach( WC()->cart->get_cart() as $cart_item_key => $values )
{
$cart_product = $values['data'];
$price = accessProtected($values['data'], 'changes')['price'];
if ($price >=40 && $cart_product->id=='969') {$found=true;}
}
if ( $found ) {
return true;
} else {
return false;
}
}
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
if (is_in_the_cart())
{
global $woocommerce;
// Check if set, if its not set add an error.
if ($_POST['dvd'] == "blank")
wc_add_notice( '<strong>Merci de séléctionner un DVD</strong>', 'error' );
}
}
//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
function wps_select_checkout_field_update_order_meta( $order_id ) {
if (is_in_the_cart())
{
if ($_POST['dvd']) update_post_meta( $order_id, 'dvd', esc_attr($_POST['dvd']));
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($order){
if (is_in_the_cart())
{
echo '<p><strong>DVD: </strong> ' . get_post_meta( $order->id, 'dvd', true ) . '</p>';
}
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
if (is_in_the_cart())
{
$keys['Dvd:'] = 'dvd';
return $keys;
}
}
I think you want to check to make sure the cart isn't empty before your function.
Also... You can access price by using get_price() from the product object which you are retrieving with $values['data']
function is_in_the_cart(){
// Make sure it's only on front end
if (is_admin()) return false;
$found = false;
// If cart is empty - bail and return false
if (empty (WC()->cart->get_cart())) {
return false;
} else {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values[ 'data' ];
// Find if product is in the cart price <=40
$price = floatval($cart_product->get_price());
if ( $price >= 40 && $cart_product->id == '969' ) {
$found = true;
}
}
if ( $found ) {
return true;
} else {
return false;
}
}
}
UPDATED - added is_admin to prevent from running on backend.
For a WooCommerce webshop we send out a lot of payment links through email. Before getting to the payment page customers are obligated to login first. We would like the customer to be able to complete payment without logging in as often they don't know their password because of different company departments.
I found this code but this only lets the administrator pay without logging in:
function your_custom_function_name($allcaps, $caps, $args)
{
if (isset($caps[0])) {
switch ($caps[0]) {
case 'pay_for_order':
$user_id = $args[1];
$order_id = isset($args[2]) ? $args[2] : null;
// When no order ID, we assume it's a new order
// and thus, customer can pay for it
if (!$order_id) {
$allcaps['pay_for_order'] = true;
break;
}
$user = get_userdata($user_id);
if (in_array('administrator', (array)$user->roles)) {
$allcaps['pay_for_order'] = true;
}
$order = wc_get_order($order_id);
if ($order && ($user_id == $order->get_user_id() || !$order - > get_user_id())) {
$allcaps['pay_for_order'] = true;
}
break;
}
}
return $allcaps;
}
add_filter('user_has_cap', 'your_custom_function_name', 10, 3);
here is working function with all users just test it :
function your_custom_function_name( $allcaps, $caps, $args ) {
if ( isset( $caps[0] ) ) {
switch ( $caps[0] ) {
case 'pay_for_order' :
$order_id = isset( $args[2] ) ? $args[2] : null;
$order = wc_get_order( $order_id );
$user = $order->get_user();
$user_id = $user->ID;
// When no order ID, we assume it's a new order
// and thus, customer can pay for it
if ( ! $order_id ) {
$allcaps['pay_for_order'] = true;
break;
}
$order = wc_get_order( $order_id );
if ( $order && ( $user_id == $order->get_user_id() || ! $order->get_user_id() ) ) {
$allcaps['pay_for_order'] = true;
}
break;
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'your_custom_function_name', 10, 3 );
I have created a different solution for this problem, allowing anyone who has the WooCommerce-generated Payment URL (which includes the Order Key) to complete the payment for that order. (So we retain some of the security/protection, rather than just allowing anyone to pay for anything and see any order.)
function allow_payment_without_login( $allcaps, $caps, $args ) {
// Check we are looking at the WooCommerce Pay For Order Page
if ( !isset( $caps[0] ) || $caps[0] != 'pay_for_order' )
return $allcaps;
// Check that a Key is provided
if ( !isset( $_GET['key'] ) )
return $allcaps;
// Find the Related Order
$order = wc_get_order( $args[2] );
if( !$order )
return $allcaps; # Invalid Order
// Get the Order Key from the WooCommerce Order
$order_key = $order->get_order_key();
// Get the Order Key from the URL Query String
$order_key_check = $_GET['key'];
// Set the Permission to TRUE if the Order Keys Match
$allcaps['pay_for_order'] = ( $order_key == $order_key_check );
return $allcaps;
}
add_filter( 'user_has_cap', 'allow_payment_without_login', 10, 3 );
Using this function, a user visiting a URL which has an Order Number, and the associated Order Key, will be able to complete the payment, but if the Order Key is not valid, or not present, then it will fail.
Examples:
your.domain/checkout/order-pay/987/?pay_for_order=true&key=wc_order_5c4155dd4462cPASS if "wc_order_5c4155dd4462c" is Order Key for Order #987
your.domain/checkout/order-pay/987/?pay_for_order=trueFAIL as No Key Parameter Present
your.domain/checkout/order-pay/987/FAIL as No Key Parameter Present