o to mi chodziło aczkolwiek nie aktualizuje mi zdjęcia. Mój kod:
add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
function add_product_to_cart_checkout()
{
global $order, $woocommerce, $post; $product_id = intval($_POST['productid']); $ebike_ids = array(17386
, 17385
, 17382
, 17378
, 17375
, 17372
, 17370
, 17369
, 17364
, 16132
, 16130
, 4561
, 4550
, 3490
, 2376
); $insurance_ids = array(16600
, 16653
, 16654
, 16655
, 16659
, 16660
); $found = false;
$ebike_quantity = 1;
$new_image = intval($_POST['image']);
if (isset($_POST['productid']) && isset($_POST['price']) && !WC
()->cart->is_empty()) { // Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// Delete all preexisting insurance products
if (in_array($cart_item['product_id'], $insurance_ids)) { WC()->cart->remove_cart_item($cart_item_key);
}
// Get ebikes total quantity
if (in_array($cart_item['product_id'], $ebike_ids)) { $ebike_quantity += $cart_item['quantity'];
}
}
// If command is not to remove add insurance product with price as custom cart item data
if ($price >= 0 && $cart_item['insurance_price'] > 0) {
WC
()->cart->add_to_cart($product_id, $ebike_quantity, 0
, array(), array('insurance_price' => $price, "new_photo" => $new_image)); //$order = wc_get_order( $order->id );
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$items = $woocommerce->cart->get_cart();
//$order = new WC_Order($product_id);
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
}
//print_r($items);
echo "dodano do koszyka>> ".$product_cart_id; }
} else {
WC
()->cart->add_to_cart($product_id, $ebike_quantity, 0
, array(), array('insurance_price' => $price, "new_photo" => $new_image)); $product_cart_id = WC()->cart->generate_cart_id( $product_id );
}
if (!WC()->cart->is_empty()) {
echo "dodano do koszyka ".$product_cart_id; } else {
}
}
// Set insurance price from custom cart item data
add_action('woocommerce_before_calculate_totals', 'set_insurance_price');
function set_insurance_price($cart)
{
if (is_admin
() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item) {
if (isset($cart_item['insurance_price']) && $cart_item['insurance_price'] > 0
) { $cart_item['data']->set_price($cart_item['insurance_price']);
}
}
}
add_action( 'woocommerce_get_cart_item_from_session', 'cart_item_from_session' , 99, 2 );
function cart_item_from_session( $data, $values ) {
$data[ 'new_photo' ] = isset( $values[ 'new_photo' ] ) ?
$values[ 'new_photo' ] : ''; return $data;
}
add_filter( 'woocommerce_add_order_item_meta', 'add_item_meta_order', 10, 3 );
function add_item_meta_order( $item_id, $values ) {
if ( !is_null( $values[ 'new_photo' ] ) ) { wc_add_order_item_meta( $item_id, 'new_photo', $values[ 'new_photo' ] );
}
}