I would like to reconstruct the code by working with the names of the product attributes instead of the id variations.
From this code:
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Get items
$items = $order->get_items();
// Set variable
$found = false;
// Set variable
$output = '';
// Loop
foreach ( $items as $item ) {
// Add whatever variation id you want below here.
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
$output = 'Thank you for buy VARIABLE A-9647';
$found = true;
break;
}
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
$output = 'Thank you for buy VARIABLE B-9648';
$found = true;
break;
}
}
// Get payment method
$payment_method = $order->get_payment_method();
// Payment method = basc & found = true
if ( $payment_method == 'bacs' && $found ) {
$output .= ' YOUR PAYMENT IS BACS';
}
// Print result
echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
I would like to reconstruct the code by working with the names of the product attributes instead of the id variations
How to change this code
from
variation_id
to
product name attribute
more exactly these lines:
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 )
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 )
Thanks in advance!
Assuming you mean this?
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Get items
$items = $order->get_items();
// Set variable
$found = false;
// Set variable
$output = '';
// Loop
foreach ( $items as $item ) {
// The WC_Product object
$product = $item->get_product();
// Add whatever attribute you want below here.
if ( !empty( $product->get_attribute( 'pa_kleur' ) ) ) {
$output = 'Thank you...1';
$found = true;
break;
}
if ( !empty( $product->get_attribute( 'pa_jaar' ) ) ) {
$output = 'Thank you...2';
$found = true;
break;
}
}
// Get payment method
$payment_method = $order->get_payment_method();
// Payment method = basc & found = true
if ( $payment_method == 'bacs' && $found ) {
$output .= ' YOUR PAYMENT IS BACS';
}
// Print result
echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
Related
I have written a function that should add discount amounts on the woo cart, depends on product combination and custom fields.
If the action is wp_footer i can see get_post_meta works well.
But if I write action to be woocommerce_cart_calculate_fees seems not to work fine.
Can anyone help me with getting into the right direction? Thanks
Here is my code and screenshot below for it:
add_action('woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees2');
function action_woocommerce_cart_calculate_fees2($cart) {
// if ( is_admin() && ! defined( 'DOING_AJAX' ) )
// return;
//if ( did_action( 'woocommerce_before_calculate_totals' ) >= 1 )
//return;
//$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
//echo $product_id . "<br>";
$is_enable = get_post_meta( $product_id, '_enable_promo_for_current_product', true );
if ($is_enable == 'yes') {
$product_arr[] = $product_id;
}
}
foreach ( $product_arr as $val ) {
$product = wc_get_product($val);
$otstapka = get_post_meta( $val, '_procent_otstapka_promo', true );
if (empty($otstapka)) {
$otstapka = 10;
}
$value = get_post_meta( $val, 'wc_product_ids', true );
if ( ! empty( $value ) ) {
$promo_product = wc_get_product( $value );
$name = $promo_product->name;
$price = $promo_product->get_price();
$sale_price = $price * (1 - ($otstapka / 100));
$otstapka2 = $price - $sale_price;
$product_promo_id = $promo_product->get_id();
if ( check_variation_is_in_cart($val) ) {
$cart->add_fee('Отстъпка за промо пакет', -$otstapka2, false);
}
}
}
}
I am trying to achieve the following thing: Woocommerce additional recipient to new order email based on product variation attribute
However, I am also trying to achieve this for when the product is a simple product, but does contain the attribute.
Still, I cannot figure out how to do this for simple products...
Here is my code so far:
add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
function add_recipient( $recipient, $order )
{
// Additional email recipient
$additional_email1 = "random#gmail.com";
$additional_email2 = "ranndom2#gmail.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
if( $item->get_product_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
// Loop through product attributes set in the variation
foreach( $product->get_attributes() as $taxonomy => $term_slug ){
// comparing attribute parameter value with current attribute value
if ( $term_slug === $term_slug1 ) {
$has_term = true;
$recipient .= ','. $additional_email1;
}
if ( $term_slug === $term_slug2 ) {
$has_term = true;
$recipient .= ','. $additional_email2;
}
}
}
if( $has_term ) break; // stop the main loop
}
return $recipient;
}
However, the code does not return any error whatsoever so I am not sure what I am doing wrong. Can anyone give me a hint please?
I revised your code. try the below code.
function add_recipient( $recipient, $order ){
if( $order ) {
// Additional email recipient
$additional_email1 = "random#gmail.com";
$additional_email2 = "ranndom2#gmail.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
if( $item->get_product_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
if( !empty( $product->get_attributes() ) ){
$product_attributes = $product->get_attributes();
// Loop through product attributes set in the variation
foreach( $product_attributes as $taxonomy => $term_slug ){
$options = $product_attributes[$taxonomy]['options'];
if( !empty( $options ) ){
foreach ( $options as $key => $option ) {
$term = get_term_by( 'ID', $option, $taxonomy );
if( $term && $term->slug == $term_slug1 ){
$has_term = true;
$recipient .= ','. $additional_email1;
}
if( $term && $term->slug == $term_slug2 ){
$has_term = true;
$recipient .= ','. $additional_email2;
}
}
}
}
}
}
if( $has_term ) break; // stop the main loop
}
}
}
add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
I Hope are you okay.
Today I have problems with I open order invoice pdf in admin after products list have components.
I Remove all components as well
I am using woocommerce-pdf-invoices-packing-slips plugin.
Below is my product URL :
https://www.pcguru.lt/produktas/amd-home-guru/
My Invoice PDF screenshot as well.
How can I remove it?
I Searching Google but couldn't Found Anything.
After Code Debugging Step by Step, That's are Found Successfully.
File Path:
/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/documents/abstract-wcpdf-order-document-methods.php
public function get_order_items() {
$items = $this->order->get_items();
$data_list = array();
if( sizeof( $items ) > 0 ) {
foreach ( $items as $item_id => $item ) {
//Find Child Product Of Composite Product
global $wpdb;
$post_id = $wpdb->get_row("SELECT meta_value FROM pcg_woocommerce_order_itemmeta WHERE (meta_key = 'wooco_ids' AND order_item_id = '". $item_id ."')");
if($post_id->meta_value !=''){
$main_id = str_replace('/1/', '', $post_id->meta_value);
$data_ids = explode(",",$main_id);
}
//END Custom Code
$data = array();
// Set the item_id
$data['item_id'] = $item_id;
// Set the id
$data['product_id'] = $item['product_id'];
$data['variation_id'] = $item['variation_id'];
// Compatibility: WooCommerce Composit Products uses a workaround for
// setting the order before the item name filter, so we run this first
if ( class_exists('WC_Composite_Products') ) {
$order_item_class = apply_filters( 'woocommerce_order_item_class', '', $item, $this->order );
}
// Set item name
$data['name'] = apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
// Set item quantity
$data['quantity'] = $item['qty'];
//$data['product_desc'] = $item->get_product(); // Get the WC_Product objec
//echo '<pre>'; print_r($product); echo '</pre>'; die;
// Set the line total (=after discount)
$data['line_total'] = $this->format_price( $item['line_total'] );
$data['single_line_total'] = $this->format_price( $item['line_total'] / max( 1, abs( $item['qty'] ) ) );
$data['line_tax'] = $this->format_price( $item['line_tax'] );
$data['single_line_tax'] = $this->format_price( $item['line_tax'] / max( 1, abs( $item['qty'] ) ) );
$data['tax_rates'] = $this->get_tax_rate( $item, $this->order, false );
$data['calculated_tax_rates'] = $this->get_tax_rate( $item, $this->order, true );
// Set the line subtotal (=before discount)
$data['line_subtotal'] = $this->format_price( $item['line_subtotal'] );
$data['line_subtotal_tax'] = $this->format_price( $item['line_subtotal_tax'] );
$data['ex_price'] = $this->get_formatted_item_price( $item, 'total', 'excl' );
$data['price'] = $this->get_formatted_item_price( $item, 'total' );
$data['order_price'] = $this->order->get_formatted_line_subtotal( $item ); // formatted according to WC settings
// Calculate the single price with the same rules as the formatted line subtotal (!)
// = before discount
$data['ex_single_price'] = $this->get_formatted_item_price( $item, 'single', 'excl' );
$data['single_price'] = $this->get_formatted_item_price( $item, 'single' );
// Pass complete item array
$data['item'] = $item;
// Get the product to add more info
if ( is_callable( array( $item, 'get_product' ) ) ) {
$product = $item->get_product();
} else {
$product = $this->order->get_product_from_item( $item );
}
// Checking fo existance, thanks to MDesigner0
if( !empty( $product ) ) {
// Thumbnail (full img tag)
$data['thumbnail'] = $this->get_thumbnail( $product );
$data['get_parent_id'] = is_callable( array( $product, 'get_parent_id' ) ) ? $product->get_parent_id() : '';
// Set item SKU
$data['sku'] = is_callable( array( $product, 'get_sku' ) ) ? $product->get_sku() : '';
// Set item weight
$data['weight'] = is_callable( array( $product, 'get_weight' ) ) ? $product->get_weight() : '';
// Set item dimensions
$data['dimensions'] = $product instanceof \WC_Product ? WCX_Product::get_dimensions( $product ) : '';
// Pass complete product object
$data['product'] = $product;
} else {
$data['product'] = null;
}
// Set item meta
if (function_exists('wc_display_item_meta')) { // WC3.0+
$data['meta'] = wc_display_item_meta( $item, array(
'echo' => false,
) );
} else {
if ( version_compare( WOOCOMMERCE_VERSION, '2.4', '<' ) ) {
$meta = new \WC_Order_Item_Meta( $item['item_meta'], $product );
} else { // pass complete item for WC2.4+
$meta = new \WC_Order_Item_Meta( $item, $product );
}
$data['meta'] = $meta->display( false, true );
}
if (!in_array($data['product_id'], $data_ids)) {
$data_list[$item_id] = apply_filters( 'wpo_wcpdf_order_item_data', $data, $this->order, $this->get_type());
}
}
}
//echo '<pre>'; print_r($data_list); echo '</pre>'; die;
return apply_filters( 'wpo_wcpdf_order_items_data', $data_list, $this->order, $this->get_type() );
}
Solution Screenshot:
I am very happy because solution are completed.
Also Thanks to all helping guys
My question is exactly this:
- Print text on Thank You page based on product attribute and payment method
I have this code that works perfectly:
add_action( 'woocommerce_thankyou', 'show_custom_text_by_variation_id', 1 );
function show_custom_text_by_variation_id( $order_id ) {
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
// Add whatever variation id you want below here.
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
echo '<br/>Example text - Thank you for buy VARIABLE A-9647 !<br/>';
}
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
echo '<br/>Example text - Thank you for buy VARIABLE B-9648 !<br/>';
}
}
}
Now I would like to return another text only when the condition of product choice is presented together with the type of payment for example bacs.
Example A :
Product purchased - Variable 9647
Selected payment method - Bacs
therefore only in this case the text on Thank You page will result:
Example text - Thank you for buy VARIABLE A-9647 - With Payment Method Bacs!
or
Example B :
Product purchased - Variable 9648
Selected payment method - Bacs
therefore only in this case the text on Thank You page will result:
Example text - Thank you for buy VARIABLE B-9648 - With Payment Method Bacs!
Thanks in advance!
Use: $order->get_payment_method();
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Get items
$items = $order->get_items();
// Set variable
$found = false;
// Set variable
$output = '';
// Loop
foreach ( $items as $item ) {
// Add whatever variation id you want below here.
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
$output = 'Thank you for buy VARIABLE A-9647';
$found = true;
break;
}
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
$output = 'Thank you for buy VARIABLE B-9648';
$found = true;
break;
}
}
// Get payment method
$payment_method = $order->get_payment_method();
// Payment method = basc & found = true
if ( $payment_method == 'bacs' && $found ) {
$output .= ' YOUR PAYMENT IS BACS';
}
// Print result
echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
EDIT
show text on top of page, before the order details
function change_order_received_text( $str, $order ) {
// Get items
$items = $order->get_items();
// Set variable
$found = false;
// Loop
foreach ( $items as $item ) {
// Add whatever variation id you want below here.
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
$str = 'Thank you for buy VARIABLE A-9647';
$found = true;
break;
}
if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
$str = 'Thank you for buy VARIABLE B-9648';
$found = true;
break;
}
}
// Get payment method
$payment_method = $order->get_payment_method();
// Payment method = basc & found = true
if ( $payment_method == 'bacs' && $found ) {
$str .= ' YOUR PAYMENT IS BACS';
}
return $str;
}
add_filter('woocommerce_thankyou_order_received_text', 'change_order_received_text', 10, 2 );
I'm using the code below to modify the WooCommerce Is_Purchasable option so that, item Y is purchasable if item X is added to the cart.
But it gives ajax error when trying to add item Y to the cart.
Here's 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', true ) ) ) {
$cart = $saved_cart['cart'];
} elseif ( is_null( $cart ) ) {
$cart = array();
}
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;
}
// Product Y
global $y_cart_items;
$y_cart_items = 2986;
//Product X
global $x_cart_items;
$x_cart_items = array(
'297'
);
// Step 2
add_filter('woocommerce_is_purchasable', function($is_purchasable, $product) {
global $y_cart_items;
global $x_cart_items;
if( $product->id == $y_cart_items ) {
// make it false
$is_purchasable = false;
// get the cart items object
foreach ( aelia_get_cart_contents() as $key => $item ) {
// do your condition
if( in_array( $item['product_id'], $x_cart_items ) ) {
// Eligible product found on the cart
$is_purchasable = true;
break;
}
}
}
return $is_purchasable;
}, 10, 2);
}, 10);
// Step 3 - Explain customers why they can't add some products to the cart
add_filter('woocommerce_get_price_html', function($price_html, $product) {
if(!$product->is_purchasable() && is_product()) {
$price_html .= '<p>' . __('Add Product X to be able to purchase Product Y.', 'woocommerce') . '</p>';
}
return $price_html;
}, 10, 2);
How do I fix this? Thank you.