У меня есть код ниже, посвященный конкретному продукту 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
Принудительно продавать конкретный продукт WooCommerce ИЛИ его вариацию в отдельном заказе. ⇐ Php
Кемеровские программисты php общаются здесь
1734561268
Anonymous
У меня есть код ниже, посвященный конкретному продукту WooCommerce Force, который будет продаваться в отдельном заказе, и пользователю 7uc1f3r.[b]Я хочу добавить к этому коду два других продукта. Первое изделие простое, два других — вариативные. Эти продукты имеют разные варианты веса, и мне нужно, чтобы только один из вариантов веса запускал сценарий и предотвращал добавление в корзину вместе с другими продуктами. Таким образом, код будет циклически перебирать массив идентификаторов продуктов, как показано ниже:
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[/b] must be bought separately due to our shipping arrangements.', 'woocommerce' ),[b] $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[/b] 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 );
Подробнее здесь: [url]https://stackoverflow.com/questions/79277136/force-specific-woocommerce-product-or-product-variation-to-be-sold-in-separate-o[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия