I have 2 functions. One is to calculate fee (here is part from it that is responsible for calculating fee and saving it into global variable):
function pre_checkout()
{
global $woocommerce;
$section2 ='';
$section3 ='';
$section2 = sanitize_text_field($_POST["user_data"]["section2"]);
$section3 = sanitize_text_field($_POST["user_data"]["section3"]);
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section1"]);
$boxToFind = '';
if(($section2 == 'toy_yes') || ($section2 == 'toy_no'))
{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"]);
}
else{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"].$_POST["user_data"]["section2"]);
}
WC()->cart->empty_cart(true);
$boxesLookupTable = array(
//susbscriptions
'small1month'=>'112',
'medium1month'=>'113',
'large1month'=>'114',
'small6month'=>'115',
'medium6month'=>'116',
'large6month'=>'117',
'small12month'=>'118',
'medium12month'=>'119',
'large12month'=>'120',
//gifts
'gift1month'=>'133',
'gift3month'=>'134',
'gift6month' =>'135',
'gift12month' =>'136'
);
$productId = $boxesLookupTable[$boxToFind];
// echo $productId;
//check if product already in cart
// if no products in cart, add it
WC()->cart->add_to_cart($productId,1);
if($section2 === "toy_yes"){
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$toy_price*$how_many_months,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/* "gift"; */
}
if($section3 === "toy_yes"){
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section2"]);
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_SESSION['product_price_with_toy']);
$_SESSION['product_price_with_toy'] = $price + $toy_price;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$how_many_months * $toy_price,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/*"subscription"; */
};
$items = WC()->cart->get_cart();
global $woocommerce;
$cart_url = wc_get_cart_url();
$checkout_url = wc_get_checkout_url();
foreach($items as $item => $values) {
$_product = $values['data']->post;
?>
<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?>
</div>
<div class="dropdown-cart-right">
<h5><?php echo $_product->post_title; ?></h5>
<p><strong>Quantity:</strong>
<?php $product = wc_get_product($productId);
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
?></p>
<?php global $woocommerce;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( $values['product_id'], '_regular_price', true);
$sale = get_post_meta( $values['product_id'], '_sale_price', true);
?>
<?php if($sale) { ?>
<p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
<?php } elseif($price) { ?>
<p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>
<?php }
?>
<div class="dropdown-cart-right dropdown-checkout-link">
<a class="checkout-submit checkout green-btn text-center" href="#">Order</a>
</div>
</div>
<div class="clear"></div>
</div>
<?php }
die();
}
add_action('wp_ajax_pre_checkout','pre_checkout');
add_action('wp_ajax_nopriv_pre_checkout', 'pre_checkout');
Next i have woocommerce action:
function add_cart_fee() {
$chuj = $GLOBALS['product_price_with_toy'];
WC()->cart->add_fee( __('Extra Gift Fee', 'woocommerce'),$chuj );
}
add_action( 'woocommerce_cart_calculate_fees', 'add_cart_fee' );
And basicaly toy fee is always 0 when im using session, global variables - only through cookies is passing values to action - i dont understand why woocommerce / wordpress is wiping global value that ive set inside my calculation function. Or maybe someone know how to update the total price with using hooks and actions, that would be even better than using fee field.
Related
i have a cart icon on the header.When the page is scrolled this cart icon is floating in a fixed position.when hover over cart icon the cart contents like name price subtotal all are displaying.now when adding a product the cart count is updating correctly.nut cart contents are updating only after refresh.
code of cart in header.php
<li>
<a href="<?php echo get_template_directory_uri(); ?>/cart"
><i class="uil uil-shopping-cart-alt"></i>
<div>Cart</div></a
>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
</li>
updating cart count code in functions.php
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
<?php
$fragments['#cart-count'] = ob_get_clean();
return $fragments;
}
this is the code displaying when hovering over the cart
<div class="miniBasketContent"><div class="miniBasketHeader">Your basket</div><div class="items"><?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = $woocommerce->cart->total;
foreach($items as $item => $values) { $_product = wc_get_product( $values['data']->get_id() );$getProductDetail = wc_get_product( $values['product_id'] );$price = get_post_meta($values['product_id'] , '_price', true);?><div class="item"><div class="image"><?php echo $getProductDetail->get_image();?></div><div class="info"><?php echo $_product->get_title();?></div><div class="price"><?php echo $price;?> AED</div></div><?php } ?></div><div class="miniBasketFooter"><div class="info"><div class="total"><span><?php echo $total; ?>AED</span>Total</div>Show basketGo to checkout</div></div></div>
what to do to update cart contents inside popup without reloading page.
You can try the below solution
demo_cart_link() Use this function where you want to display cart count HTML.
function.php
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.3', '>=' ) ) {
add_filter( 'woocommerce_add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
} else {
add_filter( 'add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
}
if ( ! function_exists( 'demo_cart_link_fragment' ) ) {
function demo_cart_link_fragment( $fragments ) {
global $woocommerce;
ob_start();
demo_cart_link();
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
}
if ( ! function_exists( 'demo_cart_link' ) ) {
function demo_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'paperfig' ); ?>" type="button" data-toggle="modal">
<i class="fas fa-shopping-basket"></i>
<span class="cart-numbers"><?php echo wp_kses_data( sprintf( _n( ' %d', ' %d', WC()->cart->get_cart_contents_count(), 'paperfig' ), WC()->cart->get_cart_contents_count() ) );?></span>
</a>
<?php
}
}
I'm trying to show / hide the comment_text under the comment_meta for a custom post type, editing the walker_class of the twenty twenty theme (child).
I succeeded in doing it with javascript but it works only on the first comment!
What's wrong?
Either trying to pass the comment_id like the div_id, it doesn't work at all.
It's about 2days I've spent round here!
<?php
/**
* Custom comment walker for this theme.
*
* #package WordPress
* #subpackage Twenty_Twenty
* #since 1.0.0
*/
if ( ! class_exists( 'TwentyTwenty_Walker_Comment' ) ) {
/**
* CUSTOM COMMENT WALKER
* A custom walker for comments, based on the walker in Twenty Nineteen.
*/
class TwentyTwenty_Walker_Comment extends Walker_Comment {
/**
* Outputs a comment in the HTML5 format.
*
* #see wp_list_comments()
* #see https://developer.wordpress.org/reference/functions/get_comment_author_url/
* #see https://developer.wordpress.org/reference/functions/get_comment_author/
* #see https://developer.wordpress.org/reference/functions/get_avatar/
* #see https://developer.wordpress.org/reference/functions/get_comment_reply_link/
* #see https://developer.wordpress.org/reference/functions/get_edit_comment_link/
*
* #param WP_Comment $comment Comment to display.
* #param int $depth Depth of the current comment.
* #param array $args An array of arguments.
*/
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard" onclick="showDiv()" >
<?php
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
$avatar = get_avatar( $comment, $args['avatar_size'] );
$comme = get_comment_text( $comment );
$id_com = get_comment_id( $comment );
if ( 0 !== $args['avatar_size'] ) {
if ( empty( $comment_author_url ) ) {
echo wp_kses_post( $avatar );
} else {
printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
echo wp_kses_post( $avatar );
}
}
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);
if ( ! empty( $comment_author_url ) ) {
echo '</a>';
}
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* Translators: 1 = comment date, 2 = comment time */
$comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
?>
<time datetime="<?php comment_time( 'c' ); ?>" title="<?php echo esc_attr( $comment_timestamp ); ?>">
<?php echo esc_html( $comment_timestamp ); ?>
</time>
</a>
<?php
if ( get_edit_comment_link() ) {
echo ' <span aria-hidden="true">•</span> <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
}
?>
</div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<!--- <div class="comment-content entry-content"> -->
<div class="comment-content entry-content" id="<?php echo $id_com ;?>" >
<?php
echo $comme;
if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}
?></div>
<script type='text/javascript'>
function showDiv() {
var id_com = <?php echo $id_com ;?>; //retrieving the id
if (document.getElementById(id_com).style.display == 'none') {
document.getElementById(id_com).style.display = 'block';
} else {
document.getElementById(id_com).style.display = 'none';
}
}
</script>
<!-- </div><!-- .comment-content -->
<?php
$comment_reply_link = get_comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="comment-reply">',
'after' => '</span>',
)
)
);
$by_post_author = twentytwenty_is_comment_by_post_author( $comment );
if ( $comment_reply_link || $by_post_author ) {
?>
<footer class="comment-footer-meta">
<?php
if ( $comment_reply_link ) {
echo $comment_reply_link; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>
</footer>
<?php
;}
?>
</article><!-- .comment-body -->
<?php
}
}
}
I succeed in solving the problem:
the javascript function needed to have a different name for each comment, same for the container to hide/show
That's the code:
<?php
/**
* Custom comment walker for this theme.
*
* #package WordPress
* #subpackage Twenty_Twenty
* #since 1.0.0
*/
if ( ! class_exists( 'TwentyTwenty_Walker_Comment' ) ) {
/**
* CUSTOM COMMENT WALKER
* A custom walker for comments, based on the walker in Twenty Nineteen.
*/
class TwentyTwenty_Walker_Comment extends Walker_Comment {
/**
* Outputs a comment in the HTML5 format.
*
* #see wp_list_comments()
* #see https://developer.wordpress.org/reference/functions/get_comment_author_url/
* #see https://developer.wordpress.org/reference/functions/get_comment_author/
* #see https://developer.wordpress.org/reference/functions/get_avatar/
* #see https://developer.wordpress.org/reference/functions/get_comment_reply_link/
* #see https://developer.wordpress.org/reference/functions/get_edit_comment_link/
*
* #param WP_Comment $comment Comment to display.
* #param int $depth Depth of the current comment.
* #param array $args An array of arguments.
*/
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard" onclick="showDiv<?php echo get_comment_id(); ?>()" >
<?php
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
$avatar = get_avatar( $comment, $args['avatar_size'] );
$comme = get_comment_text( $comment );
$id_com = get_comment_id( $comment );
if ( 0 !== $args['avatar_size'] ) {
if ( empty( $comment_author_url ) ) {
echo wp_kses_post( $avatar );
} else {
printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
echo wp_kses_post( $avatar );
}
}
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);
if ( ! empty( $comment_author_url ) ) {
echo '</a>';
}
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* Translators: 1 = comment date, 2 = comment time */
$comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
?>
<time datetime="<?php comment_time( 'c' ); ?>" title="<?php echo esc_attr( $comment_timestamp ); ?>">
<?php echo esc_html( $comment_timestamp ); ?>
</time>
</a>
<?php
if ( get_edit_comment_link() ) {
echo ' <span aria-hidden="true">•</span> <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
}
?>
</div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<div id="<?php echo get_comment_id(); ?>" style=display:none>
<div class="comment-content entry-content">
<?php
echo $comme;
if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}
?></div>
<?php
$comment_reply_link = get_comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="comment-reply">',
'after' => '</span>',
)
)
);
$by_post_author = twentytwenty_is_comment_by_post_author( $comment );
if ( $comment_reply_link || $by_post_author ) {
?>
<footer class="comment-footer-meta">
<?php
if ( $comment_reply_link ) {
echo $comment_reply_link; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>
</footer>
<?php
;}
?>
<script type='text/javascript'>
function showDiv<?php echo get_comment_id(); ?>() {
var id_com = <?php echo $id_com ;?>; //retrieving the id
if (document.getElementById(id_com).style.display == 'none') {
document.getElementById(id_com).style.display = 'block';
} else {
document.getElementById(id_com).style.display = 'none';
}
}
</script>
</div><!-- il mio div -->
</article><!-- .comment-body -->
<?php
}
}
}
I would like to have the respective short description of the product / article displayed in WOOCOMMERCE in the order overview "Order-Details-Item" (template / woocommerce / order / order-details-item.php) about the product Permalink.
I found the following code on the web to allow a brief description.
Here is the code for this:
<div class="product_short_description_cart_default">
<?php
$product_id = $_product->get_parent_id();
$product = wc_get_product($product_id);
echo $product->get_short_description();
?>
</div>
But how do I incorporate them into my order-details-item.php file?
Here is the code for this:
<?php
/**
* Order Item Details
*
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-details-item.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
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
return;
}
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'woocommerce-table__line-item order_item', $item, $order ) ); ?>">
<td class="woocommerce-table__product-name product-name">
<div class="order-details-item-default-product-name-left">
<?php
echo '<div class="product-image">'.$product->get_image(array( 80, 80)).'</div>';
$is_visible = $product && $product->is_visible();
$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
?>
</div>
<div class="order-details-item-default-product-name-right">
<?php
echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '%s', $product_permalink, $item->get_name() ) : $item->get_name(), $item, $is_visible ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$qty = $item->get_quantity();
$refunded_qty = $order->get_qty_refunded_for_item( $item_id );
if ( $refunded_qty ) {
$qty_display = '<del>' . esc_html( $qty ) . '</del> <ins>' . esc_html( $qty - ( $refunded_qty * -1 ) ) . '</ins>';
} else {
$qty_display = esc_html( $qty );
}
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $qty_display ) . '</strong>', $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, false );
wc_display_item_meta( $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, false );
// SKU mit Sprachdatei
echo '<div class="cart-sku-item">' . __( "SKU:", "woostroid") . $product->sku . '</div>';
?>
</div>
</td>
<td class="woocommerce-table__product-total product-total">
<?php echo $order->get_formatted_line_subtotal( $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
</tr>
<?php if ( $show_purchase_note && $purchase_note ) : ?>
<tr class="woocommerce-table__product-purchase-note product-purchase-note">
<td colspan="2"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
</tr>
<?php endif; ?>
Can you please help me there!
Just place below code in functions.php
add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {
$_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
$_var_description ='';
if ( $item['variation_id'] ) {
$_var_description = $_product->get_variation_description();
}else{
$_var_description = $_product->get_short_description();
}
return $_var_description.'<br>'.$item_name;
}
Thanks for your efforts and for the great code.
But did we talk past each other ?!
So I would like to have the following displayed next to the product image:
Product short description
Product name with link
Design / (variants of products)
Item number (SKU NUMBER)
points 2 and 3 and 4 work.
I only miss the first point (product short description) (echo $ product-> get_short_description ();)
Excuse me for my first bad description.
Since WooCommerce major update 3.0+ the "Purchased" column in backend orders list panel has been removed. This column previously showed a toggle list of items in the order for quick viewing.
How to Get back this "items" column in orders panel?
If there is any hook for that? Any ideas?
Thanks
That appears to have been removed for performance reasons, but you could look at the code that was removed and add it back via the manage_shop_order_posts_columns filter and manage_shop_order_posts_custom_column action.
/**
* Modify the custom columns for orders.
* #param array $columns
* #return array
*/
function so_43719068_shop_order_columns( $columns ) {
// the new column as an array for subsequent array manip
$new_column = array( 'order_items' => __( 'Purchased', 'your-plugin' ) );
$insert_after = 'order_title';
// insert after specified column
if( isset( $columns[ $insert_after ] ) ){
// find the "title" column
$index = array_search( $insert_after, array_keys( $columns) );
// reform the array
$columns = array_merge( array_slice( $columns, 0, $index + 1, true ), $new_column, array_slice( $columns, $index, count( $columns ) - $index, true ) );
// or add to end
} else {
$columns = array_merge( $columns, $new_column );
}
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'so_43719068_shop_order_columns', 20 );
/**
* Output custom columns for orders.
* #param string $column
* #param int $post_id
*/
function so_43719068_render_shop_order_columns( $column, $post_id ) {
global $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post_id ) {
$the_order = wc_get_order( $post_id );
}
switch ( $column ) :
case 'order_items' :
/* translators: %d: order items count */
echo '' . apply_filters( 'woocommerce_admin_order_item_count', sprintf( _n( '%d item', '%d items', $the_order->get_item_count(), 'woocommerce' ), $the_order->get_item_count() ), $the_order ) . '';
if ( sizeof( $the_order->get_items() ) > 0 ) {
echo '<table class="show_order_items" cellspacing="0">';
foreach ( $the_order->get_items() as $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $item->get_product(), $item );
$item_meta_html = wc_display_item_meta( $item, array( 'echo' => false ) );
?>
<tr class="<?php echo apply_filters( 'woocommerce_admin_order_item_class', '', $item, $the_order ); ?>">
<td class="qty"><?php echo esc_html( $item->get_quantity() ); ?></td>
<td class="name">
<?php if ( $product ) : ?>
<?php echo ( wc_product_sku_enabled() && $product->get_sku() ) ? $product->get_sku() . ' - ' : ''; ?><?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
<?php else : ?>
<?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
<?php endif; ?>
<?php if ( ! empty( $item_meta_html ) ) : ?>
<?php echo wc_help_tip( $item_meta_html ); ?>
<?php endif; ?>
</td>
</tr>
<?php
}
echo '</table>';
} else echo '–';
break;
endswitch;
}
add_action( 'manage_shop_order_posts_custom_column', 'so_43719068_render_shop_order_columns', 10, 2 );
I have made a function in functions.php which lists items of current categories in the category page. This is the PHP code:
<?php
class trueTopPostsWidget extends WP_Widget {
/*
* создание виджета
*/
function __construct() {
parent::__construct(
'true_top_widget',
'SideBarCategories', // заголовок виджета
array( 'description' => 'Desc' ) // описание
);
}
/*
* фронтэнд виджета
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
if($cat_obj) {
$category_name = $cat_obj->name;
$category_ID = $cat_obj->term_id;
}
function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$params = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => $temp,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($params);
$c = count($subcats);
if ($c>0):?>
<div style="margin-top:20%;">
<h2>Категории товаров</h2>
</div>
<?php endif; echo '<ul class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<li>'.$sc->name.'</li>';
}
echo '</ul>';
}
woocommerce_subcats_from_parentcat_by_ID($category_ID);
echo $c;
echo $args['after_widget'];
}
/*
* бэкэнд виджета
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Заголовок</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
/*
* сохранение настроек виджета
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
/*
* регистрация виджета
*/
function true_top_posts_widget_load() {
register_widget( 'trueTopPostsWidget' );
}
add_action( 'widgets_init', 'true_top_posts_widget_load' );
?>
But I have faced with a problem. I don't want to list categories in the single product page. Now I can see all categories in this page. And it isn't satisfied to my conditions. How can I fix it?
I have solved so:
<div class="unit-30">
<div class="blok">
<?php /* Widgetized sidebar */
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$look_for = "tovar";
$pos = strpos($actual_link,$look_for);
if ($pos==null)
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Сайтбар woocommerce') ) : ?>
<?php endif; ?>
</div>
</div>