Woocommerce 在添加新产品之前删除购物车
Woocommerce deletes cart before adding new product
我有一个非常基本的问题,我想使用 woocommerce 和 wordpress 的人会知道如何解决这个问题。
我有一个客户在页面上创建的可定制产品,然后可以将其添加到购物车。
产品和添加到购物车按钮可以说效果很好。
它添加了产品 - 但它在添加新定制的产品之前清空了购物车。为什么这样做?我怎样才能避免它 - 所以它只是在清空当前的之前将另一个添加到购物车。
我的添加到购物车按钮:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
我的产品创作:
function customcart() {
if (isset($_POST["addcustomcarts"])) {
global $woocommerce;
$my_post = array(
'post_title' => 'Design selv skilt',
'post_content' => '<div class="col-md-12">Dette er et design selv skilt, tjek egenskaber på produktet for at se hvad kunden har bestilt.</div>',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'product'
);
// Insert the post into the database
$product_ID = wp_insert_post( $my_post );
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img' . $product_ID . '.png', $unencodedData);
if ( $product_ID ){
wp_set_object_terms( $product_ID, 'design-selv-skilte', 'product_cat' );
add_post_meta($product_ID, '_regular_price', $_POST["priceInput"] );
add_post_meta($product_ID, '_price', $_POST["priceInput"] );
add_post_meta($product_ID, '_stock_status', 'instock' );
//add_post_meta($product_ID, '_manage_stock', 'yes' );
//add_post_meta($product_ID, '_stock', '10' );
add_post_meta($product_ID, '_sku', 'designselvskilt-' . $product_ID );
add_post_meta($product_ID, '_visibility', 'hidden' );
add_post_meta($product_ID, 'tekst-paa-linje-1', $_POST["textInput"] );
add_post_meta($product_ID, 'tekst-paa-linje-2', $_POST["text2Input"] );
add_post_meta($product_ID, 'stoerrelse', $_POST["størrelseInput"] );
add_post_meta($product_ID, 'form', $_POST["formInput"] );
add_post_meta($product_ID, 'farve', $_POST["farveInput"] );
add_post_meta($product_ID, 'type-skilt', $_POST["typeInput"] );
add_post_meta($product_ID, 'fastgoering', $_POST["fastgøringInput"] );
add_post_meta($product_ID, 'font', $_POST["fontInput"] );
add_post_meta($product_ID, 'linje-1-font-size', $_POST["fontSizeLine1Input"] );
add_post_meta($product_ID, 'linje-2-font-size', $_POST["fontSizeLine2Input"] );
add_post_meta($product_ID, 'product_image_gallery', $_POST["img_val"]);
add_post_meta($product_ID, 'product_image_url', 'img' . $product_ID . '.png');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$home = home_url();
$url = $home . '/img' . $product_ID . '.png';
$post_id = $product_ID;
$desc = $_POST["textInput"];
$image = media_sideload_image($url, $post_id, $desc, src );
function getImageId( $image ) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $image );
// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $image, PHP_URL_HOST ) );
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
return;
}
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
// Example: /uploads/2013/05/test-image.jpg
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
// Returns null if no attachment is found
return $attachment[0];
}
set_post_thumbnail($post_id, getImageId( $image ));
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
wp_redirect( '/kurv' ); exit;
}
}
}
你检查过传递给 $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
的值了吗
如何转储它生成的 SQL 并尝试 运行 它,看看它是否 returns 有任何错误?
看来您并不是唯一发现购物车需要先初始化才能从中获得一致和预期行为的人:
- How to properly initialize Woocommerce Cart
我建议您考虑加入以下内容:
$woocommerce->session->set_customer_session_cookie(true);
让事情顺利进行,这样您就不会制作和设置新购物车并覆盖旧购物车。
还有
我想这可能会给您带来麻烦:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
这和放
是一样的
$woocommerce->cart->add_to_cart( $product_ID, TRUE );
原因是您将 $quantity=1
的结果传递给方法,这几乎总是会成功。
你的意思是:
$woocommerce->cart->add_to_cart( $product_ID, 1 );
但是我的猜测是你采用了方法签名,它表明第二个参数是可选的,如果省略则默认为整数 1 的值,在这种情况下你可以使用以下方法使其更简单:
$woocommerce->cart->add_to_cart( $product_ID );
如果你只想传递变量那么你需要这样的东西
$quantity = 1;
// ... any other code
$woocommerce->cart->add_to_cart( $product_ID, $quantity );
所以这是这个问题的解决方案,Matthew 为我指出了关于购物车会话的正确方向,但对我有用的代码是这样的:
所以在将产品添加到购物车之前,我必须检索当前的购物车会话。
$woocommerce->cart->get_cart_from_session();
$woocommerce->cart->add_to_cart($product_ID);
有一个过滤器
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
我有一个非常基本的问题,我想使用 woocommerce 和 wordpress 的人会知道如何解决这个问题。
我有一个客户在页面上创建的可定制产品,然后可以将其添加到购物车。
产品和添加到购物车按钮可以说效果很好。
它添加了产品 - 但它在添加新定制的产品之前清空了购物车。为什么这样做?我怎样才能避免它 - 所以它只是在清空当前的之前将另一个添加到购物车。
我的添加到购物车按钮:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
我的产品创作:
function customcart() {
if (isset($_POST["addcustomcarts"])) {
global $woocommerce;
$my_post = array(
'post_title' => 'Design selv skilt',
'post_content' => '<div class="col-md-12">Dette er et design selv skilt, tjek egenskaber på produktet for at se hvad kunden har bestilt.</div>',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'product'
);
// Insert the post into the database
$product_ID = wp_insert_post( $my_post );
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img' . $product_ID . '.png', $unencodedData);
if ( $product_ID ){
wp_set_object_terms( $product_ID, 'design-selv-skilte', 'product_cat' );
add_post_meta($product_ID, '_regular_price', $_POST["priceInput"] );
add_post_meta($product_ID, '_price', $_POST["priceInput"] );
add_post_meta($product_ID, '_stock_status', 'instock' );
//add_post_meta($product_ID, '_manage_stock', 'yes' );
//add_post_meta($product_ID, '_stock', '10' );
add_post_meta($product_ID, '_sku', 'designselvskilt-' . $product_ID );
add_post_meta($product_ID, '_visibility', 'hidden' );
add_post_meta($product_ID, 'tekst-paa-linje-1', $_POST["textInput"] );
add_post_meta($product_ID, 'tekst-paa-linje-2', $_POST["text2Input"] );
add_post_meta($product_ID, 'stoerrelse', $_POST["størrelseInput"] );
add_post_meta($product_ID, 'form', $_POST["formInput"] );
add_post_meta($product_ID, 'farve', $_POST["farveInput"] );
add_post_meta($product_ID, 'type-skilt', $_POST["typeInput"] );
add_post_meta($product_ID, 'fastgoering', $_POST["fastgøringInput"] );
add_post_meta($product_ID, 'font', $_POST["fontInput"] );
add_post_meta($product_ID, 'linje-1-font-size', $_POST["fontSizeLine1Input"] );
add_post_meta($product_ID, 'linje-2-font-size', $_POST["fontSizeLine2Input"] );
add_post_meta($product_ID, 'product_image_gallery', $_POST["img_val"]);
add_post_meta($product_ID, 'product_image_url', 'img' . $product_ID . '.png');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$home = home_url();
$url = $home . '/img' . $product_ID . '.png';
$post_id = $product_ID;
$desc = $_POST["textInput"];
$image = media_sideload_image($url, $post_id, $desc, src );
function getImageId( $image ) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $image );
// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $image, PHP_URL_HOST ) );
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
return;
}
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
// Example: /uploads/2013/05/test-image.jpg
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
// Returns null if no attachment is found
return $attachment[0];
}
set_post_thumbnail($post_id, getImageId( $image ));
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
wp_redirect( '/kurv' ); exit;
}
}
}
你检查过传递给 $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
如何转储它生成的 SQL 并尝试 运行 它,看看它是否 returns 有任何错误?
看来您并不是唯一发现购物车需要先初始化才能从中获得一致和预期行为的人:
- How to properly initialize Woocommerce Cart
我建议您考虑加入以下内容:
$woocommerce->session->set_customer_session_cookie(true);
让事情顺利进行,这样您就不会制作和设置新购物车并覆盖旧购物车。
还有
我想这可能会给您带来麻烦:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
这和放
是一样的$woocommerce->cart->add_to_cart( $product_ID, TRUE );
原因是您将 $quantity=1
的结果传递给方法,这几乎总是会成功。
你的意思是:
$woocommerce->cart->add_to_cart( $product_ID, 1 );
但是我的猜测是你采用了方法签名,它表明第二个参数是可选的,如果省略则默认为整数 1 的值,在这种情况下你可以使用以下方法使其更简单:
$woocommerce->cart->add_to_cart( $product_ID );
如果你只想传递变量那么你需要这样的东西
$quantity = 1;
// ... any other code
$woocommerce->cart->add_to_cart( $product_ID, $quantity );
所以这是这个问题的解决方案,Matthew 为我指出了关于购物车会话的正确方向,但对我有用的代码是这样的:
所以在将产品添加到购物车之前,我必须检索当前的购物车会话。
$woocommerce->cart->get_cart_from_session();
$woocommerce->cart->add_to_cart($product_ID);
有一个过滤器
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}