Can you tell me why I can iupload picture and modify it in My account page, everything works fine, and, doesn't work on my Checkout page (I can display the picture I sent before in My Account) ?
// Display
function action_woocommerce_edit_account_form()
{
// Get current user id
$user_id = get_current_user_id();
// Get attachment id
$attachment_id = get_user_meta($user_id, 'image', true);
// True
if ($attachment_id) {
$original_image_url = wp_get_attachment_url($attachment_id);
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
}
add_action('woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form');
add_action('woocommerce_checkout_before_customer_details', 'action_woocommerce_edit_account_form');
// Add field
function action_woocommerce_edit_account_form_start()
{
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="image"><?php esc_html_e('Photo d\'identité', 'woocommerce'); ?> <span class="required">*</span></label>
<input type="file" class="woocommerce-Input" name="image" accept="image/x-png,image/gif,image/jpeg">
</p>
<?php
}
add_action('woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start');
add_action('woocommerce_checkout_before_customer_details', 'action_woocommerce_edit_account_form_start');
// Validate
function action_woocommerce_save_account_details_errors($args)
{
if (isset($_FILES['image']) && $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
$args->add('image_error', __('Please provide a valid image', 'woocommerce'));
}
}
add_action('woocommerce_save_account_details_errors', 'action_woocommerce_save_account_details_errors', 10, 1);
// Display the avatar on account pages
function action_woocommerce_after_edit_account_form($user)
{
$user_id = $user->ID;
$attachment_id = get_user_meta($user_id, 'image', true);
if ($attachment_id) {
$original_image_url = wp_get_attachment_url($attachment_id);
$image = wp_get_attachment_image($attachment_id, 'thumbnail');
printf('<div>%s</div>', $image);
}
}
add_action('woocommerce_after_edit_account_form', 'action_woocommerce_after_edit_account_form', 10, 1);
// Display the avatar on checkout page
function action_woocommerce_review_order_before_submit()
{
$user_id = get_current_user_id();
$attachment_id = get_user_meta($user_id, 'image', true);
if ($attachment_id) {
$original_image_url = wp_get_attachment_url($attachment_id);
$image = wp_get_attachment_image($attachment_id, 'thumbnail');
printf('<div>%s</div>', $image);
}
}
add_action('woocommerce_review_order_before_submit', 'action_woocommerce_review_order_before_submit');
// Save
function action_woocommerce_save_account_details($user_id)
{
if (isset($_FILES['image'])) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
// Define the custom upload folder
function wp_set_custom_upload_folder($uploads)
{
$uploads['path'] = $uploads['basedir'] . '/my-custom-avatar';
$uploads['url'] = $uploads['baseurl'] . '/my-custom-avatar';
if (!file_exists($uploads['path'])) {
mkdir($uploads['path'], 0755, true);
}
return $uploads;
}
add_filter('upload_dir', 'wp_set_custom_upload_folder');
// Upload and attach the new image to the user's account
$attachment_id = media_handle_upload('image', 0);
if (is_wp_error($attachment_id)) {
update_user_meta($user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message());
} else {
$old_attachment_id = get_user_meta($user_id, 'image', true);
wp_delete_attachment($old_attachment_id);
update_user_meta($user_id, 'image', $attachment_id);
}
}
}
add_action('woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1);
add_action('woocommerce_checkout_update_customer', 'action_woocommerce_save_account_details', 10, 1);
// Add enctype to form to allow image upload
function action_woocommerce_edit_account_form_tag()
{
echo 'enctype="multipart/form-data"';
}
add_action('woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag');
add_action('woocommerce_checkout_update_customer', 'action_woocommerce_edit_account_form_tag');
Thank you!!
If you can fin the right action to do, I will be be greatful :)) Add_action works everywhere except in Checkout page.
I tried lot of things, I probably don't know the right thing to do whe I save file field of the avatar.
source https://stackoverflow.com/questions/75784443/upload-avatar-user-pic-on-checkout-woocommerce-page
No comments:
Post a Comment