Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter charlotteb22

    (@charlotteb22)

    <?php
    
    /**
    
     * Understrap functions and definitions
    
     *
    
     * @package understrap
    
     */
    
    if (!defined('ABSPATH')) {
    
        exit; // Exit if accessed directly.
    
    }
    
    $understrap_includes = array(
    
        '/theme-settings.php', // Initialize theme default settings.
    
        '/setup.php', // Theme setup and custom theme supports.
    
        '/widgets.php', // Register widget area.
    
        '/enqueue.php', // Enqueue scripts and styles.
    
        '/template-tags.php', // Custom template tags for this theme.
    
        '/pagination.php', // Custom pagination for this theme.
    
        '/hooks.php', // Custom hooks.
    
        '/extras.php', // Custom functions that act independently of the theme templates.
    
        '/customizer.php', // Customizer additions.
    
        '/custom-comments.php', // Custom Comments file.
    
        '/jetpack.php', // Load Jetpack compatibility file.
    
        '/class-wp-bootstrap-navwalker.php', // Load custom WordPress nav walker.
    
        '/woocommerce.php', // Load WooCommerce functions.
    
        '/editor.php', // Load Editor functions.
    
        '/deprecated.php', // Load deprecated functions.
    
    );
    
    foreach ($understrap_includes as $file) {
    
        $filepath = locate_template('inc' . $file);
    
        if (!$filepath) {
    
            trigger_error(sprintf('Error locating /inc%s for inclusion', $file), E_USER_ERROR);
    
        }
    
        require_once $filepath;
    
    }
    
    function variation_radio_buttons($html, $args)
    
    {
    
        $args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args', $args), array(
    
            'options' => false,
    
            'attribute' => false,
    
            'product' => false,
    
            'selected' => false,
    
            'name' => '',
    
            'id' => '',
    
            'class' => '',
    
            'show_option_none' => __('Choose an option', 'woocommerce'),
    
        ));
    
        if (false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product) {
    
            $selected_key = 'attribute_' . sanitize_title($args['attribute']);
    
            $args['selected'] = isset($_REQUEST[$selected_key]) ? wc_clean(wp_unslash($_REQUEST[$selected_key])) : $args['product']->get_variation_default_attribute($args['attribute']);
    
        }
    
        $options = $args['options'];
    
        $product = $args['product'];
    
        $attribute = $args['attribute'];
    
        $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute);
    
        $id = $args['id'] ? $args['id'] : sanitize_title($attribute);
    
        $class = $args['class'];
    
        $show_option_none = (bool) $args['show_option_none'];
    
        $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __('Choose an option', 'woocommerce');
    
        if (empty($options) && !empty($product) && !empty($attribute)) {
    
            $attributes = $product->get_variation_attributes();
    
            $options = $attributes[$attribute];
    
        }
    
        $radios = '<div class="variation-radios">';
    
        if (!empty($options)) {
    
            if ($product && taxonomy_exists($attribute)) {
    
                $terms = wc_get_product_terms($product->get_id(), $attribute, array(
    
                    'fields' => 'all',
    
                ));
    
                foreach ($terms as $term) {
    
                    if (in_array($term->slug, $options, true)) {
    
                        $radios .= '<div class="variation-row"><input type="radio" name="' . esc_attr($name) . '" value="' . esc_attr($term->slug) . '" ' . checked(sanitize_title($args['selected']), $term->slug, false) . '><label for="' . esc_attr($term->slug) . '">' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) . '</label></div>';
    
                    }
    
                }
    
            } else {
    
                foreach ($options as $option) {
    
                    $checked = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false);
    
                    $radios .= '<div class="variation-row"><input type="radio" name="' . esc_attr($name) . '" value="' . esc_attr($option) . '" id="' . sanitize_title($option) . '" ' . $checked . '><label for="' . sanitize_title($option) . '">' . apply_filters('woocommerce_variation_option_name', $option) . '</b></label></div>';
    
                }
    
            }
    
        }
    
        $radios .= '</div>';
    
        return $html . $radios;
    
    }
    
    add_filter('woocommerce_dropdown_variation_attribute_options_html', 'variation_radio_buttons', 20, 2);
    
    add_filter('woocommerce_variation_option_name', 'display_price_in_variation_option_name');
    
    function display_price_in_variation_option_name($term)
    
    {
    
        global $wpdb, $product;
    
        if (empty($term)) {
    
            return $term;
    
        }
    
        if (empty($product->id)) {
    
            return $term;
    
        }
    
        $id = $product->get_id();
    
        $result = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'");
    
        $term_slug = (!empty($result)) ? $result[0] : $term;
    
        $query = "SELECT postmeta.post_id AS product_id
    
                    FROM {$wpdb->prefix}postmeta AS postmeta
    
                        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
    
                    WHERE postmeta.meta_key LIKE 'attribute_%'
    
                        AND postmeta.meta_value = '$term_slug'
    
                        AND products.post_parent = $id";
    
        $variation_id = $wpdb->get_col($query);
    
        $parent = wp_get_post_parent_id($variation_id[0]);
    
        if ($parent > 0) {
    
            $_product = new WC_Product_Variation($variation_id[0]);
    
            return $term . ' - ' . wp_kses(woocommerce_price($_product->get_price()), array()) . '';
    
        }
    
        return $term;
    
    }
    
    function my_account_menu_order()
    
    {
    
        $menuOrder = array(
    
            'dashboard' => __('Dashboard', 'woocommerce'),
    
            'orders' => __('Orders', 'woocommerce'),
    
            // 'downloads'          => __( 'Download', 'woocommerce' ),
    
            'edit-address' => __('Addresses', 'woocommerce'),
    
            'edit-account' => __('Account Details', 'woocommerce'),
    
            'customer-logout' => __('Logout', 'woocommerce'),
    
        );
    
        return $menuOrder;
    
    }
    
    add_filter('woocommerce_account_menu_items', 'my_account_menu_order');
    
    function payment_gateway_disable_on_account($available_gateways)
    
    {
    
        global $woocommerce;
    
        // Get current user & details
    
        $user = wp_get_current_user();
    
        $user_id = $user->ID;
    
        $credit_limit = get_user_meta($user_id, 'credit_limit', true);
    
        if ($credit_limit == "" || is_numeric($credit_limit) != true) {
    
            if (isset($available_gateways['cod'])) {
    
                unset($available_gateways['cod']);
    
            }
    
            return $available_gateways;
    
        }
    
        // Get cart subtotal without formatting
    
        $amount = $woocommerce->cart ? floatval(preg_replace('#[^\d.]#', '', $woocommerce->cart->get_cart_subtotal())) : 0;
    
        // Set credit limit to default of 5000 if nothing is set
    
        if (empty($credit_limit)) {
    
            $credit_limit = 5000;
    
        }
    
        // Work out remaining
    
        $credit_remaining = $credit_limit - $amount;
    
        // If ‘cash on delivery — cod’ (what we have named ‘On account’) is active AND
    
        // customer IS NOT ‘customer_with_account’ role OR
    
        // customer IS spending more than 5000 (or custom limit)
    
        // then remove cod
    
        if ($credit_remaining < 0) { //!in_array('customer_with_account', $user->roles)
    
            if (isset($available_gateways['cod'])) {
    
                unset($available_gateways['cod']);
    
            }
    
        }
    
        return $available_gateways;
    
    }
    
    add_filter('woocommerce_available_payment_gateways', 'payment_gateway_disable_on_account');
    
    // Hooks near the bottom of profile page (if current user)
    
    add_action('show_user_profile', 'custom_user_profile_fields');
    
    // Hooks near the bottom of the profile page (if not current user)
    
    add_action('edit_user_profile', 'custom_user_profile_fields');
    
    // @param WP_User $user
    
    function custom_user_profile_fields($user)
    
    {
    
    ?>
    
        <table class="form-table">
    
            <tr>
    
                <th>
    
                    <label for="credit_limit"><?php _e('Credit Limit'); ?></label>
    
                </th>
    
                <td>
    
                    <input type="text" name="credit_limit" id="credit_limit" value="<?php echo esc_attr(get_the_author_meta('credit_limit', $user->ID)); ?>" class="regular-text" />
    
                </td>
    
            </tr>
    
            <tr>
    
                <th>
    
                    <label for="legacy_account_id"><?php _e('Legacy Account ID'); ?></label>
    
                </th>
    
                <td>
    
                    <input type="text" name="legacy_account_id" id="legacy_account_id" value="<?php echo esc_attr(get_the_author_meta('legacy_account_id', $user->ID)); ?>" class="regular-text" />
    
                </td>
    
            </tr>
    
        </table>                                                          
    
    <?php
    
    }
    
    // Hook is used to save custom fields that have been added to the WordPress profile page (if current user)
    
    add_action('personal_options_update', 'update_extra_profile_fields');
    
    // Hook is used to save custom fields that have been added to the WordPress profile page (if not current user)
    
    add_action('edit_user_profile_update', 'update_extra_profile_fields');
    
    function update_extra_profile_fields($user_id)
    
    {
    
        if (current_user_can('edit_user', $user_id)) {
    
            update_user_meta($user_id, 'credit_limit', $_POST['credit_limit']);
    
            update_user_meta($user_id, 'legacy_account_id', $_POST['legacy_account_id']);
    
        }
    
    }
    
    function defer_parsing_of_js($url)
    
    {
    
        if (is_user_logged_in()) {
    
            return $url;
    
        }
    
        //don't break WP Admin
    
        if (false === strpos($url, '.js')) {
    
            return $url;
    
        }
    
        if (strpos($url, 'jquery.js')) {
    
            return $url;
    
        }
    
        return str_replace(' src', ' defer src', $url);
    
    }
    
    add_filter('script_loader_tag', 'defer_parsing_of_js', 10);
    
    // add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
    
    // function handle_custom_query_var( $query, $query_vars ) {
    
    //     if ( isset( $query_vars['like_name'] ) && ! empty( $query_vars['like_name'] ) ) {
    
    //         $query['s'] = esc_attr( $query_vars['like_name'] );
    
    //     }
    
    //     return $query;
    
    // }
    
    // function search_by_sku( $search, &$query_vars ) {
    
    //     global $wpdb;
    
    //     if(isset($query_vars->query['s']) && !empty($query_vars->query['s'])){
    
    //         $args = array(
    
    //             'posts_per_page'  => -1,
    
    //             'post_type'       => 'product',
    
    //             'meta_query' => array(
    
    //                 array(
    
    //                     'key' => '_sku',
    
    //                     'value' => $query_vars->query['s'],
    
    //                     'compare' => 'LIKE'
    
    //                 )
    
    //             )
    
    //         );
    
    //         $posts = get_posts($args);
    
    //         if(empty($posts)) return $search;
    
    //         $get_post_ids = array();
    
    //         foreach($posts as $post){
    
    //             $get_post_ids[] = $post->ID;
    
    //         }
    
    //         if(sizeof( $get_post_ids ) > 0 ) {
    
    //                 $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
    
    //         }
    
    //     }
    
    //     return $search;
    
    // }
    
    // add_filter( 'posts_search', 'search_by_sku', 999, 2 );
    
    /* Add sku to product search */
    
    function az_pre_get_posts($query)
    
    {
    
        // conditions - change the post type clause if you're not searching woocommerce or 'product' post type
    
        if (is_admin() || !$query->is_main_query() || !$query->is_search() || !get_query_var('post_type') == 'product') {
    
            return;
    
        }
    
        add_filter('posts_join', 'az_search_join');
    
        add_filter('posts_where', 'az_search_where');
    
        add_filter('posts_groupby', 'az_search_groupby');
    
    }
    
    add_action('pre_get_posts', 'az_pre_get_posts');
    
    function az_search_join($join)
    
    {
    
        global $wpdb;
    
        $join .= " LEFT JOIN $wpdb->postmeta gm ON (" .
    
            $wpdb->posts . ".ID = gm.post_id AND gm.meta_key='_sku')";
    
        // change to your meta key if not woo
    
        return $join;
    
    }
    
    function az_search_where($where)
    
    {
    
        global $wpdb;
    
        $where = preg_replace(
    
            "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
    
            "({$wpdb->posts}.post_title LIKE $1) OR (gm.meta_value LIKE $1)",
    
            $where
    
        );
    
        return $where;
    
    }
    
    /* grouping by id to make sure no dupes */
    
    function az_search_groupby($groupby)
    
    {
    
        global $wpdb;
    
        $mygroupby = "{$wpdb->posts}.ID";
    
        if (preg_match("/$mygroupby/", $groupby)) {
    
            // grouping we need is already there
    
            return $groupby;
    
        }
    
        if (!strlen(trim($groupby))) {
    
            // groupby was empty, use ours
    
            return $mygroupby;
    
        }
    
        // wasn't empty, append ours
    
        return $groupby . ", " . $mygroupby;
    
    }
    
    /**
    
     * Set a minimum order amount for checkout
    
     */
    
    add_action('woocommerce_check_cart_items', 'required_min_cart_subtotal_amount');
    
    function required_min_cart_subtotal_amount()
    
    {
    
        $minimum_amount = 15; // HERE Set minimum cart total amount
    
        $cart_subtotal  = 0; // Total (before taxes and shipping charges)
    
        // Loop through cart items
    
        foreach (WC()->cart->get_cart() as $cart_item) {
    
            // Check for product tag
    
            $cart_subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
    
        }
    
        // Add an error notice is cart total is less than the minimum required
    
        if ($cart_subtotal < $minimum_amount) {
    
            // Display an error message
    
            wc_add_notice('<strong>' . sprintf(__("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount)) . '<strong>', 'error');
    
        }
    
    }
    
    add_filter('wf_pklist_alter_billing_address', 'wt_pklist_add_user_id', 10, 3);
    
    function wt_pklist_add_user_id($billing_address, $template_type, $order)
    
    {
    
                    $user_id = $order->get_customer_id();
    
                    if(!empty($user_id))
    
                    {
    
                                    $billing_address['user_id']='Customer ID: '.$user_id;
    
                    }
    
                    return $billing_address;
    
    }
    
    ?>

    Hi, the code above is our functions PHP code. This is where our developers originally added the legacy account id meta (customer account id). What is now showing on the invoice is a customer id which means nothing to us (last paragprah of coding). How do we chance this to show the legacy account id please?

    Thread Starter charlotteb22

    (@charlotteb22)

    Hi,
    Sorry, yes the order number is on the invoice. We’d like the customer account number on the invoice. We have these set in the user section under the field ‘Legacy Account ID’. I’m not sure if these were set up via another plugin as I’m told it’s not a standard woocommerce feature.
    We need the order meta key to add this information to the invoice, but cannot find this, and in all honesty, I’m not too sure what this is.
    Does anyone else have customer account numbers on the invoice?
    Thanks

    Thread Starter charlotteb22

    (@charlotteb22)

    Hi,
    Thanks for the reply. The post is about the plugin, but also asking if there are other plugins available that can do this that anyone would recommend, please?
    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)