Reputation: 725
I have a pretty basic problem , I guess people working with woocommerce and wordpress would know how to solve this.
I have a customizable product the customer creates on a page, and then are able to add it to the cart.
The product and the add to cart button works fine so to say.
It add's the product - BUT it clears the cart before adding the newly customized product. Why does it do that ? And how can I avoid it - so it just add's another one to the cart before emptying the current one.
My add to cart button:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
My product creation:
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;
}
}
}
Upvotes: 2
Views: 1980
Reputation: 2359
There is a filter for that
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;
}
Upvotes: 1
Reputation: 725
So here is the solution for this question, Matthew was pointing me in the right direction concerning the cart session but the code that worked for me was this :
So before adding product to cart I had to retrieve the current cart session.
$woocommerce->cart->get_cart_from_session();
$woocommerce->cart->add_to_cart($product_ID);
Upvotes: 0
Reputation: 2305
It seems that you are not the only one to find that the cart needs to be initialized first to get consistent and expected behaviours from it:
I would suggest that you consider getting in something like:
$woocommerce->session->set_customer_session_cookie(true);
To get things going so that you are not making and setting a new cart and over-writing the old one.
Also
I imagine that this could cause you problems down the road:
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
This is the same as putting
$woocommerce->cart->add_to_cart( $product_ID, TRUE );
The reason being is that you are passing to the method the result of $quantity=1
which is almost always going to be a success.
What you mean is:
$woocommerce->cart->add_to_cart( $product_ID, 1 );
However my guess is that you took the method signature which shows that the second parameter is optional and defaults to a value of integer one if left out in which case you could make it even simpler with:
$woocommerce->cart->add_to_cart( $product_ID );
If you only want to pass variables then you need something like this
$quantity = 1;
// ... any other code
$woocommerce->cart->add_to_cart( $product_ID, $quantity );
Upvotes: 1
Reputation: 3093
have you checked the values your passing to $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 )
What about dumping the SQL it generates and attempt to run it, see if it returns any errors?
Upvotes: 0