Wondor how to show attributes in product summary (Woocommerce)

Just add this small snippet and there you go….

//show attributes after summary in product single view
add_action('woocommerce_single_product_summary', function() {
//template for this is in storefront-child/woocommerce/single-product/product-attributes.php
global $product;
echo $product->list_attributes();
}, 25);

 

If you want to show only minimum price of variable product you can do something like this

//Show only minimum price of variable product
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
$price .= wc_price($product->get_price());
return $price;
}

Advertisements

How to add/edit/delete custom checkout fields and send them into email in your woocommerce shop?

/**
 * Ordering custom checkout fields
 */

// Billing Fields.
add_filter( 'woocommerce_billing_fields' , 'woocommerce_billing_fields_custom' );

function woocommerce_billing_fields_custom( $fields ) {

    //Set existing fields

    $fields['billing_first_name']['label'] = 'Name of Corresponding Author';
    $fields['billing_phone']['maxlength'] = 10;
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['class'] = array( 'form-row-first' );
    $fields['billing_state']['class'] = array( 'form-row-last' );
    $fields['billing_postcode']['maxlength'] = 6;
    $fields['billing_postcode']['class'] = array( 'form-row-first' );

    //Adding new fields

    $fields['billing_article_title'] = array(
        'label' => __('Article Title', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => true, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('form-row-first')    // add class name
    );

    $fields['billing_article_id'] = array(
        'label' => __('Article Submission Id', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => true, // if field is required or not
        'clear' => true, // add clear or not
        'type' => 'number', // add field type
        'class' => array('form-row-last')    // add class name
    );

    $fields['billing_alternate_phone'] = array(
        'label' => __('Alternate Phone Number', 'woocommerce'), // Add custom field label
        'placeholder' => _x('', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => true, // add clear or not
        'type' => 'number', // add field type
        'class' => array('form-row-last')    // add class name
    );

    //Set Priorities of Your Billing form fields

    $fields['billing_first_name']['priority'] = 10;
    $fields['billing_email']['priority'] = 15;
    $fields['billing_article_title']['priority'] = 20;
    $fields['billing_article_id']['priority'] = 25;
    $fields['billing_phone']['priority'] = 30;
    $fields['billing_alternate_phone']['priority'] = 35;
    $fields['billing_address_1']['priority'] = 40;
    $fields['billing_address_2']['priority'] = 45;
    $fields['billing_country']['priority'] = 60;
    $fields['billing_state']['priority'] = 61;
    $fields['billing_city']['priority'] = 62;
    $fields['billing_postcode']['priority'] = 63;

    return $fields;
}

add_filter("woocommerce_checkout_fields", "order_fields");

  //Now order all fields in the billing form woocommerce checkout

function order_fields($fields) {

    $order = array(
        "billing_first_name",
        "billing_email",
        "billing_article_title",
        "billing_article_id",
        "billing_phone",
        "billing_alternate_phone",
        "billing_address_1",
        "billing_address_2",
        "billing_country",
        "billing_state",
        "billing_city",
        "billing_postcode",
    );
    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;
    return $fields;

}


/**
 * Validate fields if needed
 * In this case we normally validating phone only
 * validating billing phone number as 10 digit only
 */

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error. This one is only requite for companies
    if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_phone'] ))){
        wc_add_notice( "The Phone number should contain only 10 digits"  ,'error' );
    }

    // Check if set and post alternate phone without proper 10 digit

    if (!empty($_POST['billing_alternate_phone'])){
        if ( ! (preg_match('/^[0-9]{10}$/D', $_POST['billing_alternate_phone'] ))){
            wc_add_notice( "The alternate Phone should contain only 10 digits"  ,'error' );
        }
    }

}

/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST['billing_alternate_phone']) update_user_meta( $user_id, '_alternate_phone_number', esc_attr($_POST['billing_alternate_phone']) );
    if ($user_id && $_POST['billing_article_title']) update_user_meta( $user_id, '_article_title', esc_attr($_POST['billing_article_title']) );
    if ($user_id && $_POST['billing_article_id']) update_user_meta( $user_id, '_article_id', esc_attr($_POST['billing_article_id']) );
}
/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['billing_alternate_phone']) update_post_meta( $order_id, '_alternate_phone_number', esc_attr($_POST['billing_alternate_phone']));
    if ($_POST['billing_article_title']) update_post_meta( $order_id, '_article_title', esc_attr($_POST['billing_article_title']));
    if ($_POST['billing_article_id']) update_post_meta( $order_id, '_article_id', esc_attr($_POST['billing_article_id']));
    if ($_POST['billing_first_name']) update_post_meta( $order_id, '_article_author_name', esc_attr($_POST['billing_first_name']));
}
/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {

    $keys[] = 'Name of Corresponding Author';
    $keys[] = 'Article Title';
    $keys[] = 'Article Submission Id';
    $keys[] = 'Alternate Phone Number';
    return $keys;
}

