WooCommerce Add to cart button error: "choose product options" - php

In WooCommerce variable.php I have this code:
<?php
/**
* Variable product add to cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product, $post;
?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
<?php if ( ! empty( $available_variations ) ) : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<td class="value"><select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>…</option>
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
} elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
$selected_value = $selected_attributes[ sanitize_title( $name ) ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$orderby = wc_attribute_orderby( sanitize_title( $name ) );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
break;
}
$terms = get_terms( sanitize_title( $name ), $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) )
continue;
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option ) {
echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
?>
</select> <?php
if ( sizeof( $attributes ) == $loop )
echo '<a class="reset_variations" href="#reset">' . __( 'Clear selection', 'woocommerce' ) . '</a>';
?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap" style="">
<?php do_action( 'woocommerce_before_single_variation' ); ?>
<div class="single_variation"></div>
<div class="variations_button">
<?php woocommerce_quantity_input(); ?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
</div>
<input name="add-to-cart" value="<?php echo $product->id; ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="variation_id" value="" />
<?php do_action( 'woocommerce_after_single_variation' ); ?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php else : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php endif; ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
But when I choose the size and then click Add to cart, I get an error with which says "choose product options".
Does somebody know how to fix this? It's like the form and the button aren't working "together".

I found the answer.
The thing you had to fill in, was for the <input type="hidden" name="variation_id" value="" />
So i gave it as value the product id and it worked :D
So like this:
<input type="hidden" name="variation_id" value="<?php echo $product->id; ?>" />

Related

Duplicate div in php

I created this PHP code in WordPress to add image to the menu, but it is duplicating a div where there is an add button and I don't know why, I need it to be only one and not two results of the div, in case I only want the left div... I'll add an image about what I'm talking about so you can analyze my problem!
Structure is placed inside function wp_post_thumbnail_only_html
I left only the action that passes the button to facilitate.
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
class SNAP_Menu_Image{
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ), 99 );
}
public function admin_init() {
if ( !has_action( 'wp_nav_menu_item_custom_fields' ) ) {
}
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'menu_item_custom_fields' ), 10, 4 );
}
public function file_is_displayable_image( $result, $path ) {
if ( $result ) {
return true;
}
$fileExtension = pathinfo( $path, PATHINFO_EXTENSION );
return in_array( $fileExtension, $this->additionalDisplayableImageExtensions );
}
public function menu_image_init() {
add_post_type_support( 'nav_menu_item', array( 'thumbnail' ) );
$this->image_sizes = apply_filters( 'menu_image_default_sizes', $this->image_sizes );
if (is_array($this->image_sizes)) {
foreach ($this->image_sizes as $name => $params) {
add_image_size($name, $params[0], $params[1], ( array_key_exists(2, $params) ? $params[2] : false ) );
}
}
load_plugin_textdomain( 'menu-image', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
public function menu_image_nav_menu_manage_columns( $columns ) {
return $columns + array( 'image' => __( 'Image', 'menu-image' ) );
}
public function menu_image_save_post_action( $post_id, $post ) {
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position'
);
foreach ( $menu_image_settings as $setting_name ) {
if ( isset( $_POST[ $setting_name ][ $post_id ] ) && !empty( $_POST[ $setting_name ][ $post_id ] ) ) {
if ( $post->{"_$setting_name"} != $_POST[ $setting_name ][ $post_id ] ) {
update_post_meta( $post_id, "_$setting_name", esc_sql( $_POST[ $setting_name ][ $post_id ] ) );
}
}
}
}
public function wp_update_nav_menu_item_action( $item_menu_id, $menu_item_db_id ) {
global $sitepress, $icl_menus_sync;
if ( class_exists( 'SitePress' ) && $sitepress instanceof SitePress && class_exists( 'ICLMenusSync' ) && $icl_menus_sync instanceof ICLMenusSync ) {
static $run_times = array();
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position',
'thumbnail_id',
'thumbnail_hover_id',
);
foreach ( $icl_menus_sync->menus as $menu_id => $menu_data ) {
if ( !isset( $_POST[ 'sync' ][ 'add' ][ $menu_id ] ) ) {
continue;
}
$cache_key = md5( serialize( array( $item_menu_id, 'tax_nav_menu' ) ) );
$cache_group = 'get_language_for_element';
wp_cache_delete( $cache_key, $cache_group );
$lang = $sitepress->get_language_for_element( $item_menu_id, 'tax_nav_menu' );
if ( !isset( $run_times[ $menu_id ][ $lang ] ) ) {
$run_times[ $menu_id ][ $lang ] = 0;
}
$post_item_ids = array();
foreach ( $_POST[ 'sync' ][ 'add' ][ $menu_id ] as $id => $lang_array ) {
if ( array_key_exists( $lang, $lang_array ) ) {
$post_item_ids[ ] = $id;
}
}
if ( !array_key_exists( $run_times[ $menu_id ][ $lang ], $post_item_ids ) ) {
continue;
}
$orig_item_id = $post_item_ids[ $run_times[ $menu_id ][ $lang ] ];
$orig_item_meta = get_metadata( 'post', $orig_item_id );
foreach ( $menu_image_settings as $meta ) {
if ( isset( $orig_item_meta[ "_$meta" ] ) && isset( $orig_item_meta[ "_$meta" ][ 0 ] ) ) {
update_post_meta( $menu_item_db_id, "_$meta", $orig_item_meta[ "_$meta" ][ 0 ] );
}
}
$run_times[ $menu_id ][ $lang ] ++;
break;
}
}
}
public function menu_image_edit_nav_menu_walker_filter() {
return 'Menu_Image_Walker_Nav_Menu_Edit';
}
public function menu_image_wp_setup_nav_menu_item( $item ) {
if ( !isset( $item->thumbnail_id ) ) {
$item->thumbnail_id = get_post_thumbnail_id( $item->ID );
}
if ( !isset( $item->thumbnail_hover_id ) ) {
$item->thumbnail_hover_id = get_post_meta( $item->ID, '_thumbnail_hover_id', true );
}
if ( !isset( $item->image_size ) ) {
$item->image_size = get_post_meta( $item->ID, '_menu_item_image_size', true );
}
if ( !isset( $item->title_position ) ) {
$item->title_position = get_post_meta( $item->ID, '_menu_item_image_title_position', true );
}
return $item;
}
public function menu_image_nav_menu_link_attributes_filter( $atts, $item, $args, $depth = null ) {
$this->setProcessed( $item->ID );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = ! empty( $atts[ 'class' ] ) ? $atts[ 'class' ] : '';
$class .= " menu-image-title-{$position}";
if ( $item->thumbnail_hover_id ) {
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$class .= ' menu-image-not-hovered';
}
if ( ! empty( $args->walker ) && class_exists( 'FlatsomeNavDropdown' ) && $args->walker instanceof FlatsomeNavDropdown && ! is_null( $depth ) && $depth === 0 ) {
$class .= ' nav-top-link';
}
$atts[ 'class' ] = trim( $class );
return $atts;
}
public function menu_image_nav_menu_item_title_filter( $title, $item, $depth = null, $args = null) {
if (is_numeric($item)) {
$item = wp_setup_nav_menu_item( get_post( $item ) );
}
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
}
$none = ''; // Sugar.
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $title, $image );
break;
case 'after':
default:
$item_args = array( $image, $title, $none );
break;
}
$title = vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
return $title;
}
public function menu_image_nav_menu_item_filter( $item_output, $item, $depth, $args ) {
if ( $this->isProcessed( $item->ID ) ) {
return $item_output;
}
$attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
$attributes .= !empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= !empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= !empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
$attributes_array = shortcode_parse_atts($attributes);
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$class .= ' menu-image-not-hovered';
}
$attributes_array['class'] = $class;
$attributes_array = apply_filters( 'menu_image_link_attributes', $attributes_array, $item, $depth, $args );
$attributes = '';
foreach ( $attributes_array as $attr_name => $attr_value ) {
$attributes .= "{$attr_name}=\"$attr_value\" ";
}
$attributes = trim($attributes);
$item_output = "{$args->before}<a {$attributes}>";
$link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$none = '';
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $link, $image );
break;
case 'after':
default:
$item_args = array( $image, $link, $none );
break;
}
$item_output .= vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
$item_output .= "</a>{$args->after}";
return $item_output;
}
public function menu_image_add_inline_style_action() {
wp_register_style( 'menu-image', SF()->plugin_url() . '/assets/css/menu-image.css', array(), '1.1', 'all' );
wp_enqueue_style( 'menu-image' );
}
public function menu_image_admin_head_nav_menus_action() {
wp_enqueue_script( 'menu-image-admin', SF()->plugin_url() . '/assets/js/menu-image.js', array( 'jquery' ) );
wp_localize_script(
'menu-image-admin', 'menuImage', array(
'l10n' => array(
'uploaderTitle' => __( 'Chose menu image', 'menu-image' ),
'uploaderButtonText' => __( 'Select', 'menu-image' ),
),
'settings' => array(
'nonce' => wp_create_nonce( 'update-menu-item' ),
),
)
);
wp_enqueue_media();
wp_enqueue_style( 'editor-buttons' );
}
public function menu_image_delete_menu_item_image_action() {
$menu_item_id = (int) $_REQUEST[ 'menu-item' ];
check_admin_referer( 'delete-menu_item_image_' . $menu_item_id );
if ( is_nav_menu_item( $menu_item_id ) && has_post_thumbnail( $menu_item_id ) ) {
delete_post_thumbnail( $menu_item_id );
delete_post_meta( $menu_item_id, '_thumbnail_hover_id' );
delete_post_meta( $menu_item_id, '_menu_item_image_size' );
delete_post_meta( $menu_item_id, '_menu_item_image_title_position' );
}
}
public function wp_post_thumbnail_only_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$markup = '<p class="description description-thin" ><label>%s<br /><a title="%s" href="#" class="set-post-thumbnail button%s" data-item-id="%s" style="height: auto;">%s</a>%s</label></p>';
$thumbnail_id = get_post_thumbnail_id( $item_id );
$content = sprintf(
$markup,
esc_html__( 'Menu image', 'menu-image' ),
$thumbnail_id ? esc_attr__( 'Change menu item image', 'menu-image' ) : esc_attr__( 'Set menu item image', 'menu-image' ),
'',
$item_id,
$thumbnail_id ? wp_get_attachment_image( $thumbnail_id, $default_size ) : esc_html__( 'Set image', 'menu-image' ),
$thumbnail_id ? '' . __( 'Remove', 'menu-image' ) . '' : ''
);
return $content;
}
public function wp_post_thumbnail_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$content = $this->wp_post_thumbnail_only_html( $item_id );
$image_size = get_post_meta( $item_id, '_menu_item_image_size', true );
if ( !$image_size ) {
$image_size = $default_size;
}
$title_position = get_post_meta( $item_id, '_menu_item_image_title_position', true );
if ( !$title_position ) {
$title_position = apply_filters( 'menu_image_default_title_position', 'after' );
}
$content = "<div class='menu-item-images' style='min-height:70px'>$content</div>";
return apply_filters( 'admin_menu_item_thumbnail_html', $content, $item_id );
}
public function wp_ajax_set_menu_item_thumbnail() {
$json = !empty( $_REQUEST[ 'json' ] );
$post_ID = intval( $_POST[ 'post_id' ] );
if ( !current_user_can( 'edit_post', $post_ID ) ) {
wp_die( - 1 );
}
$thumbnail_id = intval( $_POST[ 'thumbnail_id' ] );
$is_hovered = (bool) $_POST[ 'is_hover' ];
check_ajax_referer( "update-menu-item" );
if ( $thumbnail_id == '-1' ) {
if ( $is_hovered ) {
$success = delete_post_meta( $post_ID, '_thumbnail_hover_id' );
} else {
$success = delete_post_thumbnail( $post_ID );
}
} else {
if ( $is_hovered ) {
$success = update_post_meta( $post_ID, '_thumbnail_hover_id', $thumbnail_id );
} else {
$success = set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
if ( $success ) {
$return = $this->wp_post_thumbnail_only_html( $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
}
wp_die( 0 );
}
public function menu_item_custom_fields( $item_id, $item, $depth, $args ){
?>
<div class="field-image hide-if-no-js wp-media-buttons">
<?php echo $this->wp_post_thumbnail_html( $item_id ) ?>
</div>
<?php
}
public function jetpack_photon_override_image_downsize_filter( $prevent, $data ) {
return $this->isAttachmentUsed( $data[ 'attachment_id' ], $data[ 'size' ] );
}
public function setUsedAttachments( $size, $id ) {
$this->used_attachments[ $size ][ ] = $id;
}
public function isAttachmentUsed( $id, $size = null ) {
if ( ! is_null( $size ) ) {
return is_string( $size ) && isset( $this->used_attachments[ $size ] ) && in_array( $id, $this->used_attachments[ $size ] );
} else {
foreach ( $this->used_attachments as $used_attachment ) {
if ( in_array( $id, $used_attachment ) ) {
return true;
}
}
return false;
}
}
public function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
if ( $this->isAttachmentUsed( $attachment->ID, $size ) ) {
unset( $attr['sizes'], $attr['srcset'] );
}
return $attr;
}
protected function setProcessed( $id ) {
$this->processed[] = $id;
}
protected function isProcessed( $id ) {
return in_array( $id, $this->processed );
}
}
$menu_image = new SNAP_Menu_Image();
require_once(ABSPATH . 'wp-admin/includes/nav-menu.php');
class Menu_Image_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
ob_start();
$item_id = esc_attr( $item->ID );
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
$original_title = false;
if ( 'taxonomy' == $item->type ) {
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
if ( is_wp_error( $original_title ) )
$original_title = false;
} elseif ( 'post_type' == $item->type ) {
$original_object = get_post( $item->object_id );
$original_title = get_the_title( $original_object->ID );
} elseif ( 'post_type_archive' == $item->type ) {
$original_object = get_post_type_object( $item->object );
if ( $original_object ) {
$original_title = $original_object->labels->archives;
}
}
$classes = array(
'menu-item menu-item-depth-' . $depth,
'menu-item-' . esc_attr( $item->object ),
'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
);
$title = $item->title;
if ( ! empty( $item->_invalid ) ) {
$classes[] = 'menu-item-invalid';
$title = sprintf( __( '%s (Invalid)' ), $item->title );
} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
$classes[] = 'pending';
$title = sprintf( __('%s (Pending)'), $item->title );
}
$title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
$submenu_text = '';
if ( 0 == $depth )
$submenu_text = 'style="display: none;"';
?>
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
<div class="menu-item-bar">
<div class="menu-item-handle">
<span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
<span class="item-controls">
<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
<span class="item-order hide-if-js">
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-up-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-up" aria-label="<?php esc_attr_e( 'Move up' ) ?>">↑</a>
|
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-down-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-down" aria-label="<?php esc_attr_e( 'Move down' ) ?>">↓</a>
</span>
<a class="item-edit" id="edit-<?php echo $item_id; ?>" href="<?php
echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
?>" aria-label="<?php esc_attr_e( 'Edit menu item' ); ?>"><span class="screen-reader-text"><?php _e( 'Edit' ); ?></span></a>
</span>
</div>
</div>
<div class="menu-item-settings wp-clearfix" id="menu-item-settings-<?php echo $item_id; ?>">
<?php if ( 'custom' == $item->type ) : ?>
<p class="field-url description description-wide">
<label for="edit-menu-item-url-<?php echo $item_id; ?>">
<?php _e( 'URL' ); ?><br />
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
</label>
</p>
<?php endif; ?>
<p class="field-xfn description description-thin">
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
<?php _e( 'Link Relationship (XFN)' ); ?><br />
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
</label>
</p>
<?php
do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
?>
<fieldset class="field-move hide-if-no-js description description-wide">
<span class="field-move-visual-label" aria-hidden="true"><?php _e( 'Move' ); ?></span>
<button type="button" class="button-link menus-move menus-move-up" data-dir="up"><?php _e( 'Up one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-down" data-dir="down"><?php _e( 'Down one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-left" data-dir="left"></button>
<button type="button" class="button-link menus-move menus-move-right" data-dir="right"></button>
<button type="button" class="button-link menus-move menus-move-top" data-dir="top"><?php _e( 'To the top' ); ?></button>
</fieldset>
<div class="menu-item-actions description-wide submitbox">
<?php if ( 'custom' != $item->type && $original_title !== false ) : ?>
<p class="link-to-original">
<?php printf( __('Original: %s'), '' . esc_html( $original_title ) . '' ); ?>
</p>
<?php endif; ?>
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'delete-menu-item',
'menu-item' => $item_id,
),
admin_url( 'nav-menus.php' )
),
'delete-menu_item_' . $item_id
); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
</div>
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
</div>
<ul class="menu-item-transport"></ul>
<?php
$output .= ob_get_clean();
}
}
As basic as the answer is, I'll leave the question active...
I had instantiated the class 2 times!

Get image from post in wordpress

I need to configure this script so that it fetches the first image that is in the article and shows it in a widjet.
At the moment the widjet only shows featured images, I would like it to show external images that were in the post.
For example, the first image in the article would be shown.
Code
<?php
/**
* Featured Posts widget
*/
class colormag_featured_posts_widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_featured_posts widget_featured_meta',
'description' => __( 'Display latest posts or posts of specific category.', 'colormag' ),
'customize_selective_refresh' => true,
);
$control_ops = array( 'width' => 200, 'height' => 250 );
parent::__construct( false, $name = __( 'TG: Featured Posts (Style 1)', 'colormag' ), $widget_ops );
}
function form( $instance ) {
$tg_defaults['title'] = '';
$tg_defaults['text'] = '';
$tg_defaults['number'] = 4;
$tg_defaults['type'] = 'latest';
$tg_defaults['category'] = '';
$instance = wp_parse_args( ( array ) $instance, $tg_defaults );
$title = esc_attr( $instance['title'] );
$text = esc_textarea( $instance['text'] );
$number = $instance['number'];
$type = $instance['type'];
$category = $instance['category'];
?>
<p><?php _e( 'Layout will be as below:', 'colormag' ) ?></p>
<div style="text-align: center;"><img src="<?php echo get_template_directory_uri() . '/img/style-1.jpg' ?>">
</div>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php _e( 'Description', 'colormag' ); ?>
<textarea class="widefat" rows="5" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to display:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="radio" <?php checked( $type, 'latest' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="latest" /><?php _e( 'Show latest Posts', 'colormag' ); ?>
<br />
<input type="radio" <?php checked( $type, 'category' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="category" /><?php _e( 'Show posts from a category', 'colormag' ); ?>
<br /></p>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>
:</label>
<?php wp_dropdown_categories( array(
'show_option_none' => ' ',
'name' => $this->get_field_name( 'category' ),
'selected' => $category,
) ); ?>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
}
$instance['number'] = absint( $new_instance['number'] );
$instance['type'] = $new_instance['type'];
$instance['category'] = $new_instance['category'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
extract( $instance );
global $post;
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$text = isset( $instance['text'] ) ? $instance['text'] : '';
$number = empty( $instance['number'] ) ? 4 : $instance['number'];
$type = isset( $instance['type'] ) ? $instance['type'] : 'latest';
$category = isset( $instance['category'] ) ? $instance['category'] : '';
$post_status = 'publish';
if ( get_option( 'fresh_site' ) == 1 ) {
$post_status = array( 'auto-draft', 'publish' );
}
$args = array(
'posts_per_page' => $number,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => $post_status,
);
// Display from category chosen.
if ( $type == 'category' ) {
$args['category__in'] = $category;
}
$get_featured_posts = new WP_Query( $args );
echo $before_widget;
?>
<?php
if ( $type != 'latest' ) {
$border_color = 'style="border-bottom-color:' . colormag_category_color( $category ) . ';"';
$title_color = 'style="background-color:' . colormag_category_color( $category ) . ';"';
} else {
$border_color = '';
$title_color = '';
}
if ( ! empty( $title ) ) {
echo '<h3 class="widget-title" ' . $border_color . '><span ' . $title_color . '>' . esc_html( $title ) . '</span></h3>';
}
if ( ! empty( $text ) ) {
?> <p> <?php echo esc_textarea( $text ); ?> </p> <?php } ?>
<?php
$i = 1;
while ( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
?>
<?php if ( $i == 1 ) {
$featured = 'colormag-featured-post-medium';
} else {
$featured = 'colormag-featured-post-small';
} ?>
<?php if ( $i == 1 ) {
echo '<div class="first-post">';
} elseif ( $i == 2 ) {
echo '<div class="following-post">';
} ?>
<div class="single-article clearfix">
<?php
if ( has_post_thumbnail() ) {
$image = '';
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$image_alt_text = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
$title_attribute = get_the_title( $post->ID );
if ( empty( $image_alt_text ) ) {
$image_alt_text = $title_attribute;
}
$image .= '<figure>';
$image .= '<a href="' . get_permalink() . '" title="' . the_title( '', '', false ) . '">';
$image .= get_the_post_thumbnail( $post->ID, $featured, array(
'title' => esc_attr( $title_attribute ),
'alt' => esc_attr( $image_alt_text ),
) ) . '</a>';
$image .= '</figure>';
echo $image;
}
?>
<div class="article-content">
<?php colormag_colored_category(); ?>
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<div class="below-entry-meta">
<?php
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on"><i class="fa fa-calendar-o"></i> %3$s</span>', 'colormag' ), esc_url( get_permalink() ), esc_attr( get_the_time() ), $time_string
);
?>
<span class="byline"><span class="author vcard"><i class="fa fa-user"></i><a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo get_the_author(); ?>"><?php echo esc_html( get_the_author() ); ?></a></span></span>
<?php if ( ! post_password_required() && comments_open() ) { ?>
<span class="comments"><i class="fa fa-comment"></i><?php comments_popup_link( '0', '1', '%' ); ?></span>
<?php } ?>
</div>
<?php if ( $i == 1 ) { ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php } ?>
</div>
</div>
<?php if ( $i == 1 ) {
echo '</div>';
} ?>
<?php
$i ++;
endwhile;
if ( $i > 2 ) {
echo '</div>';
}
// Reset Post Data
wp_reset_query();
?>
<!-- </div> -->
<?php
echo $after_widget;
}
}
This is the complete code of the widget
Try this code
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>

List product variations using wc_dropdown_variation_attribute_options in Woocommerce

I am new to woocommerce and i am trying to display the product variations as a drop-down list in shop page. But the variations are not getting populated in the select option list. below is my code snippet.
<?php foreach ( $product->get_attributes() as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
//$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
//wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
$args=array();
$result = wc_dropdown_variation_attribute_options($args);
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach;?>
The code that you have there comes from single-product/add-to-cart/variable.php template.
But you can't use it with $product->get_attributes() in the foreach loop.
In this template, $attributes is $product->get_variation_attributes()…
So the following code will work to display the attributes dropdowns (where $product is an instance of the variable product object):
<?php
$attributes = $product->get_variation_attributes();
$attribute_keys = array_keys( $attributes );
?>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
$args = array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected );
wc_dropdown_variation_attribute_options( $args );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
As you can see the wc_dropdown_variation_attribute_options() function is made to display variation attribute dropdowns for the current variable product, BUT NOT a list of variations in a drop down.
List in a dropdown the product variations of a variable product:
To list all variation attributes in a dropdown you will use the following code (where $product is an instance of the variable product object):
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
$output = '<div class="product-variations-dropdown">
<select id="available-variations" class="" name="available_variations">';
$output .= '<option value="">'. __('Choose a variation') .'</option>';
foreach( $available_variations as $variation ){
$option_value = array();
foreach( $variation['attributes'] as $attribute => $term_slug ){
$taxonomy = str_replace( 'attribute_', '', $attribute );
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name
$term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name; // Attribute value term name
$option_value[] = $attribute_name . ': '.$term_name;
}
$option_value = implode( ' | ', $option_value );
$output .= '<option value="'.$variation['variation_id'].'">'.$option_value.'</option>';
}
$output .= '
</select>
</div>';
echo $output;
}
Code is tested and works
To get an instance of the variable product object from a defined $product_id (where $product_id is the ID of a variable product) you will use:
$product = wc_get_product($product_id);

