Custom Option Cart Woocommerce - wordpress

to everyone !
Someone can help me with my proyect, Is very simple but I have one thing that makes me crazy.
I have this:
every checkbox have a value (additional value price)
I need change the price wen I checked in one, two or all checkbox and save this options to send it to the checkout and email....And show this options to order information in the back-end.
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
$item_name .= $value3.'<div class="lineList">';
$value2 = $cart_item['data']->get_meta('_preciosCata');
if( $value1 = $cart_item['data']->get_meta('_varArrayTitle') ) {
for ($i=0;$i<count($value1); $i++) {
//$item_name .= '<div class="lineOne lorem"><input type="checkbox" name="cart[%s][lorem]" id="addPrice" value="' . $value2[$i] . '"> <span class="custom-field"> ' . $value1[$i] . ' </span></div>';
$item_name .= sprintf( '<div class="lineOne lorem"><input type="checkbox" id="_checkbox' . $i . '" name="_checkbox' . $i . '" value="' . $value2[$i] . '" class="checkedBox url text" /> <span class="custom-field">' . $value3.$value1[$i] . ' </span></div>', $cart_item_key, esc_attr( $values['url'] ) );
}
}
$item_name .= '</div>';
return $item_name;
}
I update the car here but dont save the value:
add_action( 'wp_footer', 'bbloomer_cart_refresh_update_qty' );
function bbloomer_cart_refresh_update_qty() {
if (is_cart()) {
?>
<script>
jQuery('div.woocommerce').on('change', '.checkedBox', function(){
jQuery("[name='update_cart']").prop("disabled", false);
jQuery("[name='update_cart']").trigger("click");
//alert($(this).val());
});
</script>
<?php
}
}
I need help please

Related

How to add nofollow to frontpage only? (Woocommerce products, internal links)

Currently it add nofollow at all products across website, how to make it at is_front_page() only?..
function woocommerce_template_loop_product_title() {
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Change the value of your rel attribute. So, when the user is at the frontpage, rel will have a value of nofollow. Otherwise, the value will be "" empty.
<?php
function woocommerce_template_loop_product_title() {
// Add the $nofollow
$nofollow = "";
if(is_front_page() ){
$nofollow = "nofollow";
}
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '"><a href="' . get_the_permalink() . '" rel='.$nofollow.'>' . get_the_title() . '</a></p>';
?>

Woocommerce - add to cart button and quantity in product list on category page

I have a custom product list on a specific category page.
It looks like this:
Product list category page with missing quantity selection and add to cart button at end.
I have been using the below in functions.php and it has worked fine. Since the last update of WooCommerce it no longer works. The add to cart buttons and quantity fields are not showing anymore.
I get no errors and the fields where the qty and button html goes are blank when I check the html for the page.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
// Only for this product category
if ($wk_cat_value == 1 && is_product_category())
{
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() )
{
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
}
This line is a checkbox I added to the category page to specify if the current category is to use the custom template:
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
Template version Woocommerce files were 2.0.0.
Updated versions are 3.4.0.
Any suggestions greatly appreciated.
Found a solution:
function wk_add_to_cart() {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
// $wk_cat_value is a flag added to this specific category to decide whether or not the add to cart should be added
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
global $product;
if ($wk_cat_value == 1 && is_product_category()) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
echo '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
echo woocommerce_quantity_input( array(), $product, false );
echo '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
echo '</form>';
}
}
}
add_action('woocommerce_after_shop_loop_item','wk_add_to_cart');
Hope that helps.

WooCommerce cart items number based on unique products

