woocommerce conditional email template if product type in order - wordpress

I try to send a customized email template when a customer has a ticket (custom product type) in cart.
I have the following:
function bc_customer_completed_order_template($template, $template_name, $template_path)
{
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
$type = get_class($product);
if ( $type == 'WC_Product_Tickets' && 'customer-completed-order.php' === basename($template) ) {
$template = trailingslashit(plugin_dir_path( __FILE__ )) . 'templates/customer-completed-order.php';
}
}
return $template;
}
add_filter('woocommerce_locate_template', 'bc_customer_completed_order_template', 10, 3);
The conditionals are working (on cart and checkout page for example), but when the order is placed, the new template is not used.
Anybody?

Your email template will look like this:
Code snippets:
// Suppose orders don't have ticket products.
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
// Load custom email template.
wc_get_template( 'templates/custom-customer-completed-order.php' );
// Return as we don't need the below code.
return;
}

It turned out, although the above solution is correct in its idea, in reality one cannot load a template and make use of the $order without extending the woocommerce email class.
Therefore i loaded the function inside the email template itself and made an if - else statement so for situation A the layout is different then for situation b.
Like so:
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
//custom email layout here//
}
else{
//Regular email template here
}

Related

Woocommerce restricted cart based on multiple product ID (problem with variations ID product)

I have problem to restrict cart based on multiple product ID, to be exact, with the variables product. I found the code online and try it out. The solutions seem to be okay with simple product.
The only problem is, how to include variations ID in product array? For example, I have two product ID, 1669 and 1694. 1694 is variable products, where it have 4 variations ID; 1769,1770, 1771 and 1772 while 1669 is simple product. When I click 1669(simple product) and add to cart, I cannot add 1694(variables product). I want to make it enable for ID 1694.
However, when i add to cart the variable product first (1694), then the 1669 can be add to cart. below is the code :
function aelia_get_cart_contents() {
$cart_contents = array();
/**
* Load the cart object. This defaults to the persistant cart if null.
*/
$cart = WC()->session->get( 'cart', null );
if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { // #codingStandardsIgnoreLine
$cart = $saved_cart['cart'];
}
elseif ( is_null( $cart ) ) {
$cart = array();
}
elseif ( is_array( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart_' . get_current_blog_id(), true ) ) ) { // #codingStandardsIgnoreLine
$cart = array_merge( $saved_cart['cart'], $cart );
}
if ( is_array( $cart ) ) {
foreach ( $cart as $key => $values ) {
$_product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
if ( ! empty( $_product ) && $_product->exists() && $values['quantity'] > 0 ) {
if ( $_product->is_purchasable() ) {
// Put session data into array. Run through filter so other plugins can load their own session data
$session_data = array_merge( $values, array( 'data' => $_product ) );
$cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
}
}
}
}
return $cart_contents;
}
// Step 1 - Keep track of cart contents
add_action('wp_loaded', function() {
// If there is no session, then we don't have a cart and we should not take
// any action
if(!is_object(WC()->session)) {
return;
}
global $allowed_cart_items;
global $restricted_cart_items;
$restricted_cart_items = array( 1669,1694) ;
// 1669 is simple product, 1694 is variable product
// "Snoop" into the cart contents, without actually loading the whole cart
foreach(aelia_get_cart_contents() as $item) {
if(in_array($item['data']->get_id(), $restricted_cart_items)) {
$allowed_cart_items[] = $item['data']->get_id();
// If you need to allow MULTIPLE restricted items in the cart, comment
// the line below
break;
}
}
// Step 2 - Make disallowed products "not purchasable"
add_filter('woocommerce_is_purchasable', function($is_purchasable, $product) {
global $restricted_cart_items;
global $allowed_cart_items;
// If any of the restricted products is in the cart, any other must be made
// "not purchasable"
if(!empty($allowed_cart_items)) {
// To allow MULTIPLE products from the restricted ones, use the line below
$is_purchasable = in_array($product->id, $allowed_cart_items) || in_array($product->id, $restricted_cart_items);
// To allow a SINGLE products from the restricted ones, use the line below
// $is_purchasable = in_array($product->get_id(), $allowed_cart_items);
}
return $is_purchasable;
}, 10, 2);
}, 10);
hope you guys can help me. thank you
to make sure those product (simple and variable products) can be add to cart together.

Hide specific products on woocommerce when ACF true/false is true in a product

I have created a custom field true/false, and I want when true is selected in a product not to be displayed in the eshop.
I want to insert the code inside the functions.php
example
if ( in_array( 'subscriber', (array) $user->roles ) || !is_user_logged_in() ) {
$postid = get_the_ID();
$prd_only_for_Customers = get_field('prd_clients', $postid); // The ACF true/false field }
Can anyone help ?
As Howard said your question is incomplete but you can use the following method to set product hidden.
You can use pre_get_posts hook in your functions.php. Since Woocommerce 3 the products visibility is now handled by the 'product_visibility' custom taxonomy for the terms 'exclude-from-catalog' and 'exclude-from-search'… See this thread or this one too.
So you should use instead the WC_Product CRUD setter methods set_catalog_visibility() this way:
function get_post_ids_by_meta_key_and_value($key, $value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT post_id FROM `".$wpdb->postmeta."` WHERE meta_key='".$wpdb->escape($key)."' AND meta_value='".$wpdb->escape($value)."'");
$post_ids = [];
foreach( $meta as $m ) {
$post_ids[] = $m->post_id;
}
return $post_ids;
}
add_action('pre_get_posts', function( $query ){
if ( $query->is_main_query() && is_woocommerce() && !is_user_logged_in() ) {
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$product->save();
}
}
});
This code isn't tested, let me know if it worked or you faced any problem.
This is my final code if anyone needs something like this
// Specific products show only for Customer and administrator role
add_action('pre_get_posts', function( $query ){
$user = wp_get_current_user();
if ( $query->is_main_query() && is_woocommerce()) {
if (!check_user_role(array('customer','administrator')) || !is_user_logged_in() ) {
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$product->save();
}
}
else{
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('visible');
// Save and sync the product visibility
$product->save();
}
}
}
});

