RUSSEL86

woocommerce custom code for me

Jan 6th, 2016 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.37 KB | None | 0 0
  1. **========= Display custom text or ACF Custom Field Content product single page after breadcumb  – functions.php =========**
  2. add_action( 'woocommerce_before_single_product', 'enfold_customization_woocommerce_extra_text', 9);
  3. function enfold_customization_woocommerce_extra_text(){
  4. echo do_shortcode('[widget id="text-4"]');
  5. }
  6.  
  7. **========= Display ACF Custom Field Content produce category page  – functions.php =========**
  8. function action_woocommerce_after_shop_loop() {
  9. $term_id = get_queried_object()->term_id;
  10. $post_id = 'product_cat_'.$term_id;
  11. $custom_field = get_field('acf_field_name', $post_id); // My Advanced Custom Field Variable
  12. echo $custom_field;
  13. };
  14. add_action( 'woocommerce_after_shop_loop', 'action_woocommerce_after_shop_loop', 10, 2 );
  15.  
  16. **========= Create and Show Custom Fields =========**
  17. Use  Advanced Custom Fields Plugin to add a WooCommerce custom post type ‘Products’. This is then used to add custom data.
  18. 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:
  19.  
  20. function cw_add_custom_fields()
  21. {
  22.  echo 'CUSTOMIZE CONTENT'.get_post_meta(get_the_ID(), "CUSTOMIZE CONTENT", true);
  23.  echo 'CUSTOMIZEFIELD'.get_field(“CUSTOMIZEFIELD”);
  24. }
  25. add_action( 'woocommerce_single_product_summary', 'cw_add_custom_fields', 21 );
  26. Note:  these changes have to be made in the functions.php file
  27.  
  28. **========= Show number of items in cart and total  =========**
  29. <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>
  30.  
  31. if you want to show only number then follow below code ==========
  32.  
  33. <?php global $woocommerce;
  34. $count = $woocommerce->cart-> cart_contents_count;
  35. if ($count > 0) {
  36. echo '';
  37. echo $count;
  38. echo '';
  39. }
  40. ?>
  41.  
  42.  
  43. ==  or == Display number of items in cart and total
  44.  
  45. global $woocommerce;
  46.  
  47. echo "Items in cart: ".sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
  48. echo "<br />";
  49. echo "Total: ".$woocommerce->cart->get_cart_total();
  50.    
  51.  
  52. **** =====   source link == https://gist.github.com/mikejolley/2044101  ==== *****
  53.  
  54. == Get cart item name, quantity all details woocommerce ==
  55. <?php
  56.     global $woocommerce;
  57.     $items = $woocommerce->cart->get_cart();
  58.  
  59.         foreach($items as $item => $values) {
  60.             $_product = $values['data']->post;
  61.             echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>';
  62.             $price = get_post_meta($values['product_id'] , '_price', true);
  63.             echo "  Price: ".$price."<br>";
  64.         }
  65. ?>
  66.  
  67.  
  68. == To get Product Image and Regular & Sale Price: woocommerce ==
  69. <?php
  70.     global $woocommerce;
  71.     $items = $woocommerce->cart->get_cart();
  72.  
  73.         foreach($items as $item => $values) {
  74.             $_product = $values['data']->post;
  75.             //product image
  76.             $getProductDetail = wc_get_product( $values['product_id'] );
  77.             echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
  78.  
  79.             echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>';
  80.             $price = get_post_meta($values['product_id'] , '_price', true);
  81.             echo "  Price: ".$price."<br>";
  82.             /*Regular Price and Sale Price*/
  83.             echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>";
  84.             echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>";
  85.         }
  86. ?>
  87.  
  88.  
  89.  
  90.  
  91. **  woocommerce custom ajax top cart  **
  92. ================================================
  93. //try this in function.php
  94. -------------------------
  95. add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
  96. function woocommerce_header_add_to_cart_fragment( $fragments )
  97. {
  98.     global $woocommerce;
  99.     ob_start(); ?>
  100.     <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>
  101.     <?php
  102.     $fragments['a.cart-contents'] = ob_get_clean();
  103.     return $fragments;
  104. }
  105.  
  106. //try this in target.php
  107. -------------------------
  108. <div class="cart_contents">
  109.         <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>
  110.     </div>
  111. source link : http://stackoverflow.com/questions/14675292/woocommerce-custom-ajax-top-cart?rq=1
  112.  
  113. ** Add Custom Fee to WooCommerce Cart dynamically **
  114. ====================================================
  115. function woo_add_cart_fee() {
  116.   global $woocommerce;
  117.   $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
  118. }
  119. add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
  120.  
  121. // To multiply it with each product do something like
  122. -----------------------------------------------------
  123. add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
  124. function add_custom_fees( WC_Cart $cart ){
  125.     $fees = 0;
  126.     foreach( $cart->get_cart() as $item ){
  127.        $fees += $item[ 'quantity' ] * 0.08;
  128.     }
  129.     if( $fees != 0 ){
  130.         $cart->add_fee( 'Handling fee', $fees);
  131.     }
  132. }
  133. source link : http://stackoverflow.com/questions/29100694/wordpress-add-extra-fee-in-cart
  134.  
  135. // Add a basic surcharge to all transactions
  136. -------------------------------------------------
  137. /**
  138.  * Add a 1% surcharge to your cart / checkout
  139.  * change the $percentage to set the surcharge to a value to suit
  140.  * Uses the WooCommerce fees API
  141.  * Add to theme functions.php
  142.  */
  143. add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
  144. function woocommerce_custom_surcharge() {
  145.   global $woocommerce;
  146.     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  147.         return;
  148.     $percentage = 0.01;
  149.     $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;   
  150.     $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
  151.  
  152. }
  153.  
  154. source link : https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
  155.  
  156. //   Add Continue Shopping Button on Cart Page
  157. add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' );
  158. function woo_add_continue_shopping_button_to_cart() {
  159. $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
  160. echo '<div class="woocommerce-message">';
  161. echo ' <a href="'.$shop_page_url.'" class="button">Continue Shopping →</a> Need some more products?';
  162. echo '</div>';
  163. }
  164. source link : https://support.woothemes.com/hc/en-us/community/posts/201215287-Add-Continue-Shopping-Button-on-Cart-Page
  165.  
  166.  
  167. //   Change the link of the "Continue Shopping" button in the cart
  168. function wc_custom_redirect_continue_shopping() {
  169.     return 'http://yourdomain.com/your-page/';
  170. }
  171. add_filter( 'woocommerce_continue_shopping_redirect', 'wc_custom_redirect_continue_shopping' );
  172.  
  173. source link : https://support.woothemes.com/hc/en-us/articles/203641565-Change-the-link-of-the-Continue-Shopping-button-in-the-cart
  174.  
  175. // remove product page tab
  176. add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
  177. function woo_remove_product_tabs( $tabs ) {
  178.     unset( $tabs['description'] );          // Remove the description tab
  179.     unset( $tabs['reviews'] );          // Remove the reviews tab
  180.     unset( $tabs['additional_information'] );   // Remove the additional information tab
  181.     return $tabs;
  182.  
  183. }
  184.  
  185. // remove related product on product page
  186. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products',20);
  187.  
  188. // Remove showing 2 results text from the WooCommerce shop page
  189. function woocommerce_result_count() {
  190.         return;
  191. }
  192.  
  193. //Remove WooCommerce Category Product Count
  194. add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
  195. function woo_remove_category_products_count() {
  196.   return;
  197. }
  198.  
  199. //Remove WooCommerce Breadcumb
  200. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
  201.  
  202. //Remove woocommerce ordering
  203. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
  204.  
  205. //Allow HTML in term (category, tag) descriptions
  206. foreach ( array( 'pre_term_description' ) as $filter ) {
  207.     remove_filter( $filter, 'wp_filter_kses' );
  208. }
  209. foreach ( array( 'term_description' ) as $filter ) {
  210.     remove_filter( $filter, 'wp_kses_data' );
  211. }
  212.  
  213. //WooCommerce - Add "update cart" button on all cart items
  214. function fanatic_add_update_cart_item( $product_quantity, $cart_item_key ) {
  215.         global $woocommerce;
  216.         $cart = $woocommerce->cart->get_cart();
  217.         $product = $cart[ $cart_item_key ]['data'];
  218.         $product_id = $product->id;
  219.         if ( $product->is_sold_individually() === false ) {
  220.             $product_quantity .= '<input type="submit" class="update-qty" name="update_cart" value="'.__( 'Update Cart', 'woocommerce' ).'" />';
  221.         }
  222.         return $product_quantity;
  223.     }
  224.     add_filter( 'woocommerce_cart_item_quantity', 'fanatic_add_update_cart_item', 10, 2 );
  225.  
  226.  
  227. //Change the add to cart text on the product page
  228. add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpf_custom_add_cart_text',11);
  229. function wpf_custom_add_cart_text() {
  230.       return __( 'Buy this product!', 'woocommerce' );
  231.  }
  232.  
  233. // Add Min, Max, Increment & Start Value @ Add to Cart Quantity | WooCommerce Single Product Page
  234. add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
  235. function bloomer_woocommerce_quantity_changes( $args, $product ) {
  236.  
  237.    $args['input_value'] = 4; // Start from this value (default = 1)
  238.    $args['max_value'] = 10; // Max quantity (default = n/a)
  239.    $args['min_value'] = 2; // Min quantity (default = 1)
  240.    $args['step'] = 2; // Increment/decrement by this value (default = 1)
  241.    return $args;
  242. }
  243.  
  244. // woo-commerce category/sub category list based category page //
  245. ==============================================================
  246. / Remove ordering product
  247. remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
  248. add_action( 'woocommerce_before_shop_loop', 'woocommerce_category_menu');
  249.  
  250. function woocommerce_category_menu() {
  251.  if (is_product_category()) {
  252.   $parentid = get_queried_object_id();
  253.   $args = array(
  254.    'parent' => $parentid
  255.   );  
  256.   // Get the category
  257.   $this_category = get_category($parentid);
  258.   $parent_category = get_category($this_category->category_parent);
  259.   $getParentCatId = $parent_category->cat_ID;  
  260.   $args2 = array(
  261.    'parent' => $getParentCatId
  262.   );  
  263.   $terms = get_terms( 'product_cat', $args );
  264.   $children = get_terms( 'product_cat', $args2);
  265.   if( $terms ) {
  266.    echo '<ul id="category_menu" class="menu">';
  267.    foreach ( $terms as $term ) {
  268.     echo '<li class="category">';    
  269.      echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
  270.       echo $term->name;
  271.      echo '</a>';
  272.     echo '</li>';
  273.    }  
  274.    echo '</ul>';
  275.   }elseif( is_tax('product_cat') ){
  276.    echo '<ul id="category_menu" class="menu">';
  277.     foreach ( $children as $child ) {
  278.      echo '<li class="category">';  
  279.       echo '<a href="' . esc_url( get_term_link( $child ) ) . '" class="' . $child->slug . '">';
  280.        echo $child->name;
  281.       echo '</a>';
  282.      echo '</li>';
  283.    }
  284.    echo '</ul>';
  285.   }
  286.  }
  287. }
  288.  
  289. /**
  290.  * Change number of products that are displayed per page (shop page)
  291.  */
  292. add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
  293.  
  294. function new_loop_shop_per_page( $cols ) {
  295.   // $cols contains the current number of products per page based on the value stored on Options -> Reading
  296.   // Return the number of products you wanna show per page.
  297.   $cols = 12;
  298.   return $cols;
  299. }
  300.  
  301.  
  302.  
  303.  
  304. if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
  305. /**
  306. * Get the product thumbnail, or the placeholder if not set.
  307. *
  308. * @subpackage Loop
  309. * @param string $size (default: 'shop_catalog')
  310. * @param int $deprecated1 Deprecated since WooCommerce 2.0 (default: 0)
  311. * @param int $deprecated2 Deprecated since WooCommerce 2.0 (default: 0)
  312. * @return string
  313. */
  314. function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $deprecated1 = 0, $deprecated2 = 0 ) {
  315. global $post;
  316. if ( has_post_thumbnail() ) {
  317. return '<a href="' . get_permalink( $post->ID ) . '">' . get_the_post_thumbnail( $post->ID, $size ) . '</a>';
  318. } elseif ( wc_placeholder_img_src() ) {
  319. return wc_placeholder_img( $size );
  320. }
  321. }
  322. }
  323.  
  324.  
  325.  
  326. function sv_add_logo_above_wc_shop_image() {
  327.  
  328.     global $product;
  329.  
  330.     // replace the custom field name logo_src with your own
  331.     $logo_src = get_post_meta( $product->id, 'extra_badge', true );
  332.  
  333.     // Add these fields to the shop loop if set
  334.     if ( ! empty( $logo_src ) ) {
  335.         echo '<h4 class="newbadge">' . $logo_src . '</h4>';
  336.        
  337.     }
  338.  
  339. }
  340. add_action( 'woocommerce_before_shop_loop_item_title', 'sv_add_logo_above_wc_shop_image', 9 );
  341.  
  342.  
  343.  
  344. function extra_add_logo_belowtitle() {
  345.  
  346.     global $product;
  347. $show_brand_logo = get_field('show_brand_logo');
  348. if( ! empty( $show_brand_logo ) ) {  
  349.     $extra_logo = get_field('extra_logo');
  350.     //var_dump($extra_logo);
  351.     if ( ! empty( $extra_logo ) ) {
  352. ?>
  353. <div class="brand_logo">   
  354. <?php
  355. echo $extra_logo['label']; ?>
  356.    
  357. </div><?php }
  358.    
  359.  }
  360.  
  361. }
  362. add_action( 'woocommerce_after_shop_loop_item_title', 'extra_add_logo_belowtitle', 9 );
  363.  
  364.  
  365. //use the woocommerce_order_item_name for order email template add extra brand logo name
  366. function sww_edit_order_item_name(  $name, $item ) {
  367. $product_id = $item['product_id'];
  368. $show_brand_logo = get_field('show_brand_logo',$product_id);
  369. if( ! empty( $show_brand_logo ) ) {  
  370.     $extra_logo = get_field('extra_logo',$product_id);
  371.     if ( ! empty( $extra_logo ) ) {
  372.     $label =  $extra_logo['value'];
  373. }  
  374.    
  375.  } 
  376.     return $name . $label;
  377. }
  378. add_filter( 'woocommerce_order_item_name', 'sww_edit_order_item_name', 11, 3 );
Add Comment
Please, Sign In to add comment