I am trying to update multiple divs but can`t get it working.
This works as expected.
add_filter( 'woocommerce_add_to_cart_fragments', 'update_basket' );
function update_basket( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="bas" style=""><?php echo WC()->cart->get_cart_total(); ?></span>
<?php
$fragments['.bas'] = ob_get_clean();
return $fragments;
}
When I add the second div class, the output doesn`t get updated at all.
add_filter( 'woocommerce_add_to_cart_fragments', 'update_basket' );
function update_basket( $fragments ) {
global $woocommerce;
ob_start();
?>
<span class="bas" style=""><?php echo WC()->cart->get_cart_total(); ?></span>
<?php $fragments['.bas'] = ob_get_clean(); ?>
<?php ob_start(); ?>
<div class="search-form"> Your Basket:<br/>
<a style="" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"> <?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</div>
<?php
$fragments['.search-form'] = ob_get_clean();
return $fragments;
}
Related
I have add a new field via admin panel in products product_icon
where I put an SVG icon code. I need to use this SVG plugin. I have put into woocommerce/includes/abstracts/abstract-wc-product.php new function.
public function get_icon( $context = 'view' ) {
return $this->get_prop( 'icon', $context );
}
when I put it into a while loop where I have list products name, prices, etc new function get_icon() I have null. In SQL post meta meta key is ok "product_icon" for a correct product with the correct content (SVG code)
My listing Code:
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$product = wc_get_product( get_the_ID() );
?>
<div class="eb-woo-products-col">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<div class="producttabwhite eb-woo-product">
<?php } else { ?>
<div class="producttabblue eb-woo-product">
<?php } ?>
<div id="mydiv" class="eb-woo-product-image-wrapper" style="margin-top:2.5rem;margin-right:1.25rem;">
<?php echo wp_kses_post( $product->get_image( 'woocommerce_thumbnail' ) ); ?>
<?php if ( 'grid' === $layout ) { ?>
<div class="eb-woo-product-overlay">
<div class="eb-woo-product-button-list">
<?php // woocommerce_template_loop_add_to_cart(); ?>
</div>
</div>
<?php } ?>
</div>
<div class="eb-woo-product-content-wrapper1">
<div class="ml40px mt33px col-sm-8">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="producttitle">
<a class="producttitle" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } else { ?>
<p class="producttitlewhite">
<a class="producttitlewhite" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } ?>
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="productprice">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<p>Icon code<?php echo $product->get_icon(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } else { ?>
<p class="productpricewhite">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
wp_reset_postdata();
} else {
?>
<p><?php _e( 'No product found', 'essential-blocks' ); ?></p>
<?php
}
?>
Well, you don't have to edit woocommerce/includes/abstracts/abstract-wc-product.php file to fetch the meta key and the get_prop function doesn't work that way that you're assuming.
You can simply use get_post_meta function to fetch your meta key value for the product.
so you just need to replace <p><?php echo $product->get_icon(); ?></p>
with the below code:
<p><?php echo get_post_meta( $product->get_id(), 'product_icon', true ); ?></p>
How to hide counter when cart is empty? Wordpress - woocommerce - supro theme
<a href="<?php echo esc_url( wc_get_cart_url() ) ?>" class="cart-contents" id="icon-cart-contents">
<?php echo wp_kses_post( $icon_cart ); ?>
<span class="label-item cart-label"><?php echo wp_kses( $cart_html, wp_kses_allowed_html( 'post' ) ); ?></span>
<span class="mini-cart-counter"><?php echo intval( $woocommerce->cart->cart_contents_count ); ?></span>
</a>
Tnx
Updated: Add this code at your child theme’s functions.php
add_action( 'wp_head', 'x_hide_cart' );
function x_hide_cart(){
if ( WC()->cart->get_cart_contents_count() == 0 ) {
?>
<style type="text/css">.x-menu-item-woocommerce{display: none;}</style>
<?php
}
}
I am new to Wordpress development and am trying to install a traffic junky tracking code on the thankyou.php page.
My attempt has failed so far and I think it boils down to not understanding PHP well enough.
The two tracking codes from trafficjunky that were available were HTML or PHP
HTML:
<img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=[RANDOM_NUMBER]&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
PHP:
<?php
$randomNumber = time() . mt_rand(1000, 9999999);
$currentPage = substr($_SERVER["REQUEST_URI"], 0, 255);
?>
<img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=<?=$randomNumber ?>&epu=<?=$currentPage ?>&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
I tried to install the HTML version after the order processes under some PHP code on the thankyou.php WooCommerce template:
**<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>
<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
<li class="woocommerce-order-overview__order order">
<?php _e( 'Order number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); ?></strong>
</li>
<li class="woocommerce-order-overview__date date">
<?php _e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo wc_format_datetime( $order->get_date_created() ); ?></strong>
</li>
<?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
<li class="woocommerce-order-overview__email email">
<?php _e( 'Email:', 'woocommerce' ); ?>
<strong><?php echo $order->get_billing_email(); ?></strong>
</li>
<?php endif; ?>
<li class="woocommerce-order-overview__total total">
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?></strong>
</li>
<?php if ( $order->get_payment_method_title() ) : ?>
<li class="woocommerce-order-overview__payment-method method">
<?php _e( 'Payment method:', 'woocommerce' ); ?>
<strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
</li>
<?php endif; ?>
</ul>
<img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=[RANDOM_NUMBER]&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->get_payment_method(), $order->get_id() ); ?>
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>
<?php endif; ?>
</div>**
This led to order errors so i am trying to figure out how to add the PHP version of the tracking code safely without throwing errors.
Any help would be appreciated.
Thank you for the replies, I have set up the function in functions.php as instructed but I am still having trouble getting the tracking code to fire. Here is my current code:
add_action( 'woocommerce_thankyou', 'tracking_code_thankyou', 10, 1 );
function tracking_code_thankyou($order_id){
$random_number = time() . mt_rand(1000, 9999999);
$current_page = substr($_SERVER["REQUEST_URI"], 0, 255);
?>
<em>Your tracking code just below (for testing)</em>
<img id="1000145711_cpa_testing" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000145711&member_id=1000785411&cb=<?=$randomNumber ?>&epu=<?=$currentPage ?>&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
<?php
}
}
You can try adding the php tracking code inside the header.php (before the end of the head tag) or footer.php (before the end of the body tag). You need to get the id of the page if you want it to run exclusively on that page only.
Do something like this
if(is_page(yourpageidhere)):
//paste the tracking code here
endif;
UPDATED
Instead of overriding the thankyou.php template as you can see its source code there is this line:
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
Which means that you can use this woocommerce_thankyou action hook with a custom function, to output anything in this template line. First get the template clean of your code...
Also I have corrected your code in the image link, as it was not correct. You should use the following code in the function.php file of your active child theme or active theme:
add_action( 'woocommerce_thankyou', 'tracking_code_thankyou', 10, 1 );
function tracking_code_thankyou( $order_id ) {
$random_number = time() . mt_rand(1000, 9999999);
$current_page = substr($_SERVER["REQUEST_URI"], 0, 255);
$url = "https://ads.trafficjunky.net/tj_ads_pt?a=1000145711&member_id=1000785411&cb=$randomNumber&epu=$currentPage&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]";
echo '<em>Your tracking code just below (for testing)</em>
<img id="1000143661_tester" src="'.$url.'" width="1" height="1" border="0" />';
}
It will output your code just after customers details at the end of the order-received page…
Does somebody know, where I can find the .php file in woocommerce, where I can remove the word "item" or "items" from the last span?
I've tried it with some jQuery Code but it only works when I load the page completely. When I click add to cart or remove from cart an item, the cart only reload in woocommerce without my .js file to remove the two words.
Can anybody help me?
Thank you
$('.count').html($('.count').html().replace(' items',''));
$('.count').html($('.count').html().replace(' item',''));
<a class="cart-contents" href="http://*****.de/warenkorb/" title="View your shopping cart">
<span class="amount">0,00 €</span>
<span class="count">0 items</span><!--Here I want to remove the Word items to show just the number-->
</a>
After a few days of breaking my head about this i've found a solution (I'm so happy and angry too because when you know the answer the solution is so easy).
First you have to find the file woocommerce/templates/cart/mini-cart.php to overwrite our function.
When you've found it you have to find following line:
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?></li>
After you've found it you have to insert following code under the line:
<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
<span class="count"><?php echo sprintf (_n( '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></span>
</a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>
Now you have to save the file and reload the page and put something in your cart (or remove) to update your cart. Know it should be done! :-)
If you want to add your price to the header too you also have to add above <span class="count"> following lines of code:
<span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span>
If you have any questions you can always comment me…
To avoid the risk of these adjustments being overwritten if you update Storefront, you can also rewrite the function in your functions.php file like this:
if ( ! function_exists( 'storefront_cart_link' ) ) {
function storefront_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
<?php /* translators: %d: number of items in cart */ ?>
<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
</a>
<?php
}
}
I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>