Display message in WooCommerce email notifications when order has backorder items in it

I am trying to display a specific message on the Order confirmation email IF one of several products of your order is/are on backorder.
I am struggling to get the right function to scan all the products and get my boolean working.
My current code:
add_action( 'woocommerce_email_after_order_table', 'backordered_items_checkout_notice_email', 20, 4 );
function backordered_items_checkout_notice_email( $order, $sent_to_admin, $plain_text, $email ) {
$found2 = false;
foreach ( $order->get_items() as $item ) {
if( $item['data']->is_on_backorder( $item['quantity'] ) ) {
$found2 = true;
break;
}
}
if( $found2 ) {
if ( $email->id == 'customer_processing_order' ) {echo ' <strong>'.__('⌛ One or several products are Currently out of stock. <br/>Please allow 2-3 weeks for delivery.', 'plugin-mve').'</strong><br/>';}
}
}
With this code, when you click on "Order" the page just freezes and no email is sent. But I get the order in the backend.
Could anyone give me a hand to fix?
Your code contains a CRITICAL uncaught error, namely: Call to a member function is_on_backorder() on null
Following code will add the message for the customer_processing_order email notification. Also see: How to target other WooCommerce order emails
So you get:
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// Initialize
$flag = false;
// Target certain email notification
if ( $email->id == 'customer_processing_order' ) {
// Iterating through each item in the order
foreach ( $order->get_items() as $item ) {
// Get a an instance of product object related to the order item
$product = $item->get_product();
// Check if the product is on backorder
if ( $product->is_on_backorder() ) {
$flag = true;
// Stop the loop
break;
}
}
// True
if ( $flag ) {
echo '<p style="color: red; font-size: 30px;">' . __( 'My message', 'woocommerce' ) . '</p>';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );

Stop WooCommerce For Reducing Stock On Abandoned Admin New Order

If an admin goes to create an order but abandons it, the stock level is still reduced.
Steps to reproduce:
install WordPress
install WooCommerce
create simple product and tick "manage stock?" and set stock level to 10
view on front-end (see screenshot before.png)
as admin create new order but don't save it (new -> order -> add item -> exit page)
view on front-end (see screenshot after.png)
Notice stock level has been reduced even those order wasn't saved.
Is there anyway to avoid this?
Before.png:
After.png
I have worked on this issue and wrote a basic code for that.
Hooks used: "woocommerce_order_item_add_action_buttons" for Admin Order Add Item(s) and "woocommerce_process_shop_order_meta" for Admin Order Creation/Update.
First part: Stop reducing stocks of items that added while the order has not been created.
// define the woocommerce_order_item_add_action_buttons callback
function action_woocommerce_order_item_add_action_buttons( $order ) {
$orderID = $order->ID;
//check if this is the admin manual order creation
if(get_post_status($orderID) == "auto-draft" && get_post_type($orderID) == "shop_order")
{
foreach( $order->get_items() as $item_id => $item )
{
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product_quantity = $item->get_quantity();
if($variation_id == 0)
{
$product = wc_get_product($product_id);
wc_update_product_stock($product, $product_quantity, 'increase');
}
else
{
$variation = wc_get_product($variation_id);
wc_update_product_stock($variation, $product_quantity, 'increase' );
}
// The text for the note
$note = __("Stock incremented due to the auto draft post type. Stock for each item will be decremented when this order created.");
// Add the note
$order->add_order_note( $note );
}
}
};
// add the action
add_action( 'woocommerce_order_item_add_action_buttons', 'action_woocommerce_order_item_add_action_buttons', 10, 1 );
Second Part: Reduce stocks of items that added if order is created.
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 10, 2 );
function woocommerce_process_shop_order ( $post_id, $post ) {
$order = wc_get_order( $post_id );
//check if this is order create action, not an update action
if(get_post_status($post_id) == "draft" && get_post_type($post_id) == "shop_order")
{
foreach( $order->get_items() as $item_id => $item )
{
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product_quantity = $item->get_quantity();
if($variation_id == 0)
{
$product = wc_get_product($product_id);
wc_update_product_stock($product, $product_quantity, 'decrease');
}
else
{
$variation = wc_get_product($variation_id);
wc_update_product_stock($variation, $product_quantity, 'decrease' );
}
// The text for the note
$note = __("Stock decremented for all items in this order.");
// Add the note
$order->add_order_note( $note );
}
}
}
Tested and works fine. I hope this will help you. Have a good day.

WordPress SEO plugin only visible to specific user

I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

Resources