/**
 * Display Order data in thankyou page
 **/
function cloudways_display_order_data( $order_id ){  ?>
    <h2><?php _e( 'Article Information' ); ?></h2>
    <table class="shop_table shop_table_responsive additional_info">
        <tbody>
        <tr>
            <th><?php _e( 'Author Name:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_author_name', true ); ?></td>
        </tr>
        <tr>
            <th><?php _e( 'Article Title:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_title', true ); ?></td>
        </tr>
        <tr>
            <th><?php _e( 'Article Submission ID:' ); ?></th>
            <td><?php echo get_post_meta( $order_id, '_article_id', true ); ?></td>
        </tr>
        </tbody>
    </table>
<?php }
add_action( 'woocommerce_thankyou', 'cloudways_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'cloudways_display_order_data', 20 );

/**
 * @param $order
 * Display order data in admin backend
 */

function cloudways_display_order_data_in_admin( $order ){  ?>
    

' . __( 'Author Name' ) . ':' . get_post_meta( $order->id, '_article_author_name', true ) . '

'; echo '

' . __( 'Article Title' ) . ':' . get_post_meta( $order->id, '_article_title', true ) . '

'; echo '

' . __( 'Article Submission ID' ) . ':' . get_post_meta( $order->id, '_article_id', true ) . '

'; echo '

' . __( 'Alternate Phone Number' ) . ':' . get_post_meta( $order->id, '_alternate_phone_number', true ) . '

';?>
'_article_author_name', 'label' => __( 'Author Name' ), 'wrapper_class' => '_billing_first_name' ) ); ?> '_article_title', 'label' => __( 'Article Title' ), 'wrapper_class' => '_billing_article_title' ) ); ?> '_article_id', 'label' => __( 'Article Submission Id' ), 'wrapper_class' => '_billing_article_id' ) ); ?> '_alternate_phone_number', 'label' => __( 'Alternate Phone Number' ), 'wrapper_class' => '_billing__alternate_phone' ) ); ?>
</div> <?php } add_action( 'woocommerce_admin_order_data_after_order_details', 'cloudways_display_order_data_in_admin' ); /** * @param $post_id * @param $post * Update post meta with new data */ function cloudways_save_extra_details( $post_id, $post ){ update_post_meta( $post_id, '_article_author_name', wc_clean( $_POST[ '_article_author_name' ] ) ); update_post_meta( $post_id, '_article_title', wc_clean( $_POST[ '_article_title' ] ) ); update_post_meta( $post_id, '_article_id', wc_clean( $_POST[ '_article_id' ] ) ); update_post_meta( $post_id, '_alternate_phone_number', wc_clean( $_POST[ '_alternate_phone_number' ] ) ); } add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 ); /** * @param $fields * @param $sent_to_admin * @param $order * @return mixed * Add meta informations into order email */ function cloudways_email_order_meta_fields( $fields, $sent_to_admin, $order ) { $fields['author_name'] = array( 'label' => __( 'Corresponding Author' ), 'value' => get_post_meta( $order->id, '_article_author_name', true ), ); $fields['article_title'] = array( 'label' => __( 'Article Title' ), 'value' => get_post_meta( $order->id, '_article_title', true ), ); $fields['article_submission_id'] = array( 'label' => __( 'Article Submission ID' ), 'value' => get_post_meta( $order->id, '_article_id', true ), ); $fields['alternate_phone_number'] = array( 'label' => __( 'Alternate Phone Number' ), 'value' => get_post_meta( $order->id, '_alternate_phone_number', true ), ); return $fields; } add_filter('woocommerce_email_order_meta_fields', 'cloudways_email_order_meta_fields', 10, 3 );

