I have the following custom form with code added to the functions.php file:
* ----------------------------------Checkout Field Stor data Start----------------------------------*/
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 ( ! empty( $_POST['account_per_title'] ) ) {
update_post_meta( $order_id, ' Account Title', sanitize_text_field( $_POST['account_per_title'] ) );
}
if ( ! empty( $_POST['account_fname'] ) ) {
update_post_meta( $order_id, 'Account First Name', sanitize_text_field( $_POST['account_fname'] ) );
}
if ( ! empty( $_POST['account_mname'] ) ) {
update_post_meta( $order_id, 'Account Middle Name', sanitize_text_field( $_POST['account_mname'] ) );
}
if ( ! empty( $_POST['account_lname'] ) ) {
update_post_meta( $order_id, 'Account Last Name', sanitize_text_field( $_POST['account_lname'] ) );
}
And here in the template I want to display the name of the person filling in the order:
<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html ($order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Order number */ ?>
<p><?php printf( esc_html__( 'Just to let you know — we\'ve received your order #%s, and it is now being processed:', 'woocommerce' ), esc_html( $order->get_order_number() ) ); ?></p>
Can anyone help with a solution?
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient = get_post_meta( $order->id, 'Account Email', true ) ;
return $recipient;
}
Related
I'm using this snippet below to slightly modify Woocommerce's added to cart message. It redirects to the cart page from product pages. I'd like to add the product's thumbnail image to that Woocommerce message notification to show it clearer to the customer what has been added to the cart. Any solutions? I've tried a bunch of different ways, but no success.
function ace_add_to_cart_message_html( $message, $products ) {
$count = 0;
$titles = array();
foreach ( $products as $product_id => $qty ) {
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
$count += $qty;
}
$titles = array_filter( $titles );
$added_text = sprintf( _n(
'%s has been added to your cart.', // Singular
'%s are added to your cart.', // Plural
$count, // Number of products added
'woocommerce' // Textdomain
), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s %s', esc_url( wc_get_checkout_url() ), esc_html__( 'Proceed to checkout', 'woocommerce' ), esc_html( $added_text ) );
return $message;
}
You can use wc_add_to_cart_message action hook. try the below code.
function add_product_image_wc_add_to_cart_message_html( $message, $product_id ){
if ( has_post_thumbnail( $product_id ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );
endif;
$message = sprintf( '<img src="'.$image[0].'" style="height: 50px;width: 50px;float: left;margin-right: 10px;" />%s', $message );
return $message;
}
add_filter( 'wc_add_to_cart_message', 'add_product_image_wc_add_to_cart_message_html', 10, 2 );
Tested and Works
woocommerace after checkout field is no validate ...when un check create account its still ask for full fill and validation error i want when i checked create account then this validation will work
before un checked why this is asking for validation hope got my point
function
public function wooc_create_dobc_fields() {
$birthday_label = get_option( 'label_birthday' );
$birthday_required = get_option( 'required_fields_dobc' );
if ( empty( $birthday_label ) ) {
$birthday_label = 'Enter Your Date Of Birthday';
}
$user_id = get_current_user_id();
$show_date = get_user_meta( $user_id, 'dobc_date_field', true );
get_user_meta( $user_id, 'dobc_date_field', true );
?>
<div class="create-account">
<p>
<label for="label_birthday"><?php echo esc_attr( $birthday_label, 'dobc' ); ?>
<?php if ( 'yes' === $birthday_required ) : ?>
<span class="required" style="color:red;">*</span>
<?php endif; ?>
</label>
<?php $required = ( 'yes' === $birthday_required ) ? 'required' : ''; ?>
<input type="date" class="input-text" name="dobc_date_field" id="date of birthd " value="<?php echo esc_html( $show_date ); ?>" <?php echo esc_html( $required ); ?> />
<?php wp_nonce_field( 'birthday_nonce_action_checkout', 'birday_fields_checkout_nonce' ); ?>
</p>
</div>
<div class="clear"></div>
<?php
}
hooks
add_action( 'woocommerce_register_form', array( $this, 'wooc_create_dobc_fields' ) );
add_action( 'woocommerce_checkout_process', array( $this,'my_custom_checkout_field_process' ) );
call back validation
public function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( isset($_POST['dobc_date_field'] ) && empty($_POST['dobc_date_field'] ))
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}
i got the solution from my team leader
hooks
if ( 'yes' === $birthday_required ) {
if ( 'yes' == get_option( 'woocommerce_enable_myaccount_registration' ) ) {
add_action( 'woocommerce_checkout_process', array( $this, 'my_custom_checkout_field_process' ) );
call back function
public function my_custom_checkout_field_process() {
if ( isset( $_POST['birday_fields_checkout_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['birday_fields_checkout_nonce'], 'birthday_nonce_action_checkout' ) ) )
) {
print 'Sorry, your nonce did not verify.';
exit;
}
// Check if set, if its not set add an error.
if ( isset( $_POST['createaccount'] ) && empty( $_POST['dobc_date_field'] ) ) {
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}
}
Following code I tried out But it is not save value of custom Fileds.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' );
}
In above I have add code it is display when posttype is product
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : ”;
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
In above code it will added the metabox with textbox and if value in metabox then it is display
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
}
I want to save data in a wp_postmeta table. Above code i have tried out. I am beginner in wordpress.Can any give me suggestion ?
Use This code for creating custom fields for post.
class Rational_Meta_Box {
private $screens = array(
'post',
);
private $fields = array(
array(
'id' => 'custom-field-1',
'label' => 'custom field 1',
'type' => 'text',
),
array(
'id' => 'custom-field-2',
'label' => 'custom field 2',
'type' => 'text',
),
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}
/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
'my-custom-fields',
__( 'my custom fields', 'wordpress' ),
array( $this, 'add_meta_box_callback' ),
$screen,
'advanced',
'high'
);
}
}
/**
* Generates the HTML for the meta box
*
* #param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
echo 'its for custom fields for post typle';
$this->generate_fields( $post );
}
/**
* Generates the field's HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = '';
foreach ( $this->fields as $field ) {
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
$db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
switch ( $field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$field['type'] !== 'color' ? 'class="regular-text"' : '',
$field['id'],
$field['id'],
$field['type'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
/**
* Hooks into WordPress' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
return $post_id;
$nonce = $_POST['my_custom_fields_nonce'];
if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field['id'] ] ) ) {
switch ( $field['type'] ) {
case 'email':
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
break;
case 'text':
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
break;
}
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkbox' ) {
update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
}
}
}
}
new Rational_Meta_Box;
So it will create custom fields in your post type and also saved it too.
follow this for the references https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/
Just replace your add_action( 'save_post', 'cd_meta_box_save' ); function and put below code.
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Full code like ( it's working fine and also save custom field in postmeta table)
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high' );
}
function cd_meta_box_cb( $product)
{
$values = get_post_custom( $product->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : '';
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
}
}
Take a look at Pods.
It is a very powerful plugin for customization.
http://pods.io/
I've added a custom meta field to my Wordpress post type. Everytime I save, the fields don't hold the information (however the information is saved to the database).
My code:
function add_post_meta() {
add_meta_box(
'my_meta_box',
__( 'Metabox', 'framework' ),
'meta_box_content',
'post_type',
'advanced',
'high'
);
}
add_action( 'add_meta_boxes', 'add_post_meta' );
function virtual_merchant_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'meta_box_content_nonce' );
echo '<table><tr>';
echo '<td><label for="input_value">Enter Input Value:</label></td>';
echo '<td><input type="text" id="input_value" name="input_value" /></td>';
echo '</tr></table>';
}
function meta_box_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST['meta_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
$input_value = $_POST['input_value'];
update_post_meta( $post_id, 'input_value', $input_value );
}
add_action( 'save_post', 'metat_box_save' );
Get the input value using get_post_meta(), and then add it to the value="" attribute of the text input field.
function virtual_merchant_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'meta_box_content_nonce' );
$input_value = get_post_meta( $post->ID, 'input_value', true);
echo '<table><tr>';
echo '<td><label for="input_value">Enter Input Value:</label></td>';
echo '<td><input type="text" id="input_value" name="input_value" value="' . $input_value . '" /></td>';
echo '</tr></table>';
}
I need to change the link code that is being used for external products for woo commerce.
This is the code that generates the product image:
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.3
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $woocommerce;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$attachment_count = count( get_children( array( 'post_parent' => $post->ID, 'post_mime_type' => 'image', 'post_type' => 'attachment' ) ) );
if ( $attachment_count != 1 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
So I just want to change out the image link to the external link url so if someone clicks on the picture, they will go to the external link instead of a blow up of the image
The code to do so is within the add to cart code, but I don't know how to apply it to the image:
<?php
/**
* Loop Add to Cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
?>
<?php if ( ! $product->is_in_stock() ) : ?>
<?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?>
<?php else : ?>
<?php
$link = array(
'url' => '',
'label' => '',
'class' => ''
);
$handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->product_type, $product );
switch ( $handler ) {
case "variable" :
$link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
break;
case "grouped" :
$link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
break;
case "external" :
$link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
break;
default :
if ( $product->is_purchasable() ) {
$link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
$link['label'] = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
$link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
} else {
$link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
}
break;
}
echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('%s', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
?>
Sorry I'm such a nube, but I can't figure out how to get the link code the same as the 'add to cart' link code, thanks for the help!
Why not copy product-image.php to your theme or template (this will overwrite the Woocommerce default) and then change the following:
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
Where it says
<a href="%s"
Replace %s with your link, or you can register a custom post field and have a different link for each of your products.
You can ignore the "add to cart" code if this is all you are trying to accomplish. I might have misunderstood - so let me know if it's still unclear.