Forum replies created

  1. Manik Malhotra
    Keymaster
    Posts: 64
    April 9, 2020 at 7:34 am #5564
    Manik Malhotra

    Hi Gagandeep,

    So you if you want to add some columns in the orders Admin list page (in backend):

    ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST

    In the example below, we add 2 new custom columns, before existing “Total” and “Actions” columns.

    // ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
    function custom_shop_order_column($columns)
    {
    $reordered_columns = array();
    
    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
    $reordered_columns[$key] = $column;
    if( $key == 'order_status' ){
    // Inserting after "Status" column
    $reordered_columns['my-column1'] = __( 'Title1','theme_domain');
    $reordered_columns['my-column2'] = __( 'Title2','theme_domain');
    }
    }
    return $reordered_columns;
    }
    
    // Adding custom fields meta data for each new column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
    function custom_orders_list_column_content( $column, $post_id )
    {
    switch ( $column )
    {
    case 'my-column1' :
    // Get custom post meta data
    $my_var_one = get_post_meta( $post_id, '_the_meta_key1', true );
    if(!empty($my_var_one))
    echo $my_var_one;
    
    // Testing (to be removed) - Empty value case
    else
    echo '<small>(<em>no value</em>)</small>';
    
    break;
    
    case 'my-column2' :
    // Get custom post meta data
    $my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );
    if(!empty($my_var_two))
    echo $my_var_two;
    
    // Testing (to be removed) - Empty value case
    else
    echo '<small>(<em>no value</em>)</small>';
    
    break;
    }
    }

    Code goes in function.php file of your active theme. Tested and works.

  2. Manik Malhotra
    Keymaster
    Posts: 64
    April 9, 2020 at 7:32 am #5563
    Manik Malhotra

    Hi Gagandeep,

    This requires 2 functions that will add a new column

    The second function hook is a composite hook: woocommerce_my_account_my_orders_column_{$column_id} where {$column_id} need to be replaced by the column key slug that is set in the first function.

    That second function manage the displayed row values and you can add for example a custom field to get custom order meta data values.

    The code:

    add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
    function add_account_orders_column( $columns ){
    $columns['custom-column'] = __( 'New Column', 'woocommerce' );
    
    return $columns;
    }
    
    add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
    function add_account_orders_column_rows( $order ) {
    // Example with a custom field
    if ( $value = $order->get_meta( '_custom_field' ) ) {
    echo esc_html( $value );
    }
    }

    Code goes in function.php file of your active theme. Tested and works.

  3. Manik Malhotra
    Keymaster
    Posts: 64
    March 18, 2020 at 9:18 am #5456
    Manik Malhotra

    Hi Gagandeep,

    You will open the woo commerce settings option in admin and there is a tab for “Payments” and select “Custom Payment ” Tab and add your details in the custom payment tab.

    Thanks

  4. Manik Malhotra
    Keymaster
    Posts: 64
    March 16, 2020 at 5:15 am #5446
    Manik Malhotra

    Hi Gagandeep,

    Here is the code for overriding price of product in cart

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
    
    function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price
    foreach ( $cart_object->cart_contents as $key => $value ) {
    $value['data']->price = $custom_price;
    // for WooCommerce version 3+ use:
    // $value['data']->set_price($custom_price);
    }
    }

    Hope it will work.

    Thanks

  5. Manik Malhotra
    Keymaster
    Posts: 64
    February 28, 2020 at 12:01 pm #5405
    Manik Malhotra

    Hi,

    <?php
    
    $taxonomy = 'product_cat';
    $orderby = 'name';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $title = '';
    $empty = 0;
    
    $args = array(
    'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title,
    'hide_empty' => $empty
    );
    $all_categories = get_categories( $args );
    foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
    $category_id = $cat->term_id;
    echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
    
    $args2 = array(
    'taxonomy' => $taxonomy,
    'child_of' => 0,
    'parent' => $category_id,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title,
    'hide_empty' => $empty
    );
    $sub_cats = get_categories( $args2 );
    if($sub_cats) {
    foreach($sub_cats as $sub_category) {
    echo $sub_category->name ;
    }
    }
    }
    }
    ?>

    This will list all the top level categories and subcategories under them hierarchically. do not use the inner query if you just want to display the top level categories. Thanks

  6. Manik Malhotra
    Keymaster
    Posts: 64
    February 28, 2020 at 11:46 am #5404
    Manik Malhotra

    Hi Gagandeep,

    This version will work globally on all product variations in the cart for this defined variable product:

    add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
    function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();
    
    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;
    
    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
    if( $cart_item['product_id'] == $targeted_product_id ){
    $qty = intval( $cart_item['quantity'] );
    for( $i = 0; $i < $qty; $i++ )
    $items_prices[] = floatval( $cart_item['data']->get_price());
    }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
    if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );
    
    if( $discount != 0 ){
    // Displaying a custom notice (optional)
    wc_clear_notices();
    wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
    
    // The discount
    $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true );
    # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
    }

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested on Woocommerce 3+ and works.

    Thanks

  7. Manik Malhotra
    Keymaster
    Posts: 64
    February 25, 2020 at 8:01 am #5382
    Manik Malhotra

    Hi Gagandeep,

    You can add that div conditionally like the following and then the div will be shown only on shop page

    if ( is_shop() ) {
    echo '<div>Choose a category below</div>';
    }

    Thanks

  8. Manik Malhotra
    Keymaster
    Posts: 64
    February 25, 2020 at 7:56 am #5380
    Manik Malhotra

    Hi Gagandeep,

    Make sure to include the codes below for your theme’s functions.php file.

    And also this will Ajaxify your cart to be updated automatically without reloading the page.

    // Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.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;
    
    }

    Thanks

  9. Manik Malhotra
    Keymaster
    Posts: 64
    February 12, 2020 at 7:01 am #5110
    Manik Malhotra

    Hi Gagandeep,

    You just need to create an extension for WooCommerce to handle it. You can look at their existing extensions as samples and just change them accordingly to use your gateway instead of PayPal or whatever sample you’re looking at.

    WooCommerce is built using WordPress standards, so it works exactly the same as building any other WP plugin.

    Thanks

  10. Manik Malhotra
    Keymaster
    Posts: 64
    February 12, 2020 at 6:33 am #5106
    Manik Malhotra

    Hi Gagandeep,

    This action hook put some content after the billing form.

    add_action(‘woocommerce_after_checkout_billing_form’, ‘before_payment_func’);
    function before_payment_func() {
    echo ‘some content’;
    }

    Thanks

     

  11. Manik Malhotra
    Keymaster
    Posts: 64
    February 10, 2020 at 4:54 am #5075
    Manik Malhotra

    Hi Gagandeep,

    A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.

    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($terms as $term) {
    $product_cat_id = $term->term_id;
    break;
    }

    Please look into the meta.php file in the “templates/single-product/” folder of the WooCommerce plugin.

    <?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>

    Thanks

  12. Manik Malhotra
    Keymaster
    Posts: 64
    February 10, 2020 at 4:50 am #5073
    Manik Malhotra

    Hi Gagandeep,

    Try this example,

    So given a category with ID 26, the following code would return it’s products (WooCommerce 3+):

     

    $args = array(
    ‘post_type’ => ‘product’,
    ‘post_status’ => ‘publish’,
    ‘ignore_sticky_posts’ => 1,
    ‘posts_per_page’ => ’12’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘term_id’, //This is optional, as it defaults to ‘term_id’
    ‘terms’ => 26,
    ‘operator’ => ‘IN’ // Possible values are ‘IN’, ‘NOT IN’, ‘AND’.
    ),
    array(
    ‘taxonomy’ => ‘product_visibility’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘exclude-from-catalog’, // Possibly ‘exclude-from-search’ too
    ‘operator’ => ‘NOT IN’
    )
    )
    );
    $products = new WP_Query($args);
    var_dump($products);

    Thanks

  13. Manik Malhotra
    Keymaster
    Posts: 64
    February 7, 2020 at 12:11 pm #5007
    Manik Malhotra

    Hi Gagandeep,

    The path is wp-content/plugins/woocommerce/templates/checkout/form-checkout.php is the main file.

    Thanks

  14. Manik Malhotra
    Keymaster
    Posts: 64
    February 4, 2020 at 7:10 am #4824
    Manik Malhotra

    Hi Gagandeep,

    There is some errors in your code. Try this instead:

    // Add the the product custom field as item meta data in the order
    add_action( ‘woocommerce_add_order_item_meta’, ‘pd_number_order_meta_data’, 10, 3 );
    function pd_number_order_meta_data( $item_id, $cart_item, $cart_item_key ) {
    // get the product custom field value
    $pd_number = get_post_meta( $cart_item[ ‘product_id’ ], ‘_pd_number’, true );

    // Add the custom field value to order item meta
    if( ! empty($pd_number) )
    wc_update_order_item_meta( $item_id, ‘_pd_number’, $pd_number );
    }
    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Thanks

  15. Manik Malhotra
    Keymaster
    Posts: 64
    February 3, 2020 at 7:35 am #4819
    Manik Malhotra

    Hi Gagandeep,

    Try woocommerce_related_products hook in the following hooked function, to exclude a specific product category from displayed related products:

    add_filter( 'woocommerce_related_products', 'exclude_product_category_from_related_products', 10, 3 );
    function exclude_product_category_from_related_products( $related_posts, $product_id, $args ){
    // HERE define your product category slug
    $term_slug = 'hoodies';
    
    // Get the product Ids in the defined product category
    $exclude_ids = wc_get_products( array(
    'status' => 'publish',
    'limit' => -1,
    'category' => array($term_slug),
    'return' => 'ids',
    ) );
    
    return array_diff( $related_posts, $exclude_ids );
    }

    Code goes in function.php file of the active child theme (or active theme).

    Tested and works.

    Thanks