Принудительно продавать конкретный продукт WooCommerce ИЛИ его вариацию в отдельном заказе.Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Принудительно продавать конкретный продукт WooCommerce ИЛИ его вариацию в отдельном заказе.

Сообщение Anonymous »

У меня есть код ниже, посвященный конкретному продукту WooCommerce Force, который будет продаваться в отдельном заказе, и пользователю 7uc1f3r.Я хочу добавить к этому коду два других продукта. Первое изделие простое, два других — вариативные. Эти продукты имеют разные варианты веса, и мне нужно, чтобы только один из вариантов веса запускал сценарий и предотвращал добавление в корзину вместе с другими продуктами. Таким образом, код будет циклически перебирать массив идентификаторов продуктов, как показано ниже:
function filter_woocommerce_add_product_to_cart_array_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {

// Product id to bought alone
$product_id_alone_array = array("1562", "1619");
// Product id of the variation to be bought alone
$variation_id_alone_array = array("1319", "1476");

// Set variable
$flag = false;

// cycle through the array 'product_id_alone_array'
foreach ($product_id_alone_array as $product_id_alone) {
// If cart is NOT empty when a product is added
if ( ! WC()->cart->is_empty() ) {
// Generate a unique ID for the cart item
$product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );

// Check if product is in the cart
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

// If product is already in cart & product ID added is not equal to product ID alone
if ( $in_cart && ( $product_id != $product_id_alone ) ) {
$flag = true;
// Product ID alone is NOT in cart & product ID added is equal to product ID alone
} elseif( ! $in_cart && ( $product_id == $product_id_alone ) ) {
$flag = true;
}
}

global $product;
// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( $product_id );
}
$product_name = $product->get_name();

// True
if ( $flag ) {
// Set error message
wc_add_notice( sprintf(
__( 'Product %s
must be bought separately due to our shipping arrangements.', 'woocommerce' ), $product_name,
), 'error' );

// Passed = false
$passed = false;
}

return $passed;
}

// cycle through the array 'variation_id_alone_array'
foreach ($variation_id_alone_array as $variation_id_alone) {

//$variations = $variation_id_alone->get_available_variations();
//$variations_id = wp_list_pluck( $variations, 'variation_id' );
//print_r('Variations: '.$variations_id, TRUE);

$_product = new WC_Product_Variation( $variation_id_alone );
$variation_data = $_product->get_variation_attributes();
//$variation_detail = woocommerce_get_formatted_variation( $variation_data, true ); // this will give all variation detail in one line
$variation_detail = woocommerce_get_formatted_variation( $variation_data, false); // this will give all variation detail one by one
return $variation_detail; // $variation_detail will return string containing variation detail which can be used to print on website
return $variation_data; // $variation_data will return only the data which can be used to store variation data

// If cart is NOT empty when a variation is added
if ( ! WC()->cart->is_empty() ) {
// Generate a unique ID for the cart item
$variation_cart_id = WC()->cart->generate_cart_id( $variation_id_alone );

// Check if variation is in the cart
$in_cart = WC()->cart->find_product_in_cart( $variation_cart_id );

// If variation is already in cart & variation ID added is not equal to variation ID alone
if ( $in_cart && ( $variation_id != $variation_id_alone ) ) {
$flag = true;
// Variation ID alone is NOT in cart & variation ID added is equal to variation ID alone
} elseif( ! $in_cart && ( $variation_id == $variation_id_alone ) ) {
$flag = true;
}
}

global $product;
// If the WC_product Object is not defined globally
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( $variation_id );
}
$product_name = $product->get_name();

// True
if ( $flag ) {
// Set error message
wc_add_notice( sprintf(
__( 'Product %s
must be bought separately due to our shipping arrangements.', 'woocommerce' ),
$product_name,
), 'error' );

// Passed = false
$passed = false;
}

return $passed;
}
}
add_filter( 'woocommerce_add_to_cart_validation_', 'filter_woocommerce_add_product_to_cart_array_validation', 10, 5 );


Подробнее здесь: https://stackoverflow.com/questions/792 ... separate-o
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»