I have tried lots of hooks but did not able to change the position of quantity field in woo-commerce single product page. So please let me know how can I move quantity box above the 'color'. Please have a
look at screenshot for more details:
screenshot:-
https://prnt.sc/t9ri39
You can create a child theme and copy the file
woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php
to:
themes/your-child-theme/woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php.
So basically the same file structure but without the templates folder. More information about templates here: WooCommerce template structure
After you've copied the file you can remove the quantity field from it by cutting out line 17 until 29:
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
Then add the quantity field again before the variations form with a small code snippet:
add_action( 'woocommerce_before_variations_form', 'add_quantity_field_before_variations_form', 10 );
function add_quantity_field_before_variations_form() {
global $product;
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
do_action( 'woocommerce_after_add_to_cart_quantity' );
}
Add this code snippet to the functions.php of your child theme.
Related
i have following issue with woocommerce/dokan:
I created a custom field for product variation:
custom field in wordpress backend
It works fine, the values are storing and i can change the value at the backend. Therefor i added following lines of code into function.php of theme file:
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
}
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
}
// Add New Variation Settings
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
/**
* Add custom fields for variations
*
*/
function load_variation_settings_fields( $variations ) {
// duplicate the line for each field
$variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
return $variations;
}
So far, so good. I can set a value and the value can be saved over the backend.
Now i want to change the custom field for this product variation in dokan vendor dashboard. I duplicated html-product-variation.php file into child theme and added the new field:
dokan vendor dashboard product variation
with following code:
<div>
<p class="dokan-form-group">
<label><?php esc_html_e( 'Custom Textfield', 'dokan' ); ?></label>
<textarea class="dokan-form-control" name="_text_field[<?php echo esc_attr( $loop ); ?>]" rows="3" style="width:100%;"><?php echo isset( $variation_data['_text_field'] ) ? esc_textarea( $variation_data['_text_field'] ) : ''; ?></textarea>
</p>
</div>
The problem is, that the value, which i set in the backend isn't shown and if i inspect the variation_data object there is no key in the array which says "_text_field". How can i store/update variation_data object?
I hope it's understandable which issue i'm facing here and look forward to some help :)
Best wishes,
Florian
I'm making a shop page that's showing every variation of the product as single products. Using this plugin to achieve that. And I need the variation description to show up, too. However, I have no idea how to achieve this. I know there's a woocommerce_template_single_excerpt function, but that shows Attribute information, instead. So, this:
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_single_excerpt', 20 );
Gives back Attributes titles (i.e. Size, Color). But is there any way I can pull out the description that's stored in every single variation inside the product?
Thanks a bunch in advance!
I found a workaround, in a way, for anyone who runs into the same issue.
Because I had no idea how to access variation description, I created a new field called Shop Description:
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => "shop_description{$loop}",
'name' => "shop_description[{$loop}]",
'value' => get_post_meta( $variation->ID, 'shop_description', true ),
'label' => __( 'Shop Description', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'Shop description.', 'woocommerce' ),
'wrapper_class' => 'form-row form-row-full',
)
);
}
function save_variation_settings_fields( $variation_id, $loop ) {
$text_field = $_POST['shop_description'][ $loop ];
if ( ! empty( $text_field ) ) {
update_post_meta( $variation_id, 'shop_description', esc_attr( $text_field ));
}
}
function load_variation_settings_fields( $variation ) {
$variation['shop_description'] = get_post_meta( $variation[ 'variation_id' ], 'shop_description', true );
return $variation;
}
After that, I used a custom function and add_action to show it on the Shop/Archive page. You'll have to play around with the priority, as that's not sorted out here.
add_action( 'woocommerce_after_shop_loop_item_title', 'show_woocommerce_product_excerpt', 35 );
function show_woocommerce_product_excerpt() {
global $post;
if ( is_home() || is_shop() || is_product_category() || is_product_tag() ) {
echo '<p class="excerpt">';
echo get_post_meta( $post->ID, 'shop_description', true );
echo '</p>';
}
}
And then, just add a description for each variation in your Products and it'll show up on Shop page without any issues.
I created a custom field for the ecommerce admin order item meta. Everything is fine.
I would like to display the Custom Fields MetaValue on My Account's Order Details page. But nothing is being displayed. Based on Save Order item custom field in Woocommerce Admin order pages answer code, this is my attempt
function add_order_item_custom_field( $item_id, $item ) {
woocommerce_wp_text_input( array(
'id' => 'v_number'.$item_id,
'label' => __( 'V Number : ', 'ctxt' ),
'description' => __( 'Enter the title of your custom text field.', 'ctxt' ),
'desc_tip' => true,
'class' => 'v_number_class',
'value' => wc_get_order_item_meta( $item_id, '_v_number' ),
) );
}
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_custom_field', 10, 2 );
// Save the custom field value
function save_order_item_custom_field_value( $post_id, $post ){
$order = wc_get_order( $post_id );
foreach ( $order->get_items() as $item_id => $item ) {
if( isset( $_POST['v_number'.$item_id] ) ) {
$item->update_meta_data( '_v_number', sanitize_text_field( $_POST['v_number'.$item_id] ) );
$item->save();
}
}
$order->save();
}
add_action('save_post', 'save_order_item_custom_field_value' );
// Display meta my account view order page
printf (
'<p><a>V Number : <strong>' . $order->get_meta('_v_number') . '</strong></a></p>'
);
it should print your custom order meta value.
add_action( 'woocommerce_view_order', 'print_custom_order_meta' );
function print_custom_order_meta( $order_id ){
$order = wc_get_order($order_id);
foreach( $order->get_items() as $item ) {
echo 'V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}
}
edit
for need to show metavalue after each product item you need to hook with a different action like this.
add_action('woocommerce_order_item_meta_end', 'show_order_meta', 11, 3);
function show_order_meta( $item_id, $item, $order ) {
echo '<br>V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}
I need help with dokan actions on Orders page. Currently here i have two actions, to mark order complete or processing. What I want is to create action to mark order cancelled and on-hold.
I have accessed file that contains these two actions and they are in file Ajax.php:
add_action( 'wp_ajax_dokan-mark-order-complete', array( $this, 'complete_order' ) );
add_action( 'wp_ajax_dokan-mark-order-processing', array( $this, 'process_order' ) );
Is there a way to define similar action:
add_action( 'wp_ajax_dokan-mark-order-cancelled', array( $this, 'cancel_order' ) );
?
I already made a custom action that mark the order as shipped(custom status):
first you can add the below to include/Ajax.php > in the __construct function.
add_action( 'wp_ajax_dokan-mark-order-shipped', array( $this, 'ship_order' ) );
Then you have to include the custom function (in the same file):
public function ship_order() {
if ( ! is_admin() ) {
die();
}
if ( ! current_user_can( 'dokandar' ) || 'on' != dokan_get_option( 'order_status_change', 'dokan_selling', 'on' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'dokan-lite' ) );
}
if ( ! check_admin_referer( 'dokan-mark-order-shipped' ) ) {
wp_die( esc_html__( 'You have taken too long. Please go back and retry.', 'dokan-lite' ) );
}
$order_id = ! empty( $_GET['order_id'] ) ? intval( $_GET['order_id'] ) : 0;
if ( ! $order_id ) {
die();
}
if ( ! dokan_is_seller_has_order( dokan_get_current_user_id(), $order_id ) ) {
wp_die( esc_html__( 'You do not have permission to change this order', 'dokan-lite' ) );
}
$order = dokan()->order->get( $order_id );
$order->update_status( 'wc-completed' );
wp_safe_redirect( wp_get_referer() );
die();
}
then make sure that you modify the URL :
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=dokan-mark-order-shipped&order_id=' . dokan_get_prop( $order, 'id' ) ), 'dokan-mark-order-shipped' ),
Please let me know if you need any help
I am using woocommerce on my wordpress site.
I am selling paintings. The products are paintings.
I have a list of artists as posts. Each artist is one post.
I would like to connect the posts and products so I can show the artist's name on the painting page and the user can click on the name and it takes them to the artist post.
How do I do this?
This is an example of how to add a custom field in WooCommerce product general tab. Since artists are posts ( category is not specified ), it will collect links for all posts and place them in a dropdown. The value of this field will be visible on the single product page below the price ( you can open the content-single-product.php file in WooCommerce theme, to see the actions for single product template, and functions attached, and change the priority of woocommerce_product_artist function if you want to change the place where the link will appear ).
<?php
add_action( 'admin_init', 'woocommerce_custom_admin_init' );
function woocommerce_custom_admin_init() {
// display fields
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_add_custom_general_fields' );
// save fields
add_action( 'woocommerce_process_product_meta', 'woocommerce_save_custom_general_fields' );
}
function woocommerce_add_custom_general_fields() {
// creating post array for the options ( id => title)
$posts = array( '' => __( 'Select Artist' ) );
array_walk( get_posts( array( 'numberposts' => -1 ) ), function( $item ) use ( &$posts ) {
$posts[ $item->ID ] = $item->post_title;
} );
// creating dropdown ( woocommerce will sanitize all values )
echo '<div class="options_group">';
woocommerce_wp_select(
array(
'id' => '_artist',
'label' => __( 'Artist' ),
'options' => $posts
)
);
echo '</div>';
}
function woocommerce_save_custom_general_fields( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// validate id of artist page and save
if ( isset( $_POST['_artist'] ) ) {
$value = filter_input( INPUT_POST, '_artist', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0 ) ) );
update_post_meta( $post_id, '_artist', $value );
}
}
add_action( 'init', 'woocommerce_custom_init' );
function woocommerce_custom_init() {
// hook the woocommerce_product_artist function on to woocommerce_single_product_summary action ( priority 15 )
add_action( 'woocommerce_single_product_summary', 'woocommerce_product_artist', 15 );
}
function woocommerce_product_artist() {
global $post;
// get the artist page id and show in template ( if exists )
$artist_id = get_post_meta( $post->ID, '_artist', true );
if ( $artist_id ) :
?>
<div class="product-artist"><?php echo get_the_title( $artist_id ); ?></div>
<?php endif;
}
?>