WooCommerce - Get order cart items quantity ADMIN - wordpress

This variable works for the template:
$woocommerce->cart->cart_contents_count;
But doesn't work for admin...
Does anyone know how to get the cart items quantity and display it on the order edition page?
/**
* Display field value on the order edition page
**/
add_action( 'woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
// doesn't work...
echo $woocommerce->cart->cart_contents_count;
}
}

Cart's classes aren't loaded in the dashboard area by default.
Probably, you need to display number of products that are in the order you're editing now. In that case you should use $order->get_item_count() function in your "my_custom_checkout_field_display_admin_order_meta".
But if you really need to get cart items of current logged in user (admin), you should load all cart contents from session manually:
function my_custom_checkout_field_display_admin_order_meta(WC_Order $order){
global $woocommerce;
include_once( $woocommerce->plugin_path . '/classes/abstracts/abstract-wc-session.php' ); // Abstract for session implementations
include_once( $woocommerce->plugin_path . '/classes/class-wc-session-handler.php' ); // WC Session class
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
$woocommerce->session = new $session_class();
$woocommerce->cart = new WC_Cart();
$woocommerce->cart->get_cart_from_session();
echo $woocommerce->cart->cart_contents_count; //here it is
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

Related

Add text to cart page when using the "woocommerce_cart_contents_changed" hook

I am using the "woocommerce_cart_contents_changed" hook to check if a user has already added a specific product to basket however, would also like to display some text on the page to inform them that the product has been removed. The function takes 1 argument which returns an array of all cart items. If anyone could help, that would be great. Thank you.
I have tried to just simply echo content within the function. I would like some text to be displayed anywhere on the cart page once the product is added to the basket.
Hi for displaying cart items after the user add some item to the cart you need to use the AJAX function which updates the HTML page dynamically.
But if you just want to display what is in the cart after the page reload you can try that code:
/**
* Plugin Name: WooCommerce Cart Items
* Description: Display all items in WooCommerce cart with shortcode.
* Version: 1.0.0
* Text Domain: wc-cart-items
*/
class WC_Cart_Items_Shortcode {
public static function init() {
add_shortcode( 'wc_cart_items', array( __CLASS__, 'shortcode' ) );
}
public static function shortcode() {
$cart = WC()->cart->get_cart();
$items = '';
foreach ( $cart as $cart_item ) {
$product = $cart_item['data'];
$items .= sprintf( '<li>%s - %s x %s</li>', $product->get_name(), $cart_item['quantity'], $product->get_price() );
}
return sprintf( '<ul>%s</ul>', $items );
}
}
add_action( 'init', array( 'WC_Cart_Items_Shortcode', 'init' ) );

Wordpress add action on product publish

I want to add an action when a new product is created. Many people recommend save_post_product but the problem is that when i use it it runs only when i go to products and click add new which is completely useless because $post returns an object with empty properties. When i publish my product it doesn't do anything. Am i doing something wrong? Below is the code that i used
add_action('save_post_product', 'mp_sync_on_product_save', 10, 3);
function mp_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
echo "<script>console.log('" . $product . "');</script>";
// do something with this product
}

Woocommerce Updating products price with php does not trigger price change notification

I was playing around with php code where i change price for a certain product. Also i am using a plugin that notify's by email once price fells below some amount. The product price updates with no issues also appears with new price in the product list view, but sadly does not trigger the notification plugin. The strange thing is when i go to product edit and click update i immediately get the notification email.
The php code i am using currently:
$ppt = $value / $divider;
$product = wc_get_product($product_id);
print_r($product);
// Mark product as updated
update_post_meta($product_id, '_price', $ppt );
update_post_meta($product_id, '_regular_price', $ppt );
update_post_meta($product_id, '_sync_updated', true );
$product->save();
}
wp_reset_query();
Thank you in advance!
You should use WC_Product setter instead update_post_meta.
Try this (not tested) :
$ppt = $value / $divider;
$product = new WC_Product( $product_id );
// Mark product as updated
$product->set_price( $ppt );
$product->set_regular_price( $ppt );
$product->save();
}
wp_reset_query();

Error with: WooCommerce Avoid add to cart for non logged user

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;
}
}

Adding extra info to order

I am programmatically adding a product to the cart. Besides this, somehow, I want to store some extra info (an array) to the order. When the client finishes the order, I want to access that info through some WordPress actions. I'll have to do this immediately after adding the product to the cart, because the info may change after that, if the user doesn't finish the order right away. Is there any way I can do it, without putting the database to work?
You should probably use the WooCommerce Cart Item Meta API and the WooCommerce Order Item Meta API.
You use them like this:
// Add to cart item
// This is triggered on add to cart
add_filter('woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2);
function my_add_cart_item_data( $cart_item_meta, $product_id ) {
//Here we can easily filter what values should be added to what products using the $product_id
$cart_item_meta['my_meta_key'] = 'meta value';
return $cart_item_meta;
}
// Add to order item when the cart is converted to an order
// This is triggered when the order is created
add_action('woocommerce_add_order_item_meta', 'my_order_item_meta'), 10, 2);
function my_order_item_meta( $item_id, $values, $cart_item_key ) {
// The value stored in cart above is accessable in $values here
woocommerce_add_order_item_meta( $item_id, 'meta_key', $values['my_meta_key'] );
//Or add what ever you want
$meta_value = 'value';
woocommerce_add_order_item_meta( $item_id, 'meta_key', $meta_value );
}
I hope that helps.

Resources