Want your own dashboard menu items? Just take a look on these woocommerce hook

Since WooCommerce v2.6.0, the My Account area in WooCommerce has utilised what is called “endpoints”. This means that there is only 1 page set up in WordPress (something like “/my-account”), and the subpages of that are dynamic loaded (like “/my-account/orders”). It’s a good method for WooCommerce to have implemented, as it means you do not need to manage as many pages in your WordPress admin; the contents of that page is loaded in dynamically based on the “endpoint” (“orders”, in our example).

This does make adding a new subpage to the My Account area a little more complicated, but it’s definitely possible! Take a look at how it’s done.

Note: I’ll be writing this in standard functions which you could add to your theme’s functions.php file, but I’d recommend creating a plugin for the functionality and doing it within a class.

Add An “Information” Link To The My Account Navigation

Firstly, let’s add an information link to the My Account navigation.

  • The woocommerce_account_menu_items filter accepts 1 parameter: an array of navigation items.
  • In the iconic_account_menu_item() we’re adding our “Information” link to the end of the $items array. We’re using a key/value pair; the array key is the endpoint URI (“information”), and the value is a translatable string of the link title (“Information”).

Add A “My Account” Endpoint

For this example, we’ll add a new page called “Information”, under the URI of my-account/information. This means our endpoint is simply information, as per our custom menu link above.

  • We’re adding a function called iconic_add_my_account_endpoint on the init action.
  • We’re using add_rewrite_endpoint() to add our endpoint. This will require you permalinks to be refreshed (just visit SettingsPermalinks).
  • add_rewrite_endpoint() accepts 2 parameters. The first is our endpoint URI, and the second is where the endpoint will be available. As “My Account” is a page, we’ve assigned our endpoint to EP_PAGES. You can view all available “Places” on the codex.

After this is added, the /my-account/information URL should return the normal account dashboard, and not a 404 page.

Add Content To Our New Endpoint

Rather than using a custom template, I think it’s better to use the hooks available. This way we can keep the default account layout, including the sidebar navigation. WooCommerce provide a custom hook for your endpoint, which can be accessed like so:

We’re using the woocommerce_account_information_endpoint action. This is a custom action that you can use for any endpoint you create by formatting it like so woocommerce_account_{your-endpoint-slug}_endpoint.

Add An “Is Endpoint?” Helper

It could be useful for us to add a helper function to check whether the currently viewed page is an endpoint. I couldn’t see any native functions in WordPress that do this (correct me if I’m wrong?), so I wrote this simple function for that purpose.

So now we can use if( iconic_is_endpoint('information') ) { ... } to check whether the current page is our endpoint. Note: You can pass any endpoint into the function.

Conclusion

That should get you started! There’s obviously a few lines of code to add, but once you’ve done it, it’s fairly straightforward to modify and evolve. Let me know if you use this tutorial on your WooCommerce site. I’d be interested to know what tabs you’ve added.

Confused ? How to show taxonomies terms of a posts into a wordpress page

Just add this code where you want to post categories with links

// your taxonomy name
$tax = 'post_tag';

// get the terms of taxonomy
$terms = get_terms( $tax, [
  'hide_empty' => false, // do not hide empty terms
]);

// loop through all terms
foreach( $terms as $term ) {

  // if no entries attached to the term
  if( 0 == $term->count )
    // display only the term name
    echo '<h4>' . $term->name . '</h4>';

  // if term has more than 0 entries
  elseif( $term->count > 0 )
    // display link to the term archive
    echo '<h4><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></h4>';

}

Dynamically populated post fields ( Gravity Forms )

There are two ways you might want to populate a drop down field. The first way is pre-selecting a drop down option when the form is displayed. The second way is dynamically populating the options that are available in a select (aka drop down) field. This walkthrough will teach you how to configure the latter. Continue reading “Dynamically populated post fields ( Gravity Forms )”