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
}
Related
Short version: How do I get upsell for variable product in Woocommerce?
Longer version:
I need the product ids for upsells.
My old code contains depracated code:
$upsells = $product->get_upsells(); // $product is instace of WC_Product_Variable::
The call should be the following:
$upsells = $product->get_upsells_ids(); // $product is WC_Product::
But a different class.
I tried to get the parent instance using wc_get_product($product->get_parent_id()) - but fail.
So, given the instance of WC_Product_Variable how do I get to the parent method WC_Product::$product->get_upsells_ids() ??
Thanks
The get_upsells-ids() method does not exist. Try get_upsell_ids().
The following code will show you all the upsell ids for the product you are currently visiting:
// quick test to check upsell ids
add_action( 'woocommerce_before_single_product', 'echo_upsell_ids' );
function echo_upsell_ids() {
global $product;
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
$product = wc_get_product( $product_id );
$upsell_ids = $product->get_upsell_ids();
echo '<pre>' . print_r( $upsell_ids, true ) . '</pre>';
}
I have tested the code and it works. The snippet goes into your child theme's functions.php file.
I have created a advance custom field using ACF wordpress plugin to check if the product is available for this month or not. The code I am using is below but when it does work to point where it shows add to cart button if the product is available for that month if not then shows the message.
Now the issue is when product is available to purchase if I click on add to cart it says "Sorry, this product cannot be purchased." I am not able to find what is wrong with it.
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
$months = (array) get_field('availability');
$purchasable = in_array( date('F'), $months ) ? $purchasable : false;
return $purchasable;
}
add_action( 'woocommerce_single_product_summary', 'unavailable_product_display_message', 20 );
function unavailable_product_display_message() {
global $product;
if(! $product->is_purchasable() ){
echo '<p style="color:#e00000;">' . __("This product is currently unavailable.") . '</p>';
}
}
Try to change this line of code:
$months = (array) get_field('availability');
to:
$months = (array) get_field('availability', $product->get_id());
The get_field function doesn`t know from which product to get, so this will be an empty array. The variable $purchasable will always be false when this happens.
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.
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 );
I am trying to get some product prices to show up in one custom template. Is there, perhaps, an integrated woocommerce function that given ID will give me products price?
Something along the lines of: woocommerce_productprice(423)
Basically, I have a custom page template as a catalog and I want to hook product prices up to that template, so whenever something changes, it automatically changes the price without me having to change the custom template.
Help please!
Updated for WooCommerce 2.3:
$product = we_get_product( $post_id );
echo $product->get_price();
$product = wc_get_product( $product_id );
$price = $product->get_price_html();
woocommerce 3.x, it working!
Here try this
$product = new WC_Product( $post_id );
$price = $product->price;
echo $price;
Actually, there's a typo on the above code, see below:
$product = wc_get_product( $post_id );
echo $product->get_price();