dynamic pricing and request a quote woocommerce

Hello World!
I am currently working on an e-commerce website based on woocommerce and I've recently bought
the Dynamic Pricing & the Request a Quote plugins made by YITH
Everything is okay with those plugins but the e-commerce website I'm doing can both do quote and cart that's why I use the Request a Quote plugin but the differents dynamic prices I do are'nt visibe in my Quote page but they are in my cart page.
After many search on google it seems that the two plugins are not compatible.
So this is my question, is there a way to hook the final prices obtain by the dynamic prices plugin in my quote page ?
I know some basics on PHP and I have took a look at the differents functions and classes of the both plugins to try to solve the problem but I didn't found it.. :(
Here is the beta of website i'm doing, http://beta.jardivrac.com you can try the dynamic prices on this page and after go to the card page and the quote page
And here is the public function I've found on the fronted class in the dynamic prices plugin ->
public function replace_cart_item_price( $price, $cart_item, $cart_item_key ) {
if ( ! isset( $cart_item['ywdpd_discounts'] ) ) {
return $price;
}
$old_price = $price;
foreach ( $cart_item['ywdpd_discounts'] as $discount ) {
if ( isset( $discount['status'] ) && $discount['status'] == 'applied' ) {
if ( wc_price( $cart_item['ywdpd_discounts']['default_price'] ) != WC()->cart->get_product_price( $cart_item['data'] ) ) {
$price = '<del>' . wc_price( $cart_item['ywdpd_discounts']['default_price'] ) . '</del> ' . WC()->cart->get_product_price( $cart_item['data'] );
} else {
return $price;
}
}
}
$price = apply_filters( 'ywdpd_replace_cart_item_price', $price, $old_price, $cart_item, $cart_item_key );
return $price;
}
If somebody have the solution that will be a great news !
Thanks in advance for your help !
Antoine
EDIT:
This is the loop inside the Woocommerce cart.php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</td>
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail;
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail );
}
?>
</td>
<td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
} else {
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_title() ), $cart_item, $cart_item_key );
}
// Meta data
echo WC()->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
}
?>
</td>
<td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</td>
<td class="product-quantity" data-title="<?php _e( 'Quantity', 'woocommerce' ); ?>">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
'min_value' => '0'
), $_product, false );
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
?>
</td>
<td class="product-subtotal" data-title="<?php _e( 'Total', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
?>
</td>
</tr>
<?php
}
}
And this is the loop for the quote
$total = 0;
foreach ( $raq_content as $key => $raq ):
$_product = wc_get_product( ( isset( $raq['variation_id'] ) && $raq['variation_id'] != '' ) ? $raq['variation_id'] : $raq['product_id'] );
if( ! $_product ){
continue;
}
$show_price = true;
do_action( 'ywraq_before_request_quote_view_item', $raq_content, $key );
?>
<tr class="<?php echo esc_attr( apply_filters( 'yith_ywraq_item_class', 'cart_item', $raq_content, $key ) ); ?>" <?php echo esc_attr( apply_filters( 'yith_ywraq_item_attributes', '', $raq_content, $key ) ); ?>>
<td class="product-remove">
<?php
echo apply_filters( 'yith_ywraq_item_remove_link', sprintf( '×', $key, wp_create_nonce( 'remove-request-quote-' . $_product->id ), $_product->id, __( 'Retirer cet article', 'yith-woocommerce-request-a-quote' ) ), $key );
?>
</td>
<td class="product-thumbnail">
<?php $thumbnail = $_product->get_image();
if ( ! $_product->is_visible() ) {
echo $thumbnail;
} else {
printf( '%s', $_product->get_permalink(), $thumbnail );
}
?>
</td>
<td class="product-name">
<?php
$title = $_product->get_title();
if ( $_product->get_sku() != '' && get_option( 'ywraq_show_sku' ) == 'yes' ) {
$title .= apply_filters( 'ywraq_sku_label', __( ' SKU:', 'yith-woocommerce-request-a-quote' ) ) . $_product->get_sku();
}
?>
<?php echo $title ?>
<?php
// Meta data
$item_data = array();
// Variation data
if ( ! empty( $raq['variation_id'] ) && is_array( $raq['variations'] ) ) {
foreach ( $raq['variations'] as $name => $value ) {
$label = '';
if ( '' === $value ) {
continue;
}
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
// If this is a term slug, get the term's nice name
if ( taxonomy_exists( $taxonomy ) ) {
$term = get_term_by( 'slug', $value, $taxonomy );
if ( ! is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
} else {
if ( strpos( $name, 'attribute_' ) !== false ) {
$custom_att = str_replace( 'attribute_', '', $name );
if ( $custom_att != '' ) {
$label = wc_attribute_label( $custom_att );
} else {
$label = $name;
}
}
}
$item_data[] = array(
'key' => $label,
'value' => $value
);
}
}
$item_data = apply_filters( 'ywraq_request_quote_view_item_data', $item_data, $raq, $_product, $show_price );
// Output flat or in list format
if ( sizeof( $item_data ) > 0 ) {
foreach ( $item_data as $data ) {
echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['value'] ) . "\n";
}
}
?>
</td>
<td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</td>
<td class="product-quantity">
<?php
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "raq[{$key}][qty]",
'input_value' => apply_filters( 'ywraq_quantity_input_value', $raq['quantity'] ),
'max_value' => apply_filters( 'ywraq_quantity_max_value', $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), $_product ),
'min_value' => apply_filters( 'ywraq_quantity_min_value', 0, $_product )
), $_product, false );
echo $product_quantity;
?>
</td>
<?php if ( get_option( 'ywraq_hide_total_column', 'yes' ) == 'no' ): ?>
<td class="product-subtotal">
<?php
$total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
$price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
echo apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq);
?>
</td>
<?php endif ?>
</tr>
<?php do_action( 'ywraq_after_request_quote_view_item', $raq_content, $key ); ?>
<?php endforeach ?>
<!--
<?php if ( get_option( 'ywraq_hide_total_column', 'yes' ) == 'no' ): ?>
<tr>
<td colspan="3">
</td>
<th>
<?php _e( 'Total:', 'yith-woocommerce-request-a-quote') ?>
</th>
<td class="raq-totals">
<?php echo wc_price( $total ); ?>
</td>
</tr>
<?php endif ?>
-->
I think it will help, I already tried to replace the loop of the request a quote page by the loop of the card but it only created 2 cart loop, not what I expected... Maybe somebody better in PHP than me could have an idea ?
After many try & a short night I've found a "solution" not really good one, but everything works perfectly!
So I modified those pages:
request-quote-view.php (Table of the quote page)
I've added this little loop before the foreach of the differents products in the table.
$arr_discount = array();
foreach(YITH_WC_Dynamic_Pricing() as $key => $raq ){
foreach($raq as $arr => $data){
array_push($arr_discount, $data);
}
}
With this loop I put the differents informations about the dynamic pricing in a new array
So then, in the start of the foreach loop I have an other loop wich put the content we get before into the array of the quote
foreach($arr_discount as $test){
foreach($test['apply_to_products_list'] as $id){
if($id == $raq['product_id']){
foreach($test['rules'] as $rule){
$raq['arr_dsct'][] = $rule;
}
}
}
}
And finaly in the td class=subtotal I replaced the content with
if(isset($raq['arr_dsct']) == ''){
$total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
$price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
echo apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq);
} else {
$count = count($raq['arr_dsct']);
for($i = 0; $i < $count; $i++) {
if( $raq['quantity'] >= $raq['arr_dsct'][$i][min_quantity] AND $raq['quantity'] <= $raq['arr_dsct'][$i][max_quantity] ){
$price = $_product->price - $_product->price * $raq['arr_dsct'][$i][discount_amount];
$total = round($raq['quantity'] * $price, 2);
$totale = '<span class="woocommerce-Price-amount amount">'. $total .'<span class="woocommerce-Price-currencySymbol">€</span></span>';
} elseif ( $raq['quantity'] < $raq['arr_dsct'][0][min_quantity] ) {
$total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
$price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
$totale = apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq);
}
}
echo $totale;
}
request-quote-table.php (Table of the mail)
For the mail page it was exactly the same answer with only one difference, the array object of the product is named $item not $raq so It need some modification
If you want more information about the place where should be the different piece of code there is two images wich show it !

