• Resolved esia168

    (@esia168)


    Hi, I know that this plugin does not support dynamic shipping price per product. So I want to add this myself programmatically by customizing your code for my own use. I have been using the CSV file method because I have to add the shipping for each product for all countries manually in the CSV file everytime there’s a new product generated.

    I want to have a column “shipping” that will get filled up with shipping info such as this

    US::USPS Parcel Select:USD2.91,CA::Standard ePacket:USD2.97,AU::Standard ePacket:USD3.96,GB::Standard ePacket:USD3.34,CH::SF Air Mail:USD2.77,SE::Standard ePacket:USD3.86,DK::Standard ePacket:USD3.59,FR::Standard ePacket:USD2.97,DE::Standard ePacket:USD2.97

    I can dig through your code but it’s faster if you tell me on which file and which line of the code I can add this ? I have a php function that I can call by providing it product ID and/or variation ID then it will give me the shipping info.

    It’s no longer possible for me to add shipping manually, the products are too many now.

    I am a PHP programmer too,btw.

    If you can guide me ? Thanks

    • This topic was modified 5 years, 10 months ago by esia168.
    • This topic was modified 5 years, 10 months ago by esia168.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi esia168,

    The shipping feature is, by far, the most complex piece of code of our plugin and cannot be captured into a single line of code but exists out of multiple funtions.

    The code can be found in the /woo-product-feed-pro/classes/class-get-products.php file. The main function is woosea_get_shipping_cost();

    Best,
    Eva

    Thread Starter esia168

    (@esia168)

    Btw, you could checkout this shipping calculator plugin that display shipping info on product page https://codecanyon.net/item/woocommerce-shipping-calculator-on-product-page/11496815 , how they implement is quite simple but smart, basically what they did is add an item into the Woocommmerce (WC) cart programmatically, then use the existing WC code that is used in the checkout page to get the shipping information. So there’s no need to reinvent the wheel/functions on getting the shipping information. I have improvise their code to generate the string of shipping for multiple countries on a product. Wonder if you can follow some inspiration/solution from them. Here is roughly how

            public function googleShippingFeedStr(){
                $originalSetting = $_POST['calc_shipping_country'];
                $countries = ['US','CA', 'AU', 'GB', 'CH', 'SE', 'DK', 'FR', 'DE', 'AT', 'BE', 'IE', 'IL', 'IT', 'JP',
                    'NL', 'NZ', 'NO', 'PL', 'PT', 'ZA', 'ES', 'TR', 'SG', 'MY', 'ID', 'AE', 'SA', 'KR', 'TH', 'VN', 'MX'];
                $shipCountryFeed = [];
                foreach($countries as $country){
                    $_POST['calc_shipping_country'] = $country;
    
                    WC_Shortcode_Cart::calculate_shipping();
    
                    if (isset($_POST["product_id"]) && $this->check_product_incart($_POST["product_id"]) === false) {
                        $qty = (isset($_POST['current_qty']) && $_POST['current_qty'] > 0) ? $_POST['current_qty'] : 1;
                        if (isset($_POST['variation_id']) && $_POST['variation_id'] != "" && $_POST['variation_id'] > 0) {
                            $cart_item_key = WC()->cart->add_to_cart($_POST["product_id"], $qty, $_POST['variation_id']);
                        } else {
                            $cart_item_key = WC()->cart->add_to_cart($_POST["product_id"], $qty);
                        }
    
                        $packages = WC()->cart->get_shipping_packages();
                        $packages = WC()->shipping->calculate_shipping($packages);
                        $available_methods = WC()->shipping->get_packages();
                        WC()->cart->remove_cart_item($cart_item_key);
                    } else {
                        $packages = WC()->cart->get_shipping_packages();
                        $packages = WC()->shipping->calculate_shipping($packages);
                        $available_methods = WC()->shipping->get_packages();
                    }
    
                    wc_clear_notices();
                    if ($this->get_setting('shipping_type') == 2) {
                        if (isset($available_methods[0]["rates"]) && count($available_methods[0]["rates"]) > 0) {
                            $count = 0;
                            //echo '<ul class="shipping_with_price">';
                            foreach ($available_methods[0]["rates"] as $key => $method) {
                                $finalCost=$method->cost;
                                if(isset($method->taxes) && !empty($method->taxes)){
                                    foreach($method->taxes as $tax){
                                        $finalCost=$finalCost+$tax;
                                    }
                                }
                                //echo "<li>";
                                //echo wp_kses_post($method->label) . "&nbsp;<strong>(" . wc_price($finalCost) . ")</strong>";
                                //echo "</li>";
                                $pos = false;
                                $shipName = $method->label;
                                if( ($pos = stripos($shipName, '['))  !== FALSE )
                                    $shipName = trim(substr($method->label, 0, $pos ));
    
                                $shipCountryFeed[] = $country.'::'.$shipName.':USD'.$finalCost;
                                break; //only the first default most important shipping
                            }
                            //echo '</ul>';
                        }
                    }
                }
    
                //reset
                $_POST['calc_shipping_country'] = $originalSetting;
                WC_Shortcode_Cart::calculate_shipping();
    
                return join(',', $shipCountryFeed);
    
    }
    • This reply was modified 5 years, 10 months ago by esia168.
    • This reply was modified 5 years, 10 months ago by esia168.
    • This reply was modified 5 years, 10 months ago by esia168.

    Thanks for the piece of code. Whenever we feel we need to re-do the shipping functionality as it got too complex we will look into this approach.

    Best,
    Eva

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Programming guide on adding per product shipping price’ is closed to new replies.