Insert class in PHP code for Woocommerce

I am a bit of a beginner in php and I am trying to target 3 text outputs in a php file and give them a unique class so I can style them via css.

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

$customer_id = get_current_user_id();
if ( ! wc_ship_to_billing_address_only() && get_option( 'woocommerce_calc_shipping' ) !== 'no' ) {
$page_title = apply_filters( 'woocommerce_my_account_my_address_title', __('My Addresses:', 'woocommerce') );
$get_addresses    = apply_filters( 'woocommerce_my_account_get_addresses', array(
    'billing' => __( 'Billing Address:', 'woocommerce' ),
    'shipping' => __( 'Shipping Address:', 'woocommerce' )
), $customer_id );
} else {
$page_title = apply_filters( 'woocommerce_my_account_my_address_title', __( 'My Address:', 'woocommerce' ) );
$get_addresses    = apply_filters( 'woocommerce_my_account_get_addresses', array(
    'billing' =>  __( 'Billing Address:', 'woocommerce' )
), $customer_id );
}
$col = 1;
?>

I am trying to target ‘My Addresses:’ as well as ‘Billing Address:’ and ‘Shipping Address:’.

Read More

I have worked with this just a bit before and I never had to add a class inside php code, but surely it must be possible, but I can’t seem to find any info on where exactly to add my div class, p class etc..

Any help would be greatly appreciated!

Thanks a lot,

Vaya

Related posts

2 comments

  1. You can use the woocommerce filters to wrap it with whatever you want. Put something like this in your functions.php:

    function filter_woocommerce_my_account_my_address_title( $var ) { 
        return '<span class="your-class-here">'.$var.'</span>'; 
    }; 
    add_filter( 'woocommerce_my_account_my_address_title', 'filter_woocommerce_my_account_my_address_title', 10, 1 ); 
    
    function filter_woocommerce_my_account_get_addresses( $array, $customer_id ) { 
        $array['billing'] = '<span class="class-name-here">'.$array['billing'].'</span>';
        $array['shipping'] = '<span class="another-class-name-here">'.$array['shipping'].'</span>';
        return $array; 
    }; 
    add_filter( 'woocommerce_my_account_get_addresses', 'filter_woocommerce_my_account_get_addresses', 10, 2 ); 
    

    Remeber that if these elements are unique, the id attribute could be a better option.

    Hope it helps!

  2. if you go to woocommerce/templates/myaccount/my-address.php

    in line 42, you must have code like below:

    <div class="col-<?php echo ( ( $col = $col * -1 ) < 0 ) ? 1 : 2; ?> address">
    

    change code to:

    <div class="col-<?php echo ( ( $col = $col * -1 ) < 0 ) ? 1 : 2; ?> address <?php echo str_replace(" ", "-", $title); ?>">
    

    That will do the trick.

Comments are closed.