WooCommerce customer order detail in BS Modal

In a WordPress website running WooCommerce, the user can login in his (default) personal area and display information like:
Orders history
Download
Addresses
Edit info
Logout
In the orders tab, a table is presented by default, showing a list of all orders, with a View button which redirects to the full detail page of that order.
What I'm trying to do is showing that table view in a modal window.
I don't have any problem in showing the modal with the target url loaded in it.
The real problem is that the targeted url is that of the full page which is showing like in an <iframe>, and is not what I want.
I think there is some shortcode allowing to load just that table, or maybe some woocommerce function like load_order_content_by_id($id)?
Can anybody point me in the right direction?
Thanks
===SOLVED===
Thanks to Raunak Gupta for pointing me to the right function.
I override the orders.php template, added Modal window html and edited $actions:
'view' => array(
'url' => 'javascript:;',
'data' => [
'order-number' => $order->get_order_number()
],
'name' => __( 'View', 'woocommerce' )
),
and on same file:
foreach ( $actions as $key => $action ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '"';
if(isset($action['data']) && is_array($action['data'])){
foreach($action['data'] AS $data_attr=>$data_value){
echo 'data-' . sanitize_html_class($data_attr) .'="' .esc_html($data_value) . '" ';
}
}
echo '>' . esc_html( $action['name'] ) . '</a>';
}
A little JS
$('.woocommerce-MyAccount-orders .button.view').on('click', function(e){
e.preventDefault();
var data = {};
data.action = 'modal_order';
data.order_number = $(this).data('order-number');
$.get( ajax_script.ajax_url, data, function(response) {
$('#modalOrderDetail').modal('show').find('.modal-body').html(response);
});
});
and hooked into wordpress by function.php
function modal_order() {
if(is_user_logged_in()) {
$order_number = $_GET['order_number'];
woocommerce_order_details_table($order_number);
}
}
add_action('wp_ajax_modal_order', 'modal_order');
add_action('wp_ajax_nopriv_modal_order', 'modal_order');
Here is the complete code to display curent customer orders in a modal window. Its based on a classic query to get current user orders and on the template my-account/orders.php (lightly customized)…
<?php
if(is_user_logged_in()):
// The query
$args = array(
// WC orders post type
'post_type' => 'shop_order',
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
// get orders statuses
'post_status' => array_keys(wc_get_order_statuses()),
);
// Get all customer orders
$customer_orders = get_posts( $args );
$count_ord = 0;
if (!empty($customer_orders))
foreach ( $customer_orders as $custo_order )
$count_ord++;
if ( $count_ord > 0 )
$has_orders = true;
else
$has_orders = false;
// the template my-account/orders.php ?>
<?php do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
<?php if ( $has_orders ) : ?>
<table class="woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order );
$item_count = $order->get_item_count();
?>
<tr class="order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number(); ?>
</a>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
)
);
if ( ! $order->needs_payment() ) {
unset( $actions['pay'] );
}
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
unset( $actions['cancel'] );
}
if ( $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
<div class="woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php _e( 'Previous', 'woocommerce' ); ?></a>
<?php endif; ?>
<?php if ( $current_page !== intval( $customer_orders->max_num_pages ) ) : ?>
<a class="woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page + 1 ) ); ?>"><?php _e( 'Next', 'woocommerce' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<div class="woocommerce-Message woocommerce-Message--info woocommerce-info">
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
<?php _e( 'Go Shop', 'woocommerce' ) ?>
</a>
<?php endif; ?>
<?php _e( 'No order has been made yet.', 'woocommerce' ); ?>
</div>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
<?php endif; ?>
You will have to add existing css rules to your modal window and/or customized that css rules.
This code is tested and fully functional.
woocommerce_order_details_table( $order_id )
This WooCommerce function returns the full order details in HTML form by $order_id
There isn't a single function that I'm aware of that can get all of the order details that you need in one go however you can call WC_Order class to get what you need. Making some calls methods similar to these would get you the info you need. You will likely need to make calls to more than just the get_items() method depending on the exact info you need. Generally they return objects similar in structure to post objects.
$order = new WC_Order($post->ID);
$_order = $order->get_items();
Look here under the 'inherited methods' section to find the methods you might need to call to get all the info you need. https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

Categories