Woocommerce Cash On Delivery Add Custom Field - woocommerce

How can I add a custom textarea field right below cah on delivery so when you click on the cash on delivery radio button a textarea appears to enter details that get saved into the order?

Copy checkout/payment-method.php file to your theme directory woocommerce folder. i.e your-theme/woocommerce/checkout/payment-method.php
change the content li tag with the following code:
<li class="wc_payment_method payment_method_<?php echo $gateway->id; ?>">
<input id="payment_method_<?php echo $gateway->id; ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php checked( $gateway->chosen, true ); ?> data-order_button_text="<?php echo esc_attr( $gateway->order_button_text ); ?>" />
<label for="payment_method_<?php echo $gateway->id; ?>">
<?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
</label>
<?php if ( ( $gateway->has_fields() || $gateway->get_description() ) && $gateway->id != "cod" ) : ?>
<div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
<?php $gateway->payment_fields(); ?>
</div>
<?php endif; ?>
<?php if ( $gateway->id == "cod" ) : ?>
<div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
<?php $gateway->payment_fields(); ?>
<label>Custom Field</label>
<input type="text" name='cod_custom_field'>
</div>
<?php endif; ?>
</li>
Add following code in yout themes functions.php file
/**
* 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 ( ! empty( $_POST['cod_custom_field'] ) ) {
update_post_meta( $order_id, 'COD Custom Field', sanitize_text_field( $_POST['cod_custom_field'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('COD Custom Field').':</strong> ' . get_post_meta( $order->id, 'COD Custom Field', true ) . '</p>';
}
That's it. Hope this help.

Related

Adding a custom field to Dokan shipping settings

I'm trying to add a custom field to the dokan shipping settings... I've tried this code but it breaks the site.
add_filter( 'dokan_shipping_settings_form_top', 'extra_fields', 10, 2);
function extra_fields( $current_user, $profile_info ){
$seller_url= isset( $profile_info['seller_url'] ) ? $profile_info['seller_url'] : '';
?>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address">
<?php _e( 'Website', 'dokan' ); ?>
</label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="seller_url" id="reg_seller_url" value="<?php echo $seller_url; ?>" />
</div>
</div>
<?php
}
//save the field value
add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST['seller_url'] ) ) {
$dokan_settings['seller_url'] = $_POST['seller_url'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
// show on the store page
add_action( 'dokan_store_header_info_fields', 'save_seller_url', 10);
function save_seller_url($store_user){
$store_info = dokan_get_store_info( $store_user);
?>
<?php if ( isset( $store_info['seller_url'] ) && !empty( $store_info['seller_url'] ) ) { ?>
<i class="fa fa-globe"></i>
<?php echo esc_html( $store_info['seller_url'] ); ?>
<?php } ?>
<?php
}
I've tried to use 'dokan_shipping_settings_form_top' but it's not working. I am using the Dokan Regular Shipping (dashboard/settings/regular-shipping) rather than Zone Wise shipping.

The order number belongs to the user or not

I wanted to make a series of changes on the page checkout/thankyou.php woocommerce
if ( is_user_logged_in() ) {
if ( order number Belongs to user ???? ) {
show order details
} else { echo 'not show order details'; }
}
But I do not know how to check whether the order number belongs to the user or not
If the custom number entered belongs to him, show the order details
If the order number does not belong to the user, do not display the order details
I searched the site for about 3 hours but did not find anything, then I decided to ask a question
You can check with user id from order object $order->user_id; and check against the current logged-in user id for that you can use get_current_user_id(). try the below code.
thankyou.php
<?php
/**
* Thankyou page
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/thankyou.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files, and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
$user_id = $order->user_id;
if( is_user_logged_in() && $user_id == get_current_user_id() ){ ?>
<div class="woocommerce-order">
<?php
if ( $order ) :
do_action( 'woocommerce_before_thankyou', $order->get_id() );
?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed"><?php esc_html_e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed-actions">
<?php esc_html_e( 'Pay', 'woocommerce' ); ?>
<?php if ( is_user_logged_in() ) : ?>
<?php esc_html_e( 'My account', 'woocommerce' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', esc_html__( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Order number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<li class="woocommerce-order-overview__date date">
<?php esc_html_e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo wc_format_datetime( $order->get_date_created() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
<li class="woocommerce-order-overview__email email">
<?php esc_html_e( 'Email:', 'woocommerce' ); ?>
<strong><?php echo $order->get_billing_email(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php endif; ?>
<li class="woocommerce-order-overview__total total">
<?php esc_html_e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php if ( $order->get_payment_method_title() ) : ?>
<li class="woocommerce-order-overview__payment-method method">
<?php esc_html_e( 'Payment method:', 'woocommerce' ); ?>
<strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->get_payment_method(), $order->get_id() ); ?>
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', esc_html__( 'Thank you. Your order has been received.', 'woocommerce' ), null ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php endif; ?>
</div>
<?php }else{
echo 'Your custom message here.';
} ?>

Show category description in shop grid

I’d like to be able to display the category description for each of the categories in our shop – both parent and child – either in an overlay below the h4 category title or underneath.
Our theme has a filter which governs this layout, and any attempts I’ve made to adapt it and add an additional div or span by including echo category_description() inside the h4 title tags has been unsuccessful - [https://www.epianos.co.uk/digital-pianos/][1]
<div class="category-grid-item">
<a class="category-img" href="<?php echo esc_url( get_term_link( $c->slug, 'product_cat' ) ); ?>">
<?php
$thumbnail_id = get_term_meta( $c->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'large' );
$image = isset( $image[0] ) ? $image[0] : wc_placeholder_img_src();
if ( isset( $image ) ) {
?>
<img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $c->name ); ?>" />
<?php
}
?>
</a>
<h4 class="category-title">
<?php echo esc_html( $c->name ); ?><span class="count"><?php echo esc_attr( $c->count ); ?></span>
</h4>
</div>
[1]: https://www.epianos.co.uk/digital-pianos/
Try to pass the category id to the category_description function
from documentation:
category_description( int $category )
Retrieves category description.
$category (int) (Optional) Category ID. Defaults to the current category ID.
Your code should be as follows:
<h4 class="category-title">
<?php echo esc_html( $c->name ); ?> <?php echo category_description( $c->term_id ); ?>
<span class="count"><?php echo esc_attr( $c->count ); ?></span>
</h4>

Moving the woocommerce checkout account register

my goal is to move the checkout user registration form to the top of the checkout page (Above the billing form)...
I'm stuck on finding the correct variables and names to hook this form to the place I desire.
Any advice is appreciated! Thank you
You could modify form-billing.php template.
move the following code to the top or the place you want.
<?php if ( ! is_user_logged_in() && $checkout->is_registration_enabled() ) : ?>
<div class="woocommerce-account-fields">
<?php if ( ! $checkout->is_registration_required() ) : ?>
<p class="form-row form-row-wide create-account">
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
<input class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" id="createaccount" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true ) ?> type="checkbox" name="createaccount" value="1" /> <span><?php _e( 'Create an account?', 'woocommerce' ); ?></span>
</label>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_before_checkout_registration_form', $checkout ); ?>
<?php if ( $checkout->get_checkout_fields( 'account' ) ) : ?>
<div class="create-account">
<?php foreach ( $checkout->get_checkout_fields( 'account' ) as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<div class="clear"></div>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_checkout_registration_form', $checkout ); ?>
</div>
<?php endif; ?>

Wordpress WooCommerce add Variation on the Cart

Have long ago created a page and moved the shopping cart button into a sidebar wiget
Now I want to complement a product variants, and as I see it, the selection is not shown to me.
Since this is not inserted in the code.
Now would be my question what would I have to insert with this code that this works.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_display_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
<?php if ( wc_gzd_get_gzd_product( $product )->get_tax_info() ) : ?>
<p class="wc-gzd-additional-info tax-info"><?php echo wc_gzd_get_gzd_product( $product )->get_tax_info(); ?></p>
<?php elseif ( get_option( 'woocommerce_gzd_small_enterprise' ) == 'yes' ) : ?>
<p class="wc-gzd-additional-info small-business-info"><?php echo wc_gzd_get_small_business_product_notice(); ?></p>
<?php endif; ?>
<?php if ( wc_gzd_get_gzd_product( $product )->get_shipping_costs_html() ) : ?>
<p class="wc-gzd-additional-info shipping-costs-info"><?php echo wc_gzd_get_gzd_product( $product )->get_shipping_costs_html();?></p>
<?php endif; ?>
<?php if ( wc_gzd_get_gzd_product( $product )->get_delivery_time_html() ) : ?><i class="fi fa fa-truck" style="font-size:20px;width:20px;height:20px;line-height:20px;color:#ee9b11;"></i>
<?php echo wc_gzd_get_gzd_product( $product )->get_delivery_time_html();?></p>
<?php elseif ( $product->is_type( 'variable' ) ) : ?>
<p class="wc-gzd-additional-info delivery-time-info"></p>
<?php endif; ?>
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
if ( ! $product->is_purchasable() ) {
return;
}
?>
<?php
// Availability
$availability = $product->get_availability();
$availability_html = empty( $availability['availability'] ) ? '' : '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>';
echo apply_filters( 'woocommerce_stock_html', $availability_html, $availability['availability'], $product );
?>
<?php if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
if ( ! $product->is_sold_individually() ) {
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product ),
'input_value' => ( isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 )
) );
}
?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?>
There is a file mini-cart.php which contains the markup for the mini-cart, used by the cart widget. This template can be overridden by copying the plugin file woocommerce/cart/mini-cart.php to yourtheme/woocommerce/cart/mini-cart.php.
Thereafter in between the foreach statement
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
Add
if($cart_item['variation_id'] != 0) {
// Getting attribute size selected value
$attribute_size = $cart_item['variation']['attribute_pa_sizes'];
echo $attribute_size;
}
on the variable 'attribute_pa_sizes' the 'sizes' is the variants slug

Resources