Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **========= Display custom text or ACF Custom Field Content product single page after breadcumb – functions.php =========**
- add_action( 'woocommerce_before_single_product', 'enfold_customization_woocommerce_extra_text', 9);
- function enfold_customization_woocommerce_extra_text(){
- echo do_shortcode('[widget id="text-4"]');
- }
- **========= Display ACF Custom Field Content produce category page – functions.php =========**
- function action_woocommerce_after_shop_loop() {
- $term_id = get_queried_object()->term_id;
- $post_id = 'product_cat_'.$term_id;
- $custom_field = get_field('acf_field_name', $post_id); // My Advanced Custom Field Variable
- echo $custom_field;
- };
- add_action( 'woocommerce_after_shop_loop', 'action_woocommerce_after_shop_loop', 10, 2 );
- **========= Create and Show Custom Fields =========**
- Use Advanced Custom Fields Plugin to add a WooCommerce custom post type ‘Products’. This is then used to add custom data.
- Now in order to display the custom data on the product summary page, I will use a function that hook the fields to a WooCOmmerce action (with the desired priority). In the following code example, I will display custom fields after product excerpt but before add-to-cart:
- function cw_add_custom_fields()
- {
- echo 'CUSTOMIZE CONTENT'.get_post_meta(get_the_ID(), "CUSTOMIZE CONTENT", true);
- echo 'CUSTOMIZEFIELD'.get_field(“CUSTOMIZEFIELD”);
- }
- add_action( 'woocommerce_single_product_summary', 'cw_add_custom_fields', 21 );
- Note: these changes have to be made in the functions.php file
- **========= Show number of items in cart and total =========**
- <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
- if you want to show only number then follow below code ==========
- <?php global $woocommerce;
- $count = $woocommerce->cart-> cart_contents_count;
- if ($count > 0) {
- echo '';
- echo $count;
- echo '';
- }
- ?>
- == or == Display number of items in cart and total
- global $woocommerce;
- echo "Items in cart: ".sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
- echo "<br />";
- echo "Total: ".$woocommerce->cart->get_cart_total();
- **** ===== source link == https://gist.github.com/mikejolley/2044101 ==== *****
- == Get cart item name, quantity all details woocommerce ==
- <?php
- global $woocommerce;
- $items = $woocommerce->cart->get_cart();
- foreach($items as $item => $values) {
- $_product = $values['data']->post;
- echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
- $price = get_post_meta($values['product_id'] , '_price', true);
- echo " Price: ".$price."<br>";
- }
- ?>
- == To get Product Image and Regular & Sale Price: woocommerce ==
- <?php
- global $woocommerce;
- $items = $woocommerce->cart->get_cart();
- foreach($items as $item => $values) {
- $_product = $values['data']->post;
- //product image
- $getProductDetail = wc_get_product( $values['product_id'] );
- echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
- echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
- $price = get_post_meta($values['product_id'] , '_price', true);
- echo " Price: ".$price."<br>";
- /*Regular Price and Sale Price*/
- echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>";
- echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>";
- }
- ?>
- ** woocommerce custom ajax top cart **
- ================================================
- //try this in function.php
- -------------------------
- add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
- function woocommerce_header_add_to_cart_fragment( $fragments )
- {
- global $woocommerce;
- ob_start(); ?>
- <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>
- <?php
- $fragments['a.cart-contents'] = ob_get_clean();
- return $fragments;
- }
- //try this in target.php
- -------------------------
- <div class="cart_contents">
- <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php echo $woocommerce->cart->get_cart_total(); ?></a>
- </div>
- source link : http://stackoverflow.com/questions/14675292/woocommerce-custom-ajax-top-cart?rq=1
- ** Add Custom Fee to WooCommerce Cart dynamically **
- ====================================================
- function woo_add_cart_fee() {
- global $woocommerce;
- $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
- }
- add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
- // To multiply it with each product do something like
- -----------------------------------------------------
- add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
- function add_custom_fees( WC_Cart $cart ){
- $fees = 0;
- foreach( $cart->get_cart() as $item ){
- $fees += $item[ 'quantity' ] * 0.08;
- }
- if( $fees != 0 ){
- $cart->add_fee( 'Handling fee', $fees);
- }
- }
- source link : http://stackoverflow.com/questions/29100694/wordpress-add-extra-fee-in-cart
- // Add a basic surcharge to all transactions
- -------------------------------------------------
- /**
- * Add a 1% surcharge to your cart / checkout
- * change the $percentage to set the surcharge to a value to suit
- * Uses the WooCommerce fees API
- * Add to theme functions.php
- */
- add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
- function woocommerce_custom_surcharge() {
- global $woocommerce;
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- $percentage = 0.01;
- $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
- $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
- }
- source link : https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
- // Add Continue Shopping Button on Cart Page
- add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' );
- function woo_add_continue_shopping_button_to_cart() {
- $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
- echo '<div class="woocommerce-message">';
- echo ' <a href="'.$shop_page_url.'" class="button">Continue Shopping →</a> Need some more products?';
- echo '</div>';
- }
- source link : https://support.woothemes.com/hc/en-us/community/posts/201215287-Add-Continue-Shopping-Button-on-Cart-Page
- // Change the link of the "Continue Shopping" button in the cart
- function wc_custom_redirect_continue_shopping() {
- return 'http://yourdomain.com/your-page/';
- }
- add_filter( 'woocommerce_continue_shopping_redirect', 'wc_custom_redirect_continue_shopping' );
- source link : https://support.woothemes.com/hc/en-us/articles/203641565-Change-the-link-of-the-Continue-Shopping-button-in-the-cart
- // remove product page tab
- add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
- function woo_remove_product_tabs( $tabs ) {
- unset( $tabs['description'] ); // Remove the description tab
- unset( $tabs['reviews'] ); // Remove the reviews tab
- unset( $tabs['additional_information'] ); // Remove the additional information tab
- return $tabs;
- }
- // remove related product on product page
- remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products',20);
- // Remove showing 2 results text from the WooCommerce shop page
- function woocommerce_result_count() {
- return;
- }
- //Remove WooCommerce Category Product Count
- add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
- function woo_remove_category_products_count() {
- return;
- }
- //Remove WooCommerce Breadcumb
- remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
- //Remove woocommerce ordering
- remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
- //Allow HTML in term (category, tag) descriptions
- foreach ( array( 'pre_term_description' ) as $filter ) {
- remove_filter( $filter, 'wp_filter_kses' );
- }
- foreach ( array( 'term_description' ) as $filter ) {
- remove_filter( $filter, 'wp_kses_data' );
- }
- //WooCommerce - Add "update cart" button on all cart items
- function fanatic_add_update_cart_item( $product_quantity, $cart_item_key ) {
- global $woocommerce;
- $cart = $woocommerce->cart->get_cart();
- $product = $cart[ $cart_item_key ]['data'];
- $product_id = $product->id;
- if ( $product->is_sold_individually() === false ) {
- $product_quantity .= '<input type="submit" class="update-qty" name="update_cart" value="'.__( 'Update Cart', 'woocommerce' ).'" />';
- }
- return $product_quantity;
- }
- add_filter( 'woocommerce_cart_item_quantity', 'fanatic_add_update_cart_item', 10, 2 );
- //Change the add to cart text on the product page
- add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpf_custom_add_cart_text',11);
- function wpf_custom_add_cart_text() {
- return __( 'Buy this product!', 'woocommerce' );
- }
- // Add Min, Max, Increment & Start Value @ Add to Cart Quantity | WooCommerce Single Product Page
- add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
- function bloomer_woocommerce_quantity_changes( $args, $product ) {
- $args['input_value'] = 4; // Start from this value (default = 1)
- $args['max_value'] = 10; // Max quantity (default = n/a)
- $args['min_value'] = 2; // Min quantity (default = 1)
- $args['step'] = 2; // Increment/decrement by this value (default = 1)
- return $args;
- }
- // woo-commerce category/sub category list based category page //
- ==============================================================
- / Remove ordering product
- remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
- add_action( 'woocommerce_before_shop_loop', 'woocommerce_category_menu');
- function woocommerce_category_menu() {
- if (is_product_category()) {
- $parentid = get_queried_object_id();
- $args = array(
- 'parent' => $parentid
- );
- // Get the category
- $this_category = get_category($parentid);
- $parent_category = get_category($this_category->category_parent);
- $getParentCatId = $parent_category->cat_ID;
- $args2 = array(
- 'parent' => $getParentCatId
- );
- $terms = get_terms( 'product_cat', $args );
- $children = get_terms( 'product_cat', $args2);
- if( $terms ) {
- echo '<ul id="category_menu" class="menu">';
- foreach ( $terms as $term ) {
- echo '<li class="category">';
- echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
- echo $term->name;
- echo '</a>';
- echo '</li>';
- }
- echo '</ul>';
- }elseif( is_tax('product_cat') ){
- echo '<ul id="category_menu" class="menu">';
- foreach ( $children as $child ) {
- echo '<li class="category">';
- echo '<a href="' . esc_url( get_term_link( $child ) ) . '" class="' . $child->slug . '">';
- echo $child->name;
- echo '</a>';
- echo '</li>';
- }
- echo '</ul>';
- }
- }
- }
- /**
- * Change number of products that are displayed per page (shop page)
- */
- add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
- function new_loop_shop_per_page( $cols ) {
- // $cols contains the current number of products per page based on the value stored on Options -> Reading
- // Return the number of products you wanna show per page.
- $cols = 12;
- return $cols;
- }
- if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
- /**
- * Get the product thumbnail, or the placeholder if not set.
- *
- * @subpackage Loop
- * @param string $size (default: 'shop_catalog')
- * @param int $deprecated1 Deprecated since WooCommerce 2.0 (default: 0)
- * @param int $deprecated2 Deprecated since WooCommerce 2.0 (default: 0)
- * @return string
- */
- function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $deprecated1 = 0, $deprecated2 = 0 ) {
- global $post;
- if ( has_post_thumbnail() ) {
- return '<a href="' . get_permalink( $post->ID ) . '">' . get_the_post_thumbnail( $post->ID, $size ) . '</a>';
- } elseif ( wc_placeholder_img_src() ) {
- return wc_placeholder_img( $size );
- }
- }
- }
- function sv_add_logo_above_wc_shop_image() {
- global $product;
- // replace the custom field name logo_src with your own
- $logo_src = get_post_meta( $product->id, 'extra_badge', true );
- // Add these fields to the shop loop if set
- if ( ! empty( $logo_src ) ) {
- echo '<h4 class="newbadge">' . $logo_src . '</h4>';
- }
- }
- add_action( 'woocommerce_before_shop_loop_item_title', 'sv_add_logo_above_wc_shop_image', 9 );
- function extra_add_logo_belowtitle() {
- global $product;
- $show_brand_logo = get_field('show_brand_logo');
- if( ! empty( $show_brand_logo ) ) {
- $extra_logo = get_field('extra_logo');
- //var_dump($extra_logo);
- if ( ! empty( $extra_logo ) ) {
- ?>
- <div class="brand_logo">
- <?php
- echo $extra_logo['label']; ?>
- </div><?php }
- }
- }
- add_action( 'woocommerce_after_shop_loop_item_title', 'extra_add_logo_belowtitle', 9 );
- //use the woocommerce_order_item_name for order email template add extra brand logo name
- function sww_edit_order_item_name( $name, $item ) {
- $product_id = $item['product_id'];
- $show_brand_logo = get_field('show_brand_logo',$product_id);
- if( ! empty( $show_brand_logo ) ) {
- $extra_logo = get_field('extra_logo',$product_id);
- if ( ! empty( $extra_logo ) ) {
- $label = $extra_logo['value'];
- }
- }
- return $name . $label;
- }
- add_filter( 'woocommerce_order_item_name', 'sww_edit_order_item_name', 11, 3 );
Add Comment
Please, Sign In to add comment