Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce将自定义元数据添加到签出页面_Php_Wordpress_Woocommerce - Fatal编程技术网

Php Woocommerce将自定义元数据添加到签出页面

Php Woocommerce将自定义元数据添加到签出页面,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试在签出页面上获取此文本输入字段值。由于某种原因,我无法在结帐页面上打印数据 /* Create Text Field */ add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_data_field' ); function add_custom_data_field() { echo '<table class="variations" cellspacing="0"> <t

我正在尝试在签出页面上获取此文本输入字段值。由于某种原因,我无法在结帐页面上打印数据

/* Create Text Field */
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_data_field' );
function add_custom_data_field() {
  echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
              <td class="label"><label for="color">Custom Data</label></td>
              <td class="value">
                  <input type="text" name="user_data" value="" />
              </td>
          </tr>                             
      </tbody>
  </table>';
}


/* Add Data in a Custom Session, on ‘Add to Cart’ Button Click */
add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');

function wdm_add_user_custom_data_options_callback()
{
      //Custom data - Sent Via AJAX post method
      $product_id = $_POST['id']; //This is product ID
      $user_custom_data_values =  $_POST['user_data']; //This is User  custom value sent via AJAX
      session_start();
      $_SESSION['wdm_user_custom_data'] = $user_custom_data_values;
      die();
}

/* Add Custom Data in WooCommerce Session */
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,2);

if(!function_exists('wdm_add_item_data'))
{
    function wdm_add_item_data($cart_item_data,$product_id)
    {
        /*Here, We are adding item in WooCommerce session with,  wdm_user_custom_data_value name*/
        global $woocommerce;
        session_start();    
        if (isset($_SESSION['wdm_user_custom_data'])) {
            $option = $_SESSION['wdm_user_custom_data'];       
            $new_value = array('wdm_user_custom_data_value' => $option);
        }
        if(empty($option))
            return $cart_item_data;
        else
        {    
            if(empty($cart_item_data))
                return $new_value;
            else
                return array_merge($cart_item_data,$new_value);
        }
        unset($_SESSION['wdm_user_custom_data']); 
        //Unset our custom session variable, as it is no longer needed.
    }
}

/* Extract Custom Data from WooCommerce Session and Insert it into Cart Object */
add_filter('woocommerce_get_cart_item_from_session',     'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
    function wdm_get_cart_items_from_session($item,$values,$key)
    {
        if (array_key_exists( 'wdm_user_custom_data_value', $values ) )
        {
        $item['wdm_user_custom_data_value'] =     $values['wdm_user_custom_data_value'];
        }       
        return $item;
    }
}

/* Display User Custom Data on Cart and Checkout page  */
add_filter('woocommerce_checkout_cart_item_quantity','wdm_add_user_custom_option_from_session_into_cart',1,3);  
add_filter('woocommerce_cart_item_price','wdm_add_user_custom_option_from_session_into_cart',1,3);
if(!function_exists('wdm_add_user_custom_option_from_session_into_cart'))
{
 function   wdm_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
    {
        /*code to add custom data on Cart & checkout Page*/    
        if(count($values['wdm_user_custom_data_value']) > 0)
        {
            $return_string = $product_name . "</a><dl class='variation'>";
            $return_string .= "<table class='wdm_options_table' id='" . $values['product_id'] . "'>";
            $return_string .= "<tr><td>" . $values['wdm_user_custom_data_value'] . "</td></tr>";
            $return_string .= "</table></dl>"; 
            return $return_string;
        }
        else
        {
            return $product_name;
        }
    }
}


/* Add Custom Data as Metadata to the Order Items */
add_action('wc_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
if(!function_exists('wdm_add_values_to_order_item_meta'))
{
  function wdm_add_values_to_order_item_meta($item_id, $values)
  {
        global $woocommerce,$wpdb;
        $user_custom_values = $values['wdm_user_custom_data_value'];
        if(!empty($user_custom_values))
        {
            wc_add_order_item_meta($item_id,'wdm_user_custom_data',$user_custom_values);  
        }
  }
}


/* Remove User Custom Data, if Product is Removed from Cart */
add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1);
if(!function_exists('wdm_remove_user_custom_data_options_from_cart'))
{
    function     wdm_remove_user_custom_data_options_from_cart($cart_item_key)
    {
        global $woocommerce;
        // Get cart
        $cart = $woocommerce->cart->get_cart();
        // For each item in cart, if item is upsell of deleted product, delete it
        foreach( $cart as $key => $values)
        {
        if ( $values['wdm_user_custom_data_value'] == $cart_item_key )
            unset( $woocommerce->cart->cart_contents[ $key ] );
        }
    }
}
/*创建文本字段*/
添加操作(“添加到购物车按钮之前的woocommerce”、“添加自定义数据字段”);
函数添加\自定义\数据\字段(){
回声'
自定义数据
';
}
/*在自定义会话中添加数据,在“添加到购物车”按钮上单击*/
添加操作(“wp\u ajax\u wdm\u添加用户\u自定义数据\u选项”,“wdm\u添加用户\u自定义数据\u选项\u回调”);
添加操作(“wp\u ajax\u nopriv\u wdm\u添加用户\u自定义数据\u选项”,“wdm\u添加用户\u自定义数据\u选项\u回调”);
函数wdm_添加_用户_自定义_数据_选项_回调()
{
//自定义数据-通过AJAX post方法发送
$product\U id=$\U POST['id'];//这是产品id
$user\u custom\u data\u values=$\u POST['user\u data'];//这是通过AJAX发送的用户自定义值
会话_start();
$\u会话['wdm\u用户\u自定义\u数据']=$user\u自定义\u数据\u值;
模具();
}
/*在WooCommerce会话中添加自定义数据*/
添加过滤器('WOOMerce\u add\u cart\u item\u data'、'wdm\u add\u item\u data',1,2);
如果(!function_存在('wdm_add_item_data'))
{
功能wdm_添加_项目_数据($cart_项目_数据,$product_id)
{
/*在这里,我们在WooCommerce会话中添加项目,其中包含wdm\u user\u custom\u data\u value name*/
全球商业;
会话_start();
如果(isset($_会话['wdm_用户_自定义_数据'])){
$option=$\会话['wdm\用户\自定义\数据'];
$new\u value=array('wdm\u user\u custom\u data\u value'=>$选项);
}
if(空($选项))
返回$cart\u item\u数据;
其他的
{    
if(空($cart\u item\u data))
返回$new_值;
其他的
返回数组\u merge($cart\u item\u data,$new\u value);
}
取消设置($_会话['wdm_用户_自定义_数据]);
//取消设置自定义会话变量,因为不再需要它。
}
}
/*从WooCommerce会话提取自定义数据并将其插入购物车对象*/
添加\u过滤器('woocmerce\u get\u cart\u items\u from\u session','wdm\u get\u cart\u items\u from\u session',1,3);
如果(!函数_存在('wdm_获取_购物车_项目_来自_会话'))
{
函数wdm_从会话获取购物车项目($item,$values,$key)
{
如果(数组\键\存在('wdm\用户\自定义\数据\值',$values))
{
$item['wdm_用户\自定义\数据\值']=$values['wdm_用户\自定义\数据\值'];
}       
返回$item;
}
}
/*在购物车和结帐页面上显示用户自定义数据*/
添加过滤器('woocommerce\u checkout\u cart\u item\u quantity','wdm\u添加用户\u自定义\u选项\u从\u会话\u添加到\u cart',1,3);
添加过滤器('woocommerce\u cart\u item\u price','wdm\u添加用户\u自定义\u选项\u从\u会话\u添加到\u cart',1,3);
如果(!函数_存在('wdm_添加_用户_自定义_选项_从_会话_添加到_购物车'))
{
功能wdm_将_用户_自定义_选项_从_会话_添加到_购物车($product_name、$values、$cart_item_key)
{
/*在购物车和结帐页面添加自定义数据的代码*/
如果(计数($values['wdm\u user\u custom\u data\u value'])>0)
{
$return\u string=$product\u name.“;
$return_字符串=“”;
$return_string.=''.$values['wdm_user_custom_data_value'];
$return_字符串=“”;
return$return\u字符串;
}
其他的
{
返回$product\u名称;
}
}
}
/*将自定义数据作为元数据添加到订单项*/
添加动作(“wc添加订单项目元”,“wdm添加值添加到订单项目元”,1,2);
如果(!function_存在('wdm_add_values_to_order_item_meta'))
{
函数wdm_将值添加到订单项元($item_id,$values)
{
全球$woocmerce,$wpdb;
$user_custom_values=$values['wdm_user_custom_data_value'];
如果(!空($user\u custom\u值))
{
wc_添加_订单_项目_元($item_id,'wdm_用户_自定义_数据',$user_自定义_值);
}
}
}
/*如果产品已从购物车中删除,则删除用户自定义数据*/
添加操作(“购物车前的woocommerce\u商品\u数量\u零”,“wdm\u从购物车中删除用户\u自定义\u数据\u选项”,1,1);
如果(!function_存在('wdm_remove_user_custom_data_options_from_cart'))
{
功能wdm\u从购物车中删除用户自定义数据选项($cart\u item\u key)
{
全球商业;
//推车
$cart=$woocommerce->cart->get_cart();
//对于购物车中的每个项目,如果项目是已删除产品的追加销售,则将其删除
foreach($cart as$key=>$value)
{
如果($values['wdm\U user\U custom\U data\U value']==$cart\U item\U key)
取消设置($woocmerce->cart->cart_内容[$key]);
}
}
}
我从这篇文章中更新了我的代码,我发现它非常有用。


当我进行var_转储时,仍然会得到一个未定义的变量

在添加到购物车按钮之前,在variable.php中为变量产品触发操作的位置