function wplister_prepare_listing($post_id, $profile_id) { // prepare product $listingsModel = new ListingsModel(); $listingsModel->prepareProductForListing($post_id); if (!$profile_id) { return; } // get profile $profilesModel = new ProfilesModel(); $profile = $profilesModel->getItem($profile_id); if (!$profile) { return; } $listingsModel->applyProfileToNewListings($profile); }
public function ajax_wple_select_profile() { // TODO: check nonce if (isset($_REQUEST['profile_id']) && isset($_REQUEST['product_ids'])) { $profile_id = $_REQUEST['profile_id']; $product_ids = $_REQUEST['product_ids']; $select_mode = $_REQUEST['select_mode']; $default_account_id = get_option('wplister_default_account_id', 1); $lm = new ListingsModel(); if ('products' == $select_mode) { // get profile $pm = new ProfilesModel(); $profile = $pm->getItem($profile_id); // prepare new listings from products // $response = $lm->prepareListings( $product_ids, $profile_id ); $response = $lm->prepareListings($product_ids, $profile_id); $lm->applyProfileToNewListings($profile); } elseif ('listings' == $select_mode) { // change profile for existing listings // $profile = WPLE_AmazonProfile::getProfile( $profile_id ); // doesn't work $pm = new ProfilesModel(); $profile = $pm->getItem($profile_id); // $items = $lm->applyProfileToListings( $profile, $product_ids ); foreach ($product_ids as $listing_id) { $item = ListingsModel::getItem($listing_id); $lm->applyProfileToItem($profile, $item); } // build response $response = new stdClass(); // $response->success = $prepared_count ? true : false; $response->success = true; $response->msg = sprintf(__('Profile "%s" was applied to %s items.', 'wplister'), $profile['profile_name'], count($product_ids)); $this->returnJSON($response); exit; } else { die('invalid select mode: ' . $select_mode); } if ($response->success) { // store ASIN as product meta // update_post_meta( $post_id, '_wple_asin', $asin ); // show message if ($response->skipped_count) { $response->msg = sprintf(__('%s product(s) have been prepared and %s products were skipped.', 'wplister'), $response->prepared_count, $response->skipped_count); } else { $response->msg = sprintf(__('%s product(s) have been prepared.', 'wplister'), $response->prepared_count); } // include link to prepared listings $response->msg .= ' <a href="admin.php?page=wplister&listing_status=prepared" class="button button-small">' . __('View prepared listings', 'wplister') . '</a>'; // show shorter message if no listings were prepared if (!$response->prepared_count) { $response->msg = sprintf(__('%s products have been skipped.', 'wplister'), $response->skipped_count); } if ($response->errors) { $response->msg .= '<br>' . join('<br>', $response->errors); } if ($response->warnings) { $response->msg .= '<br>' . join('<br>', $response->warnings); } $this->returnJSON($response); exit; } else { if (isset($lm->lastError)) { echo $lm->lastError . "\n"; } echo "Failed to prepare product!"; exit; } } }
public function handleActions() { // handle save listing if ($this->requestAction() == 'save_listing') { $this->saveListing(); } // set account_id $account_id = isset($_REQUEST['auction']) ? WPLE_ListingQueryHelper::getAccountID($_REQUEST['auction']) : false; // handle verify action if ($this->requestAction() == 'verify') { $this->initEC($account_id); $this->EC->verifyItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were verified with eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems verifying your items.', 'wplister'), 1); } } // handle revise action if ($this->requestAction() == 'revise') { $this->initEC($account_id); $this->EC->reviseItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were revised on eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems revising your items.', 'wplister'), 1); } } // handle publish to eBay action if ($this->requestAction() == 'publish2e') { $this->initEC($account_id); $this->EC->sendItemsToEbay($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were published on eBay.', 'wplister')); } else { $this->showMessage(__('Some items could not be published.', 'wplister'), 1); } } // handle relist action if ($this->requestAction() == 'relist') { $this->initEC($account_id); $this->EC->relistItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were re-listed on eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems relisting your items.', 'wplister'), 1); } } // handle end_item action if ($this->requestAction() == 'end_item') { $this->initEC($account_id); $this->EC->endItemsOnEbay($_REQUEST['auction']); $this->EC->closeEbay(); $this->showMessage(__('Selected listings were ended.', 'wplister')); } // handle update from eBay action if ($this->requestAction() == 'update') { $this->initEC($account_id); $this->EC->updateItemsFromEbay($_REQUEST['auction']); $this->EC->closeEbay(); $this->showMessage(__('Selected items were updated from eBay.', 'wplister')); } // handle delete action if (isset($_REQUEST['auction']) && $this->requestAction() == 'delete_listing') { $id = $_REQUEST['auction']; if (is_array($id)) { foreach ($id as $single_id) { WPLE_ListingQueryHelper::deleteItem($single_id); } } else { WPLE_ListingQueryHelper::deleteItem($id); } $this->showMessage(__('Selected items were removed.', 'wplister')); } // handle archive action if ($this->requestAction() == 'archive') { $id = $_REQUEST['auction']; $data = array('status' => 'archived'); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were archived.', 'wplister')); } // handle wple_reset_status action if ($this->requestAction() == 'wple_reset_status') { $lm = new ListingsModel(); $id = $_REQUEST['auction']; $data = array('status' => 'prepared', 'ebay_id' => NULL, 'end_date' => NULL, 'date_published' => NULL, 'last_errors' => ''); if (is_array($id)) { foreach ($id as $single_id) { $status = WPLE_ListingQueryHelper::getStatus($single_id); if (!in_array($status, array('ended', 'sold', 'archived'))) { wple_show_message("Item with status <i>{$status}</i> was skipped. Only ended and sold items can have their status reset to <i>prepared</i>.", 'warn'); continue; } ListingsModel::updateListing($single_id, $data); $lm->reapplyProfileToItem($single_id); } wple_show_message(__('Selected items had their status reset to prepared.', 'wplister')); } } // handle wple_clear_eps_data action if ($this->requestAction() == 'wple_clear_eps_data') { $id = $_REQUEST['auction']; if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateWhere(array('id' => $single_id), array('eps' => '')); } wple_show_message(__('EPS cache was cleared for selected items.', 'wplister')); } } // handle lock action if ($this->requestAction() == 'lock') { $id = $_REQUEST['auction']; $data = array('locked' => true); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were locked.', 'wplister')); } // handle unlock action if ($this->requestAction() == 'unlock') { $id = $_REQUEST['auction']; $data = array('locked' => false); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were unlocked.', 'wplister')); } // handle cancel_schedule action if ($this->requestAction() == 'cancel_schedule') { $id = $_REQUEST['auction']; $data = array('relist_date' => null); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were unscheduled from auto relist.', 'wplister')); } // clean listing archive if ($this->requestAction() == 'wpl_clean_listing_archive') { WPLE_ListingQueryHelper::cleanArchive(); $this->showMessage(__('Archive was cleared.', 'wplister')); } // handle toolbar action - prepare listing from product if ($this->requestAction() == 'wpl_prepare_single_listing') { // get profile $profilesModel = new ProfilesModel(); $profile = isset($_REQUEST['profile_id']) ? $profilesModel->getItem($_REQUEST['profile_id']) : false; if ($profile) { // prepare product $listingsModel = new ListingsModel(); $listing_id = $listingsModel->prepareProductForListing($_REQUEST['product_id'], $profile['profile_id']); if ($listing_id) { $listingsModel->applyProfileToNewListings($profile); $this->showMessage(__('New listing was prepared from product.', 'wplister')); } else { $msg = __('Could not create a new listing from this product.', 'wplister'); if ($listingsModel->errors) { $msg .= '<br>' . join('<br>', $listingsModel->errors); } if ($listingsModel->warnings) { $msg .= '<br>' . join('<br>', $listingsModel->warnings); } $this->showMessage($msg, 2); } } elseif (isset($_REQUEST['product_id'])) { // prepare product $listingsModel = new ListingsModel(); $listingsModel->prepareProductForListing($_REQUEST['product_id']); } } // handle reapply profile action if ($this->requestAction() == 'reapply') { $listingsModel = new ListingsModel(); $listingsModel->reapplyProfileToItems($_REQUEST['auction']); $this->showMessage(__('Profiles were re-applied to selected items.', 'wplister')); } // cancel (re-)selecting profile process if ($this->requestAction() == 'cancel_profile_selection') { ListingsModel::cancelSelectingListings(); } }