我正在为WC可变产品开发一个功能。这将是与不同的“等级”的门票,为每一级另一个价格。用户应该选择他想要的门票数量,在创建该表单之后,他可以为每一张票(行)添加名称、地址和票务级别。所有这些数据都应该是发送到购物车并按顺序保存的数据。
编辑2
又是你们好。
我花了更多的时间在这个问题上,并更进一步。
我几乎做了我想做的事:
<代码>H 111>所有这些数据都将发送到订单/电子邮件H 212G 213。
现在我发现了问题(当然,可能还有更多的问题)。但是,我一直在做这件事,就像在同一个浏览器窗口中记录用户一样。之后,我意识到,当我使用inkognito模式,或其他浏览器没有会话或此网站的cookie存储,添加项目后,购物车是空的。
当我第一次尝试添加一些其他项目,即使用常规WC添加到购物车按钮,这是有效的,然后我可以添加我的票,没有问题。
但是当我先加票的时候,购物车是空的。在我的主浏览器中,作为日志用户,我没有问题。
我对此做了一些研究,但我只是在尝试。我真的不明白这个问题,所以我又在浪费时间了。
如果有人能帮助我或引导我找到解决方案,我会很高兴的。
谢谢您抽时间见我。
下面是我插件中的代码:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'UberMike' ) ) {
class UberMike{
private static $farba_vlastnost = 'attribute_pa_level-bezca';
private static $attributes_values = array();
public function __construct() {
self::$farba_vlastnost = self::$farba_vlastnost;
self::$attributes_values = self::$attributes_values;
// called only after woocommerce has finished loading
add_action( 'woocommerce_init', array( &$this, 'woocommerce_loaded' ) );
// called after all plugins have loaded
add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
//enqueue scripts and css style
add_action("wp_loaded", array( &$this, "enqueue_scripts") );
//add custom actions
//remove WC action completely
add_action('woocommerce_single_product_summary', function () {
remove_action('woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
}, -1000);
//add custom add to cart
add_action( 'woocommerce_variable_add_to_cart', array( $this, 'add_to_cart'), 30 );
//create collapsible section input number and button
add_action( 'woocommerce_variable_add_to_cart', array( $this, 'createCollapsibleSection') );
//hide collapsible section in header
add_action('wp_footer', array( $this, 'collapsibleSectionHeader') );
//boot session
// add_action('wp_head', array( $this, 'hook_cookie') );
add_action('wp_head', array( $this, 'boot_session') );
//display meta data to each item in cart anch checkout
add_filter( 'woocommerce_get_item_data', array( $this, 'ubermike_render_meta_on_cart_and_checkout'), 20, 2 );
//display grouped custom fields in checkout and order notes
add_action( 'woocommerce_checkout_create_order', array( $this, 'my_custom_checkout_field') );
add_action( 'woocommerce_after_order_notes', array( $this, 'my_custom_checkout_field') );
//display fields in order and email
add_action('woocommerce_checkout_create_order_line_item', array( $this, 'save_custom_order_item_meta_data'), 10, 4 );
}
/**
* Take care of anything that needs woocommerce to be loaded.
* For instance, if you need access to the $woocommerce global
*/
public function woocommerce_loaded() {
// ...
}
/**
* Take care of anything that needs all plugins to be loaded
*/
public function plugins_loaded() {
// ...
}
//enqueue JS scripts and CSS style
public function enqueue_scripts() {
wp_register_style( 'ubermike_css', plugins_url('assets/ubermike.css', __FILE__) );
wp_enqueue_style('ubermike_css');
wp_register_script( 'ubermike_js', plugins_url('assets/ubermike.js',__FILE__ ), array( 'jquery' ));
wp_enqueue_script('ubermike_js');
}
//add to cart replacement
public function add_to_cart($allsets){
global $product, $post, $woocommerce;
//check product category
$is_ticket = false;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $t){
if ($t->slug=='ticket'){
$is_ticket = true;
break;
}
}
//if the category is other than ticket, basic WC single variable product will be displayed on frontend
if (!method_exists($product,'get_variation_attributes') || !$is_ticket) {
wp_enqueue_script( 'wc-add-to-cart-variation' );
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes()
) );
return;
}
//get product variations
$variations = $product->get_available_variations();
//storing variables of product and each variation of this product
//product ID and quantity (default 1 piece for row)
$custom_product_id = $product->get_id();
$custom_product_quantity = 1;
//storing variations data to variables with index
$index_n = 1;
?>
<div id="hidden-variables-container">
<?php
foreach($variations as $var) {
$product_variation = new WC_Product_Variation($var['variation_id']);
//here i used ->id before
// $somevar = $this->_fillAttributeValues($product, $product_variation->get_id());
$this->_fillAttributeValues($product, $product_variation->id);
//variation attribute name json encode
$attr_text[$index_n] = json_encode($var['attributes']);
//variation name to display in frontend
$variation_name_to_display[$index_n] = self::_getAttributeValue($var['attributes']);
//variation attribute name json encode htmlspecialchars
$variation_name_full[$index_n] = htmlspecialchars($attr_text[$index_n]);
//variation id
$variation_id[$index_n] = $var['variation_id'];
?>
<!-- Variables to frontend for JS use -->
<div class="hidden-variable <?php echo $index_n; ?>">
<span class="um_variation_name_frontend <?php echo $index_n; ?>"><?php echo $variation_name_to_display[$index_n]; ?></span>
<span class="um_variation_attribute_name <?php echo $index_n; ?>"><?php echo $variation_name_full[$index_n]; ?></span>
<span class="um_variation_id <?php echo $index_n; ?>"><?php echo $variation_id[$index_n];?></span>
</div>
<?php
$index_n++;
}
?>
</div>
<?php
}
//get values of attributes
private function _getAttributeValue($get_attributes){
$text = '';
foreach ($get_attributes as $name=>$attribute){
if ($name==self::$farba_vlastnost){
if (isset(self::$attributes_values['pa_level-bezca'][$attribute])) $text = self::$attributes_values['pa_level-bezca'][$attribute];
break;
}
}
if (empty($text)){
$text = [];
foreach ($get_attributes as $name=>$attribute){
if (isset(self::$attributes_values[substr($name, strlen('attribute_'))][$attribute])) $text[] = self::$attributes_values[substr($name, strlen('attribute_'))][$attribute];
}
$text = join(', ', $text);
}
return $text;
}
//fill attribute values to display name in frontend
private function _fillAttributeValues($product, $product_variation_id){
$attributes = $product->get_attributes();
foreach ($attributes as $attribute){
if ($attribute['is_taxonomy']){
$post_terms = wp_get_post_terms( $product_variation_id, $attribute[ 'name' ] );
foreach ( $post_terms as $term ) {
self::$attributes_values[$term->taxonomy][$term->slug] = esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) );
}
}
}
}
function createCollapsibleSection(){
global $product, $post, $woocommerce;
//check if product term is ticket
$is_ticket = false;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $t){
if ($t->slug=='ticket'){
$is_textil = true;
} else{
}
}
?>
<!-- Input type number for JS created fields -->
<div class="quantity">
<label class="tickets-quantity" for="number-of-tickets">Vyberte počet lístkov</label>
<input type="number" id="number-of-tickets" class="" step="1" min="1" max="" name="number-of-tickets" value="1" title="Počet" size="4" inputmode="numeric">
</div>
<!-- This span opens hidden modal with input rows -->
<span class="open-modal"> Vyplniť údaje </span>
<?php
}
function collapsibleSectionHeader(){
global $product, $post, $woocommerce;
?>
<!-- Form with input fields and select for variation -->
<div id="clone-wrapper-bigbranding" class="collapsible-wrapper-bigbranding hidenx">
<div class="uber-modal">
<span class="close-modal">Zatvoriť</span>
<form id="form-motherboard" action="#" method="post">
<input type="hidden" name="product_id" value="<?php $product->id; ?>">
<input type="hidden" name="ubermike" value="1">
<input class="do-kosika" type="submit" name="submit" value="Pridať do košíka" />
<?php
if (isset($_POST['ubermike'])){
$product_id = absint($_POST['product_id']);
// This loops through rows and adds products to cart
$cnt = count($_POST['ticketLevel']);
$quantity_total = 0;
for($i=0;$i<$cnt;$i++){
$cats = explode("|", $_POST['ticketLevel'][$i]);
$selected_val = $cats[0]; // cat_id
$selected_level = $cats[1]; // cat_name
$customer_name = $_POST['name'][$i];
$customer_address = $_POST['address'][$i];
$customer_bio = $_POST['bio'][$i];
$custom_data = array(); // Initializing
$custom_data['custom_data']['name'] = array(
'label' => 'Meno',
'value' => $customer_name
);
$custom_data['custom_data']['address'] = array(
'label' => 'Adresa',
'value' => $customer_address
);
$custom_data['custom_data']['bio'] = array(
'label' => 'Bio',
'value' => $customer_bio
);
$custom_data['custom_data']['level'] = array(
'label' => 'Level',
'value' => $selected_level
);
$quantity = 1;
WC()->cart->add_to_cart( $product_id, $quantity, $selected_val, array(), $custom_data );
do_action( 'woocommerce_set_cart_cookies', TRUE );
$quantity_total += $quantity;
}
if ($quantity_total) wc_add_to_cart_message( array( $product_id => $quantity_total ), true );
}
?>
</form>
</div>
</div>
<?
}
function ubermike_render_meta_on_cart_and_checkout( $cart_data, $cart_item ){
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( isset( $cart_item['custom_data'] ) ) {
foreach( $cart_item['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
$custom_items[] = array(
'name' => $custom_data['label'],
'value' => $custom_data['value'],
);
}
}
}
return $custom_items;
}
function my_custom_checkout_field( $checkout ) {
global $woocommerce;
echo '<div id="my_custom_checkout_field"><h2>' . __('Údaje objednávky') . '</h2>';
$indexing = 1;
foreach ( $woocommerce->cart->get_cart() as $cart_item ) {
if( isset($cart_item['custom_data']) ) {
echo '<div class="uber-ticket-wrapper">';
echo '<div class="uber-ticket">Ticket ' . $indexing . ' </div>';
foreach( $cart_item['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
echo $custom_data['label'];
echo("<br>");
echo $custom_data['value'];
echo("<br>");
}
}
echo "</div>";
$indexing++;
}
}
echo '</div>';
}
// Save cart item custom data as order item meta data and display it everywhere in Orders and email notifications
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if (isset($values['custom_data'])) {
foreach( $values['custom_data'] as $key => $custom_data ){
if( $key != 'key' ){
$item->update_meta_data( $custom_data['label'], $custom_data['value'] );
}
}
}
}
function boot_session() {
session_start();
}
}
//instantiate plugin class and add it to the set of globals
$GLOBALS['ubermike'] = new UberMike();
}
}这里的图片显示单个产品,然后以模态形式形成。

发布于 2020-02-17 19:51:21
用这个答案解决:https://stackoverflow.com/a/53422060/12897376
当未登录时,您需要启动Woocommerce用户会话。因此,您将使用以下内容:
add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){
if( is_user_logged_in() || is_admin() )
return;
if ( ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
}非常感谢。
https://stackoverflow.com/questions/60223745
复制相似问题