Is there any way to change the items in cart number based only on the unique products?
I can’t find anything to start play with. Any help greatly appreciated!
Example:
Pen 3x
Book 2x
Currently :
5 items on Cart
Should be :
2 items on Cart.
Thanks
This is the current code on the theme and I target the kleo-notifications new-alert:
if ( ! function_exists( 'kleo_woo_get_mini_cart' ) ) {
function kleo_woo_get_mini_cart( $just_inner = false ) {
global $woocommerce;
//Enqueue variations script for quick view
wp_enqueue_script( 'wc-add-to-cart-variation' );
$cart_output = "";
$cart_total = $woocommerce->cart->get_cart_total();
$cart_count = $woocommerce->cart->cart_contents_count;
$cart_count_text = kleo_product_items_text( $cart_count );
$cart_has_items = '';
if ( $cart_count != "0" ) {
$cart_has_items = ' has-products';
}
if ( ! $just_inner ) {
$cart_output .= '<li class="menu-item kleo-toggle-menu shop-drop">'
. '<a class="cart-contents js-activated" href="' . wc_get_cart_url() . '" title="' . __( "View Cart", "woocommerce" ) . '">'
. '<span class="cart-items' . $cart_has_items . '"><i class="icon icon-basket-full-alt"></i> ';
if ( $cart_count != "0" ) {
$cart_output .= "<span class='kleo-notifications new-alert'>" . $cart_count . "</span>";
}
add_filter( 'woocommerce_add_to_cart_fragments', 'test_cart_count_fragments', 20, 10 );
function test_cart_count_fragments( $fragments ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$fragments['span.new-alert'] = '<span class="kleo-notifications new-alert">' . count($items). '</span>';
return $fragments;
}

move custom menu in a particular position

I displayed WPML language switcher in my menu, at the bottom as the last item. I want it at the second position of the secondary menu.
How to change my code to achieve that please?
Current code:
/*Display WPML language switcher in the menu */
function wpml144107($menu, $args){
if ( ! is_admin() ):
if( $args->theme_location == 'secondary' ):
if(function_exists('icl_get_languages')):
$languages = icl_get_languages('skip_missing=0&orderby=custom');
if(count($languages) >= 1):
$flags = '<li id="menu-item-lang"><div class="flags_top">';
//foreach((array)$languages as $language):
//echo print_r($languages);
$language = $languages['fr'];
$flags .= '
<span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
</span> /';
$language = $languages['en'];
$flags .= '
<span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
</span>';
//endforeach;
$flags .= '</div></li>';
endif;
return $menu . $flags;
endif;
endif;
endif;
return $menu;
}
add_action('wp_nav_menu_items', 'wpml144107', 50, 2);
At this point:
return $menu . $flags;
The language menu is appended to the menu.
If you change it to:
return $flags . $menu;
It would be at the first position...
But you want to have it at the second one, right? Then it is a little more complicated, you have to split the string containing the menu, I suppose that every element is within a <li>-element:
/* finds the position of the first occurrence of </li>,
so the end of the first element, add 4 because we want the position of
the end of the </li>-tag not the start... */
$splitpos = strpos($menu, '</li>') + 4;
// cut the string up to that position and you get the first element...
$firstelem = substr($menu, 0, $splitpos));
// cut after that position and you get the rest of the menu...
$rest = substr($menu, $splitpos));
// now return the menu with your language menu in between...
return $firstelem . $flags . $rest;
I guess this should work! :)
I have found a way to do it: I split the menu items in an array to be able to insert your language switcher in 2nd position.
I have revisited your code a bit:
// Display WPML language switcher in 2nd position of the menu
function wpml144107($items, $args){
if ( ! is_admin() && $args->theme_location == 'secondary' && function_exists('icl_get_languages') ):
$languages = icl_get_languages('skip_missing=0&orderby=custom');
if( count($languages) == 0 ) return $items; // If there is no languages we return defaults menu items
$html = 'id="menu-item-lang">
<div class="flags_top">
<span class="icl-'. $languages['fr']['language_code'] . ( $languages['fr']['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $languages['fr']['language_code'] . '" href="' . $languages['fr']['url']. '">' . $languages['fr']['language_code'] . '</a>
</span> /';
$html .= '
<span class="icl-'. $languages['en']['language_code'] . ( $languages['en']['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $languages['en']['language_code'] . '" href="' . $languages['en']['url']. '">' . $languages['en']['language_code'] . '</a>
</span>';
$html .= '</div>
';
// Html in between each items (to split the items in an array)
$glue = '</li>
<li ';
// Spliting menu items in an array
$items_array = explode( $glue, $items );
// Counter
$count = 0;
foreach($items_array as $key => $item){
// We insert in 2nd position the language switcher
if($key == 1)
$ordered_items[$count] = $html;
else
$ordered_items[$count] = $item;
$count++;
}
// We set back the array of menu items in a correct html string
$items = implode( $glue, $ordered_items );
endif;
return $items;
}
add_action('wp_nav_menu_items', 'wpml144107', 50, 2);
Code goes in function.php file of your active child theme (active theme or in any plugin file).
Tested and Works in WooCommerce 3+
I solved it using this answer
with this code:
/*Display WPML language switcher in the secondary menu at the second position*/
function add_custom_in_menu( $items, $args )
{
if( $args->theme_location == 'secondary' ):
if(function_exists('icl_get_languages')):
$languages = icl_get_languages('skip_missing=0&orderby=custom');
if(count($languages) >= 1):
$items_array = array();
while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) )
{
$items_array[] = substr($items, 0, $item_pos);
$items = substr($items, $item_pos);
}
$items_array[] = $items;
$customHtml = '<li>bla</li>';
$flags = '<li id="menu-item-lang"><div class="flags_top">';
//foreach((array)$languages as $language):
//echo print_r($languages);
$language = $languages['fr'];
$flags .= '
<span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
</span> /';
$language = $languages['en'];
$flags .= '
<span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' ) .'">
<a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
</span>';
//endforeach;
$flags .= '</div></li>';
// insert custom item after 2nd one
array_splice($items_array, 1, 0, $flags);
$items = implode('', $items_array);
endif;
endif;
endif;
return $items;
}
add_filter('wp_nav_menu_items','add_custom_in_menu', 10, 2);
I think it's the same approach you guys suggested. Thank you.

Insert custom content in WooCommerce

I have some problem in WooCommerce Admin Order menu. I want to add some content in the column but I don't know where I can make it.
This is some example screenshoot about my woocommerce admin order view :
I want to add phone number bellow email address in Order column.
Please help me to do that.
Thank you.
I've had to do this once, i've followed this tutorial :
https://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934
add_action('manage_shop_order_posts_custom_column', 'match_order_woocommerce_custom_order_columns', 2);
function match_order_woocommerce_custom_order_columns( $column ) {
remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
global $post, $woocommerce;
$order = new WC_Order( $post->ID );
switch ($column) {
case "order_title" :
if ($order->user_id) $user_info = get_userdata($order->user_id);
if (isset($user_info) && $user_info) :
$user = '<a href="user-edit.php?user_id=' . esc_attr( $user_info->ID ) . '">';
if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.' '.$user_info->last_name;
else $user .= esc_html( $user_info->display_name );
$user .= '</a>';
else :
$user = __('Guest', 'woocommerce');
endif;
echo '<strong>'.sprintf( __('Order %s', 'woocommerce'), $order->get_order_number() ).'</strong> ' . __('made by', 'woocommerce') . ' ' . $user;
if ($order->billing_email) :
echo '<small class="meta">'.__('Email:', 'woocommerce') . ' ' . ''.esc_html( $order->billing_email ).'</small>';
endif;
if ($order->billing_phone) :
echo '<small class="meta">'.__('Tel:', 'woocommerce') . ' ' . esc_html( $order->billing_phone ) . '</small>';
endif;
break;
}
}
Please try this snippet in your active theme's functions.php

Resources