0

I had a developer write this plugin for me to add functionality to import bulk pricing to products through WP All Import, a while back. He has not been getting back to me regarding this. I had to delete the auto import that we had set up together and I don't remember how to use it to import the bulk pricing with the system he built. Could someone explain what the code indicate that I would do to use it?

<?php
/*
Plugin Name: WP All Import Woo Bulk Pricing Add-On
Description: Import data related to bulk pricing
Version: 1.0    */
//  

function import_pricing_fields($id, $xml_node) {
    // return;
    $post_type = get_post_type($id);

    if($post_type == 'product' || $post_type == 'product_variation' ){
        $xml_node = (array) $xml_node;
        $_product = wc_get_product( $id );
        $number_of_prices = 3;
        if( $_product->is_type( 'simple' ) ) {
            update_post_meta($id,'_regular_price', $xml_node['price']);
            update_post_meta($id,'_price', $xml_node['price']);
            $pricing_array = array();
            for($i=1;$i<$number_of_prices;$i++){
                if(isset($xml_node['qb_'.$i]) && $xml_node['qb_'.$i] != 0){
                    $pricing_array[$i]['min'] = $xml_node['qb_'.$i];
                    if($i > 1){
                        $pricing_array[$i-1]['max'] = $xml_node['qb_'.$i]-1;
                        $pricing_array[$i]['max'] = "*";
                    } else {
                        $pricing_array[$i]['max'] = "*";
                    }
                    $pricing_array[$i]['val'] = $xml_node['price_'.$i];
                }
            }
            $pricing_array = array_values($pricing_array);


            if(!empty($pricing_array)) {
                update_post_meta($id,'_wc_bulk_pricing_ruleset','_custom');
                update_post_meta($id,'_wc_bulk_pricing_custom_ruleset', $pricing_array);
            }
        } else{
            $var_id = wc_get_product_id_by_sku($xml_node['catalog']);
            $variations = $_product->get_children();

            if(!empty($variations)) {
                foreach ($variations as $variation) {
                    $sku = get_post_meta($variation, '_sku', true);
                    if(!empty($sku) && $sku == $xml_node['catalog']) {
                        $var_id = $variation;
                    }
                }
            }


            update_post_meta($var_id,'_regular_price', $xml_node['price']);
            update_post_meta($var_id,'_price', $xml_node['price']);
            if(isset($xml_node['qb_1'])) {
                $pricing_array = array();
                for($i=1;$i<$number_of_prices;$i++){
                    if(isset($xml_node['qb_'.$i]) && $xml_node['qb_'.$i] != 0){
                        $pricing_array[$i]['min'] = $xml_node['qb_'.$i];
                        if($i > 1){
                            $pricing_array[$i-1]['max'] = $xml_node['qb_'.$i]-1;
                            $pricing_array[$i]['max'] = "*";
                        } else {
                            $pricing_array[$i]['max'] = "*";
                        }
                        $pricing_array[$i]['val'] = $xml_node['price_'.$i];
                    }
                }
                $pricing_array = array_values($pricing_array);


                if(!empty($pricing_array)) {
                    update_post_meta($var_id,'_wc_bulk_pricing_ruleset','_custom');
                    update_post_meta($var_id,'_wc_bulk_pricing_custom_ruleset', $pricing_array);
                }
            }

        }
    }
}
add_action('pmxi_saved_post','import_pricing_fields', 10, 2);

1 Answer 1

1

Could someone explain what the code indicate that I would do to use it?

The function import_pricing_fields() is hooked into the 'pmxi_saved_post' action.

http://www.wpallimport.com/documentation/advanced/action-reference/

What this means is that every time you import a post using WP All Import it will also run the function import_pricing_fields().

However this is one check to pass before the majority of the code in this function will run, and that is on line 4 if($post_type == 'product' || $post_type == 'product_variation' ). Which simply means if the post is a product or product_variation continue running this code.

The rest of the code looks like it does what you said, import bulk pricing...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The programmer that built this didn't explain and has ignored all my emails regarding this and I've been trying to utilize its function but what I had requested was not what he had done.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.