1
0
mirror of synced 2025-01-18 17:01:41 +03:00

Updated generate icml catalog, bug fix (#8)

This commit is contained in:
Akolzin Dmitry 2017-08-15 13:28:29 +02:00 committed by Alex Lushpai
parent 6d0f765f1c
commit 754c41d49e
6 changed files with 89 additions and 79 deletions

View File

@ -245,7 +245,7 @@
return $this->client->makeRequest(
"/custom-fields/$entity/edit/{$customField['code']}",
$this->client::METHOD_POST,
WC_Retailcrm_Request::METHOD_POST,
array('customField' => json_encode($customField))
);
}

View File

@ -14,8 +14,9 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) :
*/
class WC_Retailcrm_Proxy
{
public function __construct($api_url, $api_key, $api_vers = 'v4')
{
public function __construct($api_url, $api_key, $api_vers)
{
if (!$api_vers) $api_vers = 'v4';
$this->logger = new WC_Logger();
if ( ! class_exists( 'WC_Retailcrm_Client_V3' ) ) {

View File

@ -214,7 +214,7 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) :
'label' => __( 'Выгружать остатки из CRM', 'textdomain' ),
'title' => 'Inventories',
'class' => 'checkbox',
'type' => 'checkbox'б
'type' => 'checkbox',
'description' => 'Отметьте данный пункт, если хотите выгружать остатки товаров из CRM в магазин.'
);

View File

@ -58,7 +58,6 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
public function generate()
{
$categories = $this->get_wc_categories_taxonomies();
$offers = $this->get_wc_products_taxonomies();
if (file_exists($this->tmpFile)) {
if (filectime($this->tmpFile) + $this->fileLifeTime < time()) {
@ -74,12 +73,8 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
$this->writeCategories($categories);
unset($categories);
}
if (!empty($offers)) {
$this->writeOffers($offers);
unset($offers);
}
$this->get_wc_products_taxonomies();
$dom = dom_import_simplexml(simplexml_load_file($this->tmpFile))->ownerDocument;
$dom->formatOutput = true;
$formatted = $dom->saveXML();
@ -317,101 +312,109 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
*/
private function get_wc_products_taxonomies() {
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
$offset = 0;
$limit = 100;
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
if ( version_compare( get_option( 'woocommerce_db_version' ), '3.0', '<' ) ) {
$product = new WC_Product($theid);
$parent = new WC_Product($product->get_parent());
}
else {
if (get_post_type($theid) == 'product') {
$product = wc_get_product($theid);
do {
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => $limit, 'offset' => $offset));
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
if ( version_compare( get_option( 'woocommerce_db_version' ), '3.0', '<' ) ) {
$product = new WC_Product($theid);
$parent = new WC_Product($product->get_parent());
}
elseif (get_post_type($theid) == 'product_variation') {
else {
$post = get_post($theid);
if (get_post($post->post_parent)) {
if (get_post_type($theid) == 'product') {
$product = wc_get_product($theid);
$parent = wc_get_product($product->get_parent_id());
} else {
continue;
$parent = false;
}
elseif (get_post_type($theid) == 'product_variation') {
if (get_post($post->post_parent)) {
$product = wc_get_product($theid);
$parent = wc_get_product($product->get_parent_id());
}
}
}
}
if ($product->get_type() == 'variable') continue;
if ($product->get_type() == 'variable') continue;
if ((isset($parent) && $parent->get_type() == 'variable') || $product->get_type() == 'simple') {
if ($this->get_parent_product($product) > 0) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' );
$term_list = wp_get_post_terms($parent->get_id(), 'product_cat', array('fields' => 'ids'));
$attributes = get_post_meta( $parent->get_id() , '_product_attributes' );
} else {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' );
$term_list = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));
$attributes = get_post_meta( $product->get_id() , '_product_attributes' );
}
if ($product->get_type() == 'simple' || $parent && $parent->get_type() == 'variable') {
if ($this->get_parent_product($product) > 0) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' );
$term_list = wp_get_post_terms($parent->get_id(), 'product_cat', array('fields' => 'ids'));
$attributes = get_post_meta( $parent->get_id() , '_product_attributes' );
} else {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $theid ), 'single-post-thumbnail' );
$term_list = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));
$attributes = get_post_meta( $product->get_id() , '_product_attributes' );
}
$attributes = (isset($attributes[0])) ? $attributes[0] : $attributes;
$attributes = (isset($attributes[0])) ? $attributes[0] : $attributes;
$params = array();
$params = array();
$weight = $product->get_weight();
if (!empty($weight)) {
$params[] = array('code' => 'weight', 'name' => 'Weight', 'value' => $weight);
}
$weight = $product->get_weight();
$attrName = '';
if (!empty($weight)) {
$params[] = array('code' => 'weight', 'name' => 'Weight', 'value' => $weight);
}
if (!empty($attributes)) {
foreach ($attributes as $attribute_name => $attribute) {
$attributeValue = get_post_meta($product->get_id(), 'attribute_'.$attribute_name);
$attributeValue = end($attributeValue);
if ($attribute['is_visible'] == 1 && !empty($attribute['value'])) {
$params[] = array(
'code' => $attribute_name,
'name' => $attribute['name'],
'value' => $attributeValue
);
$attrName = '';
$attrName .= (!empty($attributeValue)) ? ' - ' . $attributeValue : '';
if (!empty($attributes)) {
foreach ($attributes as $attribute_name => $attribute) {
$attributeValue = end(get_post_meta($product->get_id(), 'attribute_'.$attribute_name));
if ($attribute['is_visible'] == 1 && !empty($attribute['value'])) {
$params[] = array(
'code' => $attribute_name,
'name' => $attribute['name'],
'value' => $attributeValue
);
$attrName .= (!empty($attributeValue)) ? ' - ' . $attributeValue : '';
}
}
}
}
if ($product->get_sku() != '') {
$params[] = array('code' => 'sku', 'name' => 'SKU', 'value' => $product->get_sku());
}
if ($product->get_sku() != '') {
$params[] = array('code' => 'sku', 'name' => 'SKU', 'value' => $product->get_sku());
}
$name = ($post->post_title == $product->get_title()) ?
$post->post_title . $attrName :
$post->post_title;
$name = ($post->post_title == $product->get_title()) ?
$post->post_title . $attrName :
$post->post_title;
$product_data = array(
$product_data = array(
'id' => $product->get_id(),
'productId' => ($this->get_parent_product($product) > 0) ? $parent->get_id() : $product->get_id(),
'name' => $name,
'productName' => ($this->get_parent_product($product) > 0) ? $parent->get_title() : $product->get_title(),
'price' => $this->get_price_with_tax($product),
'purchasePrice' => $product->get_regular_price(),
'picture' => $image[0],
'url' => ($this->get_parent_product($product) > 0) ? $parent->get_permalink() : $product->get_permalink(),
'quantity' => is_null($product->get_stock_quantity()) ? 0 : $product->get_stock_quantity(),
'categoryId' => $term_list
);
if (!empty($params)) {
$product_data['params'] = $params;
}
$full_product_list[$theid] = $product_data;
unset($product_data);
}
endwhile;
if (!empty($params)) {
$product_data['params'] = $params;
}
return $full_product_list;
$full_product_list[] = $product_data;
unset($product_data);
}
endwhile;
$this->writeOffers($full_product_list);
unset($full_product_list);
$offset += $limit;
} while ($loop->have_posts());
}
/**

View File

@ -250,6 +250,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
$order_data['externalId'] = $order->id;
$order_data['number'] = $order->get_order_number();
$order_data['createdAt'] = $order->order_date;
$order_data['customerComment'] = $order->get_customer_note();
if ($this->retailcrm_settings['api_version'] == 'v5') {
$discount = $order->data['discount_total'] + $order->data['discount_tax'];
@ -285,15 +286,19 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
$status = $order->get_status();
$order_data['status'] = $this->retailcrm_settings[$status];
$user_data_billing = $order->get_address('billing');
if (!empty($user_data_billing)) {
if (!empty($user_data_billing['phone'])) $order_data['phone'] = $user_data_billing['phone'];
if (!empty($user_data_billing['email'])) $order_data['email'] = $user_data_billing['email'];
}
$user_data = $order->get_address('shipping');
if (!empty($user_data)) {
if (!empty($user_data['first_name'])) $order_data['firstName'] = $user_data['first_name'];
if (!empty($user_data['last_name'])) $order_data['lastName'] = $user_data['last_name'];
if (!empty($user_data['phone'])) $order_data['phone'] = $user_data['phone'];
if (!empty($user_data['email'])) $order_data['email'] = $user_data['email'];
if (!empty($user_data['last_name'])) $order_data['lastName'] = $user_data['last_name'];
if (!empty($user_data['postcode'])) $order_data['delivery']['address']['index'] = $user_data['postcode'];
if (!empty($user_data['city'])) $order_data['delivery']['address']['city'] = $user_data['city'];
if (!empty($user_data['country'])) $order_data['delivery']['address']['countryIso'] = $user_data['country'];

View File

@ -312,6 +312,7 @@ function ajax_upload() {
type: "POST",
url: '<?php echo $ajax_url; ?>?action=do_upload',
success: function (response) {
alert('Заказы и клиенты выгружены');
console.log('AJAX response : ',response);
}
});