zoukankan      html  css  js  c++  java
  • 接入物流api案例

    
    <?php 
    //add some shortcode
    add_action('woocommerce_before_shop_loop','add_code',10);
    function add_code(){
    	echo do_shortcode("[woof]"); 
    	//echo do_shortcode('[woof_price_filter type =“text”] ');
    }
    
    
    //add a custom area to checkout page 
    
    add_action('woocommerce_after_order_notes', 'customise_checkout_field');
    
    function customise_checkout_field($checkout)
    {
    	echo '<div id="customise_checkout_field_shipping"><h2>' . __('Shipping Method') . '</h2>';
    	woocommerce_form_field('customised_field_name', array(
    		'type' => 'select',
    	    'options'     => array(
            	'HKDHL' => __('HKDHL', 'woocommerce' ),
            	'EMS' => __('EMS', 'woocommerce' )
            ),
    		'class' => array(
    			'my-field-class form-row-wide'
    		) ,
    		'label' => __('Customise Additional Field') ,
    		'placeholder' => __('Guidence') ,
    		'required' => true,
    	) , $checkout->get_value('customised_field_name'));
    	echo '</div>';
    }
    
    
    // add weight
    add_action('woocommerce_before_checkout_form', 'bbloomer_print_cart_weight');
     
    function bbloomer_print_cart_weight( $posted ) {
    global $woocommerce;
    $notice = 'Your cart weight is: <span id="myWeight">' . $woocommerce->cart->cart_contents_weight . get_option('woocommerce_weight_unit').'</span>';
    if( is_checkout() ) {
        wc_print_notice( $notice, 'notice' );
    } else {
        wc_add_notice( $notice, 'notice' );
    }
    }
    
    
    
    //add some code to checkout 
    add_action('woocommerce_after_order_notes','myshipping');
    function myshipping(){
    	
    	echo "
    	<script>
    		var _myContry,_myWeight,_my_channelId,strMy,priceMy;
      var _myContry_select = document.querySelector('#billing_country');
      _myContry_select.onchange=function(event){
        _myContry = event.target.value;
    	_myWeight = document.getElementById('myWeight').innerText;
    	  _myWeight=_myWeight.substring(0,_myWeight.length-2);
    	  console.log(_myWeight);
        console.log(_myContry);
       jQuery('#billing_email').focus(function(){
       		jQuery.ajax({
          url:'https://www.billiardsupermarket.com/wp-content/uploads/getMyshipping.php',
          method:'GET',
          data:{'_myContry':_myContry,'_myWeight':_myWeight},
          success:function(data){
    	  strMy= JSON.parse(data);//将json字符串格式转为js对象
          	console.log(strMy);
            jQuery('#customise_checkout_field_shipping select').change(function(event){
    				_my_channelId = event.target.value;
    				console.log(_my_channelId);
    				for(var i=0;i<strMy[0].length;i++){
    					if(_my_channelId==strMy[0][i][0]){
    					priceMy = strMy[1][i];
    
    					}
    				}
    				
    				//document.cookie = 'priceMy='+priceMy+'; expires=Thu, 18 Dec 2019 12:00:00 UTC';
            });
          },
          error:function(data){
            console.log('data is error!please check.');
          }
          });
       });
      }
    
    
    </script>";
    
    
    }
    //add some fee to checkout page
    add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
    function endo_handling_fee() {
         global $woocommerce;
     
         if ( is_admin() && ! defined( 'DOING_AJAX' ) )
              return;
     
         $fee= $_COOKIE['priceMy'];
    		$feeNum=(int)$fee;
         $woocommerce->cart->add_fee( 'Shipping', $feeNum, true, 'standard' );
    }
    
    
    
    	$data = json_decode($_POST['_myContry']);
    	print_r($data);
    	if($data){
    			add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
    	function endo_handling_fee() {
         global $woocommerce;
     
         if ( is_admin() && ! defined( 'DOING_AJAX' ) )
              return;
     		
           $fee= $data;
    		$feeNum=(int)$fee;
         $woocommerce->cart->add_fee( 'Shipping', $feeNum, true, 'standard' );
          }
    	}
    
    
    
     ?>
    
    <?php 
    $xml1 = '<?xml version="1.0" encoding="utf-8"?>
    <GePriceServiceRequest>
    <authtoken>哈哈哈哈哈哈</authtoken>
    <clientid>哈哈哈哈哈哈</clientid>
    <country>';
    $xml2 = '</country><rweight>';
    $xml3 = '</rweight></GePriceServiceRequest>';
    $xml = $xml1.$_GET["_myContry"].$xml2.$_GET["_myWeight"].$xml3;
    $client = new SoapClient(null, array(
      'location' => 'http://oa.myex.cc/services/APIPrice?Wsdl',
      'uri' => 'http://oa.myex.cc/services/APIPrice?Wsdl',
    ) );
    $result = $client->getPrice($xml);
    // print_r($result);
    $result_str =str_replace("<br>","",$result);//字符创替换
    $result_str =str_replace("&","",$result_str);
    // print_r($result_str);
    $xmlarr = simplexml_load_string($result_str)->Price;//将xml字符串格式转为对象格式
    // print_r($xmlarr);
    
    $shipping_channelid=[];
    $shipping_amt=[];
    $shipping_all=[];
    foreach($xmlarr as $key => $value) {
    	 array_push($shipping_channelid,$value->channelid);//加入到数组
    	 array_push($shipping_amt,ceil($value->amt/7));
    }
    
    // print_r($shipping_channelid);
    // print_r($shipping_amt);
    array_push($shipping_all, $shipping_channelid,$shipping_amt);
    // print_r($shipping_all);
    echo json_encode($shipping_all);//将数组解析为json字符串格式
     ?>
    

    要点:$xmlarr = simplexml_load_string($result_str)这个可以将xml字符串格式转为对象形式

    js变量传给php,可以通过ajax发送参数的形式发给后台接收;
    也可以在js中进行cookie的存储,在php文件中调用$_COOKIE全局变量进行获取

    php 变量给js ```
    var a =

  • 相关阅读:
    模拟按键'ESC',解决韩语等输入法对输入框(codemirror)的支持
    grpc的基础知识
    HttpClientFactory 是 HttpClient 的正确使用方式
    Workflow Core + asp.net core 5.0 实现简单审批工作流
    GitHub自动化部署(CD) asp.net core 5.0 项目(免费空间)
    CleanArchitecture Application代码生成插件-让程序员告别CURD Ctrl+C Ctrl+V
    C# 字符串转成JSON对象 反射获取属性值
    java设计模式-状态模式
    2021目前可用的百度网盘不限速下载方法
    docker映射配置文件
  • 原文地址:https://www.cnblogs.com/cyany/p/8583399.html
Copyright © 2011-2022 走看看