Код работал нормально, но показывался на всех продуктах, и мне нужно, чтобы загрузка файлов была доступна только для определенных продуктов, после добавления этого к
// Показать дополнительные поля продукта "
global $product;
$defined_products_ids = array( 2130, 2138, 2314, );
if( in_array($product->get_id(), $defined_products_ids) ) {
isset($_POST['upload_active']) && $_POST['upload_active'] && isset($_FILES['custom_file']) ) {
и этот код
// @ ===> Управление загрузкой файлов <=== @ </p>
// Добавить данные пользовательских полей в качестве пользовательских данных элемента корзины
$defined_products_ids = array( 2130, 2138, 2314, );
if( in_array($product->get_id(), $defined_products_ids) ) {
isset($_POST['upload_active']) && $_POST['upload_active'] && isset($_FILES['custom_file']) ) {
Перестал работать, вот мой код functions.php
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
// Display additional product fields
add_action( 'woocommerce_before_add_to_cart_button', 'display_product_add_on_custom_fields', 9 );
function display_product_add_on_custom_fields(){
global $product;
$defined_products_ids = array( 2130, 2138, 2314, );
if( in_array($product->get_id(), $defined_products_ids) ) {
isset($_POST['upload_active']) && $_POST['upload_active'] && isset($_FILES['custom_file']) ) {
// Adding some space between dropdowns and add to cart button for variable products
$style = $product->is_type('variable') ? ' style="padding-bottom:12px;"' : '';
?>
<div class="product-add-on"' . $style . '>
</div>
<p><label for="file_field"><?php echo __("Upload image") . ': '; ?>
'<abbr class="required" title="Required">*</abbr>
<input type="hidden" name="upload_active" value='custom_file'>
</p><input type='file' name='custom_file' accept='image/*' value='custom_file'></label>'
</p>
<?php
}
// @ ===> Manage the file upload <=== @
// Add custom fields data as the cart item custom data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_data_as_custom_cart_item_data', 10, 2 );
function add_custom_fields_data_as_custom_cart_item_data( $cart_item, $product_id ){
$defined_products_ids = array( 2130, 2138, 2314, );
if( in_array($product->get_id(), $defined_products_ids) ) {
isset($_POST['upload_active']) && $_POST['upload_active'] && isset($_FILES['custom_file']) ) {
$upload = wp_upload_bits( $_FILES['custom_file']['name'], null, file_get_contents( $_FILES['custom_file']['tmp_name'] ) );
$filetype = wp_check_filetype( basename( $upload['file'] ), null );
$upload_dir = wp_upload_dir();
$upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];
$base_name = basename( $upload['file'] );
$cart_item['custom_file'] = array(
'guid' => $upl_base_url .'/'. _wp_relative_upload_path( $upload['file'] ),
'file_type' => $filetype['type'],
'file_name' => $base_name,
'title' => preg_replace('/\.[^.]+$/', '', $base_name ),
'key' => md5( microtime().rand() ),
);
}
return $cart_item;
}
// Product add on custom fields validation
add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_custom_fields_validation', 10, 3 );
function product_add_on_custom_fields_validation( $passed, $product_id, $quantity ){
$defined_products_ids = array( 2130, 2138, 2314, );
if( isset($_POST['upload_active']) && $_POST['upload_active'] && isset($_FILES['custom_file']) )
&& in_array($product_id, $defined_products_ids) ) {
wc_add_notice( '"Custom Image" is a required field', 'error' );
$passed = false;
}
return $passed;
}
// Display custom cart item data in cart
add_filter( 'woocommerce_get_item_data', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item, $product_id ){
if ( isset( $cart_item['custom_file']['title'] ) ){
$cart_item_data[] = array(
'name' => __( 'Image', 'woocommerce' ),
'value' => $cart_item['custom_file']['title']
);
}
return $cart_item_data;
}
// Save and display custom item data everywhere on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_product_custom_field_as_order_item_meta', 10, 4 );
function add_product_custom_field_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( isset($values['custom_file']) ) {
$item->update_meta_data('_logo_file_data', $values['custom_file'] );
}
}
// Display a linked button + the link of the Image file in backend
add_action( 'woocommerce_after_order_itemmeta', 'backend_logo_link_after_order_itemmeta', 20, 3 );
function backend_logo_link_after_order_itemmeta( $item_id, $item, $product, $values, $order ) {
// Only in backend for order line items (avoiding errors)
if( is_admin() && $item->is_type('line_item') && $item->get_meta('_logo_file_data') ){
$file_data = $item->get_meta( '_logo_file_data' );
echo '<p><a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Open Image") . '</a></p>';
echo '<p>Link: <textarea type="text" class="input-text" readonly>'.$file_data['guid'].'</textarea></p>';
}
}
?>
Если кто-то может помочь мне с этим кодом и объяснить мне, что мне здесь не хватает, предположив, что он не указывает на правильный $ _Post, попытался изменить их на custom_file вместо upload_active, тогда он не показывает изображение в корзине, на кассе или в конце