public function get_attachment_by_hash($hash_file)
{
if ( !$hash_file ) return 0;
$attachments = get_posts(array(
'numberposts' => 1,
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'hash_file',
'value' => $hash_file
)
)
));
if (!is_array($attachments) or !count($attachments)) return 0;
if ($attachments[0]->ID)
return $attachments[0]->ID;
else
return 0;
//update_post_meta( $attachment_id, 'photo_title', $metadata['image_meta']['title'] );
}
$tmp_file = download_url( $_first_pic, 600 );
if ( is_wp_error( $tmp_file ) ) {
$this->error($tmp_file->get_error_messages());
return '';
} else {
$hash_file = hash_file('md5', $tmp_file);
$this->log("md5 hash file: $hash_file");
$this->log("download image: $_first_pic");
}
$att_id = $this->get_attachment_by_hash($hash_file);
if ($att_id) {
//find attachment duplicate ID: $att_id
} else {
$att_id = media_handle_sideload( array(
'name' => preg_replace('/\?.*/', '', basename($_first_pic)),
'tmp_name' => $tmp_file,
), $this->product_id, $title);
if ( is_wp_error( $att_id ) ) {
$this->error($att_id->get_error_messages());
} else {
update_post_meta($att_id, 'hash_file', $hash_file);
set_post_thumbnail($this->product_id, $att_id);
}
}
add_filter( 'terms_clauses', function ( $pieces, $taxonomies, $args ) {
global $pagenow, $wpdb;
if ( is_admin() && $pagenow == 'edit-tags.php' && $taxonomies[0] == 'program_session' ) {
$pieces['join'] .= ' INNER JOIN ' . $wpdb->termmeta . ' AS tm ON t.term_id = tm.term_id ';
$pieces['where'] .= ' AND tm.meta_key = "program_session_start"';
$pieces['orderby'] = ' ORDER BY tm.meta_value ';
}
return $pieces;
}, 10, 3 );
$_first_product = get_posts( array(
'numberposts' => 1,
'post_type' => 'product',
'category' => $_categories,
'post_status' => 'publish',
//'orderby'=>'menu_order',
'order'=>'ASC',
) );
if ( is_array($_first_product) and count($_first_product) and $_first_product[0]->ID == $_product_id )
$_is_first_product = true;
if (isset($_POST['isOrder']) && $_POST['isOrder'] == 1) {
$address = array(
'first_name' => $_POST['notes']['domain'],
'last_name' => '',
'company' => $_POST['customer']['company'],
'email' => $_POST['customer']['email'],
'phone' => $_POST['customer']['phone'],
'address_1' => $_POST['customer']['address'],
'address_2' => '',
'city' => $_POST['customer']['city'],
'state' => '',
'postcode' => $_POST['customer']['postalcode'],
'country' => 'NL'
);
$order = wc_create_order();
foreach ($_POST['product_order'] as $productId => $productOrdered) :
$order->add_product( get_product( $productId ), 1 );
endforeach;
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();
update_post_meta( $order->id, '_payment_method', 'ideal' );
update_post_meta( $order->id, '_payment_method_title', 'iDeal' );
// Store Order ID in session so it can be re-used after payment failure
WC()->session->order_awaiting_payment = $order->id;
// Process Payment
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'ideal' ]->process_payment( $order->id );
// Redirect to success/confirmation/payment page
if ( $result['result'] == 'success' ) {
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order->id );
wp_redirect( $result['redirect'] );
exit;
}
}
function receipt_page($order_id) {
global $woocommerce;
$this->order = new WC_Order($order_id);
// если заказ не помечен как готовый к оплате, то меняем статус на "Обработка" и перекидываем на страницу с уведомлением!
if (get_post_meta( $this->order->id, 'ready_to_pay', true) != 1) {
$woocommerce->cart->empty_cart();
$this->order->update_status('processing');
wp_redirect( home_url( '/' ) ); // заменить на URL-адрес страницы с текстом: "Ваш заказ поступил в обработку..."
return false;
}
add_action( 'woocommerce_order_status_pending', function ($order_id) {
update_post_meta( $order_id, 'ready_to_pay', 1);
});
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php
$_discounts = get_posts(array('post_type'=>'o-discount'));
if (is_array($_discounts) and is_object($_discounts[0])) {
//print_r(get_post_custom($_discounts[0]->ID));
$_meta = get_post_meta($_discounts[0]->ID, 'o-discount', true);
if ($_meta['percentage-or-fixed-amount'] > 0) {
$_percentage = 100 - $_meta['percentage-or-fixed-amount'];
$_discount_price = $product->price * ($_percentage / 100);
echo '<p class="price woo-discount"><label>Цена при покупке через сайт:</label>' . number_format($_discount_price, 0, '.', ' ') . ' <span class="woocommerce-Price-currencySymbol">₽</span></p>';
}
}
function save_wc_custom_attributes($post_id, $custom_attributes) {
foreach ($custom_attributes as $name => $value) {
$product_attributes[] = array(
'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name
'value' => $value, // set attribute value
'position' => 1,
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => is_array($value) ? 1 : 0,
);
}
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
// пример записи значений типа текст, для товара с ID = $id
$_product_attributes = array();
$_product_attributes['pa_weight'] = '140';
// $_product_attributes['pa_color'] = 'red';
// допустим, нам нужно записать значения разных цветов:
$colors = array('red', 'green', 'yellow');
// записываем:
wp_set_object_terms($id, $colors, 'pa_color', false);
// обновляем такие свойства атрибута как: position, is_visible, is_taxonomy
$_product_attributes['pa_color'] = $colors;
save_wc_custom_attributes($id, $_product_attributes);