Forum Replies Created

Viewing 15 replies - 16 through 30 (of 114 total)
  • Thread Starter allstarsft

    (@allstarsft)

    Update — Shipping Class. I found another way to code.. the stackoverflow version is abit complicated for me to understand how its done..

    /** Add Shipping Class field into Order Emails - Product Table **/
    // Display Items shipping class name in Order email notification
    add_filter( 'woocommerce_order_item_name', 'shipping_class_order_item_name', 20, 3 );
    function shipping_class_order_item_name( $item_name, $item, $is_visible ) {
        // Targeting email notifications only
        if( is_wc_endpoint_url() ) { 
    		return $item_name;
    	}
    
        $product = $item->get_product();     // Get the WC_Product object (from order item)
    	$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
    	$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
    	
        if( empty( $shipping_class_id ) ) {
            return $item_name; // Return default product title (in case of)
    	}
    
        $label = __( 'Shipping class', 'woocommerce' );
    	
    	// Output the Product Title and the new code which wraps the Shipping Class name
        return $item_name . ' <p class="item-shipping_class" style="margin:12px 10px; color: #D5024F;"><strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
    }
    	

    So currently the priority for — add_filter( ‘woocommerce_order_item_name’, ‘shipping_class_order_item_name’, 20, 3 );

    At 20 it should be appearing after Warranty — which is 10

    add_action( ‘woocommerce_order_item_meta_start’, ‘add_attribute_warranty_to_order_emails’, 10, 3 );

    But in Email preview, its still showing as Shipping Class first — am using Kadence Email Customizer for the Preview.

    Thread Starter allstarsft

    (@allstarsft)

    For PA_warranty, this is what I did — learning from Stackoverflow :

    https://stackoverflow.com/questions/56240214/display-specific-product-attribute-in-woocommerce-email-notifications

    /** Add Attribute - Warranty field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_attribute_warranty_to_order_emails', 10, 3 );
    function add_attribute_warranty_to_order_emails( $item_id, $item, $order ) {
        // Set below your product attribute taxonomy (always starts with "pa_")
        $taxonomy = 'pa_warranty';
    
        // On email notifications
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $product    = $item->get_product();
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
    
            if ( $term_names = $product->get_attribute( $taxonomy ) ) {
                echo '<p class="item-attribute_warranty" style="margin:12px 10px; font-weight:600; color: darkblue">' . $label_name . ': ' . nl2br( $term_names ) . '</p>';
            }
        }
    }

    Though I managed to solve all three …. but there’s a minor issue :

    In Cart / Checkout page — the order of the 3 fields are as follows :

    1. Warranty
    2. Shipping Class
    3. SRP

    But in the Order Emails, Shipping class became first. No matter I changed the numbers of the priority — its still the same. — add_filter( ‘woocommerce_order_item_name’, ‘shipping_class_order_item_name’, 10, 3 );

    #Scratch head….

    Thread Starter allstarsft

    (@allstarsft)

    I think I managed to solve it with this : (not sure why the above — doesn’t work?)

    https://stackoverflow.com/questions/54258286/add-shipping-class-under-items-in-new-order-email-in-woocommerce

    /** Add Shipping Class field into Order Emails - Product Table **/
    
    
    // Setting the email_is as a global variable
    add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
    function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
        $GLOBALS['email_id_str'] = $email->id;
    }
    
    // Display Items shipping class name in Order email notification
    add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 3 );
    function custom_order_item_name( $item_name, $item, $is_visible ) {
        // Targeting email notifications only
        if( is_wc_endpoint_url() ) return $item_name;
    
        // Get the WC_Product object (from order item)
        $product = $item->get_product();
    
        if( $shipping_class_id = $product->get_shipping_class_id() ){
            // Getting the email ID global variable
            $refNameGlobalsVar = $GLOBALS;
            $email_id = $refNameGlobalsVar['email_id_str'];
    
            // Only for All Order emails notification
            if( ! empty($email_id) )  { //&& 'new_order' === $email_id -- for New Order Email only 
                $shipping_class_name = get_term( $shipping_class_id, 'product_shipping_class' )->name;
                $item_name .= '<p class="item-shipping_class" style="margin:12px 10px; font-weight:600; color: #D5024F"> ' . __( 'Shipping class', 'woocommerce' ) . ': ' . $shipping_class_name . '</p>';
            }
        }
    
        return $item_name;
    }
    Thread Starter allstarsft

    (@allstarsft)

    Can any expert help me on this ? I had tried the Shipping Class coding but it didn’t work… haha.

    Coding I used :

    /** Add Shipping Class field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_custom_order_meta_shipping_class_to_email', 10, 3 );
    function add_custom_order_meta_shipping_class_to_email($item_id, $item, $order){
       // On email notifications for line items
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $shipping_class = get_post_meta( $item->get_product_id(), 'product_shipping_class', true );
    		
            if ( ! empty($shipping_class) ) {      
    			printf( '<div>' . __("Shipping Class : %s ", "woocommerce"). '</div>', $shipping_class ); 			
    	    }
        }
    }	
    
    Thread Starter allstarsft

    (@allstarsft)

    @shameemreza

    Thanks for your input on this!

    Seems the Product Meta method should be this link : https://iconicwp.com/docs/woocommerce-custom-fields-for-variations/how-to-add-custom-fields-to-email/

    But above link is for Shops with Variation products.

    IF I’m not wrong, the way I do it is by following this Stackoverflow post –https://stackoverflow.com/questions/63838330/how-to-show-woocommerce-custom-product-meta-in-new-order-emails

    I managed to add SRP custom field into the Order Emails:

    /** Add SRP field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_custom_order_meta_srp_to_email', 10, 3 );
    function add_custom_order_meta_srp_to_email($item_id, $item, $order){
       // On email notifications for line items
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $srp = get_post_meta( $item->get_product_id(), '_srp', true );
    
            if ( ! empty($srp) ) {
                printf( '<div>' .__( "SRP : " . get_woocommerce_currency_symbol() . number_format( round($srp, 2), 2 ) ." / piece.","woocommerce"). '</div>' );
            }
        }
    }	

    Documenting this down for others as well.

    I am also going to add these fields later on :

    1) Shipping Class (product_shipping_class, $shipping_class) and

    2) Attribute – Warranty information (pa_warranty, $warranty).

    Will try out later then update here as well.

    • This reply was modified 1 year ago by allstarsft.
    • This reply was modified 1 year ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    ** Managed to adjust the fields in Cart / Checkout page — this is the actual order I want the Order emails to have as well. Not sure which template I should edit?

    Thread Starter allstarsft

    (@allstarsft)

    Ah!! After some trial and error, I might have found the answer.

    in Cart-shipping.php (Cart Page) — I just indicate TD colspan=”3″ without if Cart and else Checkout statement.

    So eventually the coding is just :

    <tr class="woocommerce-shipping-totals shipping">
    	<th><?php echo wp_kses_post( $package_name ); ?></th>
    	
    	<td colspan="3" data-title="<?php echo esc_attr( $package_name ); ?>">

    Do let me know if this is the correct way?

    Thanks. (Image below — to show it works, really span across 3 cols)

    Thread Starter allstarsft

    (@allstarsft)

    Was wondering which template I should use to allow for TD colspan=”3″ to be added so that Shipping methods shown will span across 3 columns.

    Is it still cart-shipping.php — I just want it in Check out page where the Shipping methods are ?

    • This reply was modified 1 year, 1 month ago by allstarsft.
    • This reply was modified 1 year, 1 month ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    @anastas10s Thanks for your reply. I know how to edit the template files, but need to find out which is the correct file to edit for this issue.

    As per my above post, I had tried but failed. Maybe its the wrong template file? I need this for the Checkout Page — where the shipping methods appear.

    Thanks.

    Note : In fact doing the addition of the coding to cart-shipping.php caused Critical error for the Cart page — Cart Totals section.

    • This reply was modified 1 year, 1 month ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    @shameemreza — After reading through the 3 resources, I am still unable to comprehend — as those seems to only add custom data outside of the product details table.

    SRP, Shipping Class and Attribute – Warranty — these are fields that I require to be in each Product Detail — inside the Product Table. — Ideally should look the same as the Cart/Checkout pages. — ie, these 3 fields are before POS Item No (Product code plugin)

    Below screenshot is the Email Order details — which at present only contain the Product name, Qty, and POS Item No.

    In future, I would also wished to indicate Qty as a column and Unit Price as a column as well. — but those I think I will need to edit the Template separately.

    Thank you.

    • This reply was modified 1 year, 1 month ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    Thank you very much for your assistance. I thought I would just like to learn how to do it. hahahaha.

    I will see what I can do, in the coming week, in terms of donations. ?? Thank you very much…

    Thread Starter allstarsft

    (@allstarsft)

    Thanks will do so.

    Thread Starter allstarsft

    (@allstarsft)

    @grola, thanks for your reply. Have tried Cart-shipping.php as well but hmm.. doesn’t work too . See coding I changed

    	<?php if ( is_cart() ) : ?>
    	<td data-title="<?php echo esc_attr( $package_name ); ?>">
    	else ( is_checkout() ) {
    	<td data-title="<?php echo esc_attr( $package_name ); ?>" colspan="3" > 
    	}
    Thread Starter allstarsft

    (@allstarsft)

    Ok, @grola — I just tried to add the conditionals in — Cart-totals.php but it didn’t work. (I need the colspan in Checkout table only — Order Review)

    <?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
    
    <?php do_action( 'woocommerce_cart_totals_before_shipping' ); ?>
    
    <?php wc_cart_totals_shipping_html(); ?>
    
    <?php do_action( 'woocommerce_cart_totals_after_shipping' ); ?>
    
    <?php elseif ( WC()->cart->needs_shipping() && 'yes' === get_option( 'woocommerce_enable_shipping_calc' ) ) : ?>
    
    <tr class="shipping">
    	<th><?php esc_html_e( 'Shipping', 'woocommerce' ); ?></th>
        if (is_cart()) { 
    	<td data-title="<?php esc_attr_e( 'Shipping', 'woocommerce' ); ?>"><?php woocommerce_shipping_calculator(); ?></td> 
      } elseif ( is_checkout() ) {
      <td colspan="4" data-title="<?php esc_attr_e( 'Shipping', 'woocommerce' ); ?>"><?php woocommerce_shipping_calculator(); ?></td> 
    }
    </tr>
    
    <?php endif; ?>

    if (is_cart()) {

    <td data-title=”<?php esc_attr_e( ‘Shipping’, ‘woocommerce’ ); ?>”><?php woocommerce_shipping_calculator(); ?></td>

    } elseif ( is_checkout() ) {

    <td colspan=”4″ data-title=”<?php esc_attr_e( ‘Shipping’, ‘woocommerce’ ); ?>”><?php woocommerce_shipping_calculator(); ?></td>

    }

    If this coding works, the column should have span across 4 columns and the longer Shipping remarks text would have become one liner instead of 2.

    Note: I have created extra empty columns — to match the top’s as Product name can be long and I have defined Quantity and Unit Price at the top as well.

    • This reply was modified 1 year, 1 month ago by allstarsft.
    • This reply was modified 1 year, 1 month ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    Ah! So add this into the Cart-totals.php file’s said coding?

Viewing 15 replies - 16 through 30 (of 114 total)