/**
  * Delete a product attribute
  *
  * @param int $idAttribute The attribute ID
  * @param int $idProduct The product ID
  *
  * @return array
  */
 public function ajaxProcessDeleteProductAttribute($idAttribute, $idProduct)
 {
     if (!\CombinationCore::isFeatureActive()) {
         return false;
     }
     if ($idProduct && \ValidateCore::isUnsignedId($idProduct) && \ValidateCore::isLoadedObject($product = new \ProductCore($idProduct))) {
         if (($depends_on_stock = \StockAvailableCore::dependsOnStock($idProduct)) && \StockAvailableCore::getQuantityAvailableByProduct($idProduct, $idAttribute)) {
             return array('status' => 'error', 'message' => 'It is not possible to delete a combination while it still has some quantities in the Advanced Stock Management. You must delete its stock first.');
         } else {
             $product->deleteAttributeCombination((int) $idAttribute);
             $product->checkDefaultAttributes();
             \ToolsCore::clearColorListCache((int) $product->id);
             if (!$product->hasAttributes()) {
                 $product->cache_default_attribute = 0;
                 $product->update();
             } else {
                 \ProductCore::updateDefaultAttribute($idProduct);
             }
             if ($depends_on_stock && !\StockCore::deleteStockByIds($idProduct, $idAttribute)) {
                 return array('status' => 'error', 'message' => 'Error while deleting the stock');
             } else {
                 return array('status' => 'ok', 'message' => 'Successful deletion');
             }
         }
     } else {
         return array('status' => 'error', 'message' => 'You cannot delete this attribute.');
     }
 }
示例#2
0
    /**
     * Returns a preview url to a category page.
     *
     * @param int|null $id_category optional category ID if a specific category is required.
     * @param int|null $id_lang optional language ID if a specific language is needed.
     * @return string the url.
     */
    public function getPreviewUrlCategory($id_category = null, $id_lang = null)
    {
        try {
            if (!$id_category) {
                // Find a category that is active, not the root category and has a parent category.
                $sql = <<<EOT
\t\t\t\tSELECT `id_category`
\t\t\t\tFROM `ps_category`
\t\t\t\tWHERE `active` = 1
\t\t\t\tAND `id_parent` > 0
EOT;
                // There is not "is_root_category" in PS 1.4, but in >= 1.5 we want to skip the root.
                if (_PS_VERSION_ >= '1.5') {
                    $sql .= ' AND `is_root_category` = 0';
                }
                $row = Db::getInstance()->getRow($sql);
                $id_category = isset($row['id_category']) ? (int) $row['id_category'] : 0;
            }
            if (is_null($id_lang)) {
                $id_lang = (int) Context::getContext()->language->id;
            }
            $category = new Category($id_category, $id_lang);
            if (!ValidateCore::isLoadedObject($category)) {
                return '';
            }
            $params = array('nostodebug' => 'true');
            return $this->getCategoryUrl($category, $id_lang, null, $params);
        } catch (Exception $e) {
            // Return empty on failure
            return '';
        }
    }
 public function duplicateProduct($productId, $namePattern = 'copy of %s')
 {
     //TODO : use the $namePattern var to input translated version of 'copy of %s', if translation requested.
     $product = new \ProductCore($productId);
     if (!\ValidateCore::isLoadedObject($product)) {
         throw new \Exception('AdminProductDataUpdater->duplicateProduct() received an unknown ID.', 5005);
     }
     $id_product_old = $product->id;
     if (empty($product->price) && \ShopCore::getContext() == \ShopCore::CONTEXT_GROUP) {
         $shops = \ShopGroupCore::getShopsFromGroup(\ShopCore::getContextShopGroupID());
         foreach ($shops as $shop) {
             if ($product->isAssociatedToShop($shop['id_shop'])) {
                 $product_price = new \ProductCore($id_product_old, false, null, $shop['id_shop']);
                 $product->price = $product_price->price;
             }
         }
     }
     unset($product->id);
     unset($product->id_product);
     $product->indexed = 0;
     $product->active = 0;
     // change product name to prefix it
     foreach ($product->name as $langKey => $oldName) {
         if (!preg_match('/^' . str_replace('%s', '.*', preg_quote($namePattern, '/') . '$/'), $oldName)) {
             $newName = sprintf($namePattern, $oldName);
             if (mb_strlen($newName, 'UTF-8') <= 127) {
                 $product->name[$langKey] = $newName;
             }
         }
     }
     if ($product->add() && \CategoryCore::duplicateProductCategories($id_product_old, $product->id) && \ProductCore::duplicateSuppliers($id_product_old, $product->id) && ($combination_images = \ProductCore::duplicateAttributes($id_product_old, $product->id)) !== false && \GroupReductionCore::duplicateReduction($id_product_old, $product->id) && \ProductCore::duplicateAccessories($id_product_old, $product->id) && \ProductCore::duplicateFeatures($id_product_old, $product->id) && \ProductCore::duplicateSpecificPrices($id_product_old, $product->id) && \PackCore::duplicate($id_product_old, $product->id) && \ProductCore::duplicateCustomizationFields($id_product_old, $product->id) && \ProductCore::duplicateTags($id_product_old, $product->id) && \ProductCore::duplicateDownload($id_product_old, $product->id)) {
         if ($product->hasAttributes()) {
             \ProductCore::updateDefaultAttribute($product->id);
         }
         if (!\Image::duplicateProductImages($id_product_old, $product->id, $combination_images)) {
             throw new DataUpdateException('product', $id_product_old, 'An error occurred while copying images.', 5008);
         } else {
             $this->hookDispatcher->dispatchForParameters('actionProductAdd', array('id_product' => (int) $product->id, 'product' => $product));
             if (in_array($product->visibility, array('both', 'search')) && \Configuration::get('PS_SEARCH_INDEXATION')) {
                 \SearchCore::indexation(false, $product->id);
             }
             return $product->id;
         }
     } else {
         throw new \Exception('An error occurred while creating an object.', 5009);
     }
 }
 protected function restoreOrderToCart(Cart $cart)
 {
     if (!$cart || !ValidateCore::isLoadedObject($cart)) {
         return null;
     }
     Db::getInstance()->execute('BEGIN');
     $new_cart = null;
     try {
         /** @var CartCore $new_cart */
         /** @noinspection PhpUndefinedClassInspection */
         $new_cart = new Cart();
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_customer = (int) $cart->id_customer;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_address_delivery = (int) $cart->id_address_delivery;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_address_invoice = (int) $cart->id_address_invoice;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_lang = (int) $cart->id_lang;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_currency = (int) $cart->id_currency;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->id_carrier = (int) $cart->id_carrier;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->recyclable = (int) $cart->recyclable;
         /** @noinspection PhpUndefinedFieldInspection */
         $new_cart->gift = (int) $cart->gift;
         $new_cart->add();
         /** @noinspection PhpUndefinedMethodInspection */
         $products = $cart->getProducts();
         if ($products) {
             foreach ($products as $p) {
                 $idProduct = $p['id_product'];
                 $idProductAttribute = $p['id_product_attribute'];
                 $qty = $p['cart_quantity'];
                 /** @noinspection PhpUndefinedClassInspection */
                 /** @noinspection PhpUndefinedFieldInspection */
                 $producToAdd = new Product((int) $idProduct, true, (int) $cart->id_lang);
                 /** @noinspection PhpUndefinedFieldInspection */
                 if (!$producToAdd->id || !$producToAdd->active) {
                     continue;
                 }
                 /* Check the quantity availability  */
                 if ($idProductAttribute > 0 and is_numeric($idProductAttribute)) {
                     /** @noinspection PhpUndefinedClassInspection */
                     /** @noinspection PhpUndefinedMethodInspection */
                     /** @noinspection PhpUndefinedFieldInspection */
                     if (!$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) and !Attribute::checkAttributeQty((int) $idProductAttribute, (int) $qty)) {
                         /* There is not enough product attribute in stock - set customer qty to current stock on hand */
                         /** @noinspection PhpUndefinedFunctionInspection */
                         $qty = getAttributeQty($idProductAttribute);
                     }
                 } elseif (!$producToAdd->checkQty((int) $qty)) {
                     /* There is not enough product in stock - set customer qty to current stock on hand */
                     /** @noinspection PhpUndefinedMethodInspection */
                     $qty = $producToAdd->getQuantity($idProduct);
                 }
                 $new_cart->updateQty((int) $qty, (int) $idProduct, (int) $idProductAttribute, NULL, 'up');
                 unset($p);
             }
         }
         $new_cart->update();
         Db::getInstance()->execute('COMMIT');
     } catch (Exception $e) {
         Db::getInstance()->execute('ROLLBACK');
         throw $e;
     }
     /** @noinspection PhpUndefinedFieldInspection */
     $this->context->cookie->id_cart = (int) $new_cart->id;
     return $new_cart;
 }
示例#5
0
 /**
  * This method is used to render the payment button,
  * Take care if the button should be displayed or not.
  */
 public function hookPayment($params)
 {
     include_once _PS_MODULE_DIR_ . 'alipay/api/loader.php';
     $currency_id = $params['cart']->id_currency;
     $currency = new Currency((int) $currency_id);
     $cart = new Cart($params['cart']->id);
     if (!ValidateCore::isLoadedObject($cart)) {
         return false;
     }
     if (in_array($currency->iso_code, $this->limited_currencies) == false) {
         return false;
     }
     $service = Configuration::get('ALIPAY_SERVICE_PAYMENT');
     $credentials = AlipayTools::getCredentials($service, false);
     $alipayapi = new AlipayApi($credentials);
     $alipayapi->setReturnUrl($this->getReturnUrl($cart->secure_key, $cart->id));
     $alipayapi->setNotifyUrl($this->getNotifyUrl($cart->secure_key, $cart->id));
     $alipayapi->setCharset('UTF-8');
     date_default_timezone_set('Asia/Hong_Kong');
     $payment_request = new PaymentRequest();
     $payment_request->setCurrency($currency->iso_code);
     $payment_request->setPartnerTransactionId(date('YmdHis') . $cart->id);
     $payment_request->setGoodsDescription($this->getGoodsDescription());
     $payment_request->setGoodsName($this->getGoodsName($cart->id));
     $payment_request->setOrderGmtCreate(date('Y-m-d H:i:s'));
     $payment_request->setOrderValidTime(21600);
     $payment_request->setTotalFee($cart->getOrderTotal());
     $alipayapi->prepareRequest($payment_request);
     $url = $alipayapi->createUrl();
     $this->smarty->assign(array('module_dir' => $this->_path, 'alipay_payment_url' => $url));
     return $this->display(__FILE__, 'views/templates/hook/payment.tpl');
 }
 protected function processOrderStateChange($new_order_state, array $payload, \Payin7\Models\OrderModel $order, &$message, &$code)
 {
     $ret = true;
     $message = null;
     $code = null;
     switch ($new_order_state) {
         case 'cancel':
             /** @var OrderCore $orderm */
             /** @noinspection PhpUndefinedClassInspection */
             $orderm = new Order($order->getOrderId());
             if (ValidateCore::isLoadedObject($orderm)) {
                 $state = $orderm->current_state;
                 if ($state == $this->module->getConfigIdOrderStatePending()) {
                     // temporarily disable updating history back to Payin7
                     $this->module->setHistoryUpdateEnabled(false);
                     $orderm->setCurrentState($this->module->getConfigIdOrderStateCancelled());
                     // reenable history
                     $this->module->setHistoryUpdateEnabled(true);
                     $message = 'Local order state set to (1): ' . $this->module->getConfigIdOrderStateCancelled();
                 }
             } else {
                 $message = 'Local order could not be loaded (2)';
                 $ret = false;
             }
             break;
         case 'active':
             $is_verified = isset($payload['is_verified']) ? (bool) $payload['is_verified'] : false;
             $is_paid = isset($payload['is_paid']) ? (bool) $payload['is_paid'] : false;
             // update the order state
             if (!$order->getPayin7OrderAccepted()) {
                 $order->setPayin7OrderAccepted(true);
                 $order->savePayin7Data();
             }
             if ($is_verified && $is_paid) {
                 /** @var OrderCore $orderm */
                 /** @noinspection PhpUndefinedClassInspection */
                 $orderm = new Order($order->getOrderId());
                 $state = $orderm->current_state;
                 if ($state == $this->module->getConfigIdOrderStatePending()) {
                     // temporarily disable updating history back to Payin7
                     $this->module->setHistoryUpdateEnabled(false);
                     $orderm->setCurrentState($this->module->getConfigIdOrderStateAccepted());
                     // reenable history
                     $this->module->setHistoryUpdateEnabled(true);
                     $message = 'Local order state set to (2): ' . $this->module->getConfigIdOrderStateAccepted();
                 }
             } else {
                 $message = 'Order not verified / paid';
                 $ret = false;
             }
             break;
     }
     return $ret;
 }
示例#7
0
 /**
  * Get specific prices list for a product
  *
  * @param object $product
  * @param object $defaultCurrency
  * @param array $shops Available shops
  * @param array $currencies Available currencies
  * @param array $countries Available countries
  * @param array $groups Available users groups
  *
  * @return array
  */
 public function getSpecificPricesList($product, $defaultCurrency, $shops, $currencies, $countries, $groups)
 {
     $content = [];
     $specific_prices = \SpecificPriceCore::getByProductId((int) $product->id);
     $tmp = array();
     foreach ($shops as $shop) {
         $tmp[$shop['id_shop']] = $shop;
     }
     $shops = $tmp;
     $tmp = array();
     foreach ($currencies as $currency) {
         $tmp[$currency['id_currency']] = $currency;
     }
     $currencies = $tmp;
     $tmp = array();
     foreach ($countries as $country) {
         $tmp[$country['id_country']] = $country;
     }
     $countries = $tmp;
     $tmp = array();
     foreach ($groups as $group) {
         $tmp[$group['id_group']] = $group;
     }
     $groups = $tmp;
     if (is_array($specific_prices) && count($specific_prices)) {
         foreach ($specific_prices as $specific_price) {
             $id_currency = $specific_price['id_currency'] ? $specific_price['id_currency'] : $defaultCurrency->id;
             if (!isset($currencies[$id_currency])) {
                 continue;
             }
             $current_specific_currency = $currencies[$id_currency];
             if ($specific_price['reduction_type'] == 'percentage') {
                 $impact = '- ' . $specific_price['reduction'] * 100 . ' %';
             } elseif ($specific_price['reduction'] > 0) {
                 $impact = '- ' . \ToolsCore::displayPrice(\Tools::ps_round($specific_price['reduction'], 2), $current_specific_currency) . ' ';
                 if ($specific_price['reduction_tax']) {
                     $impact .= '(' . $this->translator->trans('Tax incl.', [], 'Admin.Global') . ')';
                 } else {
                     $impact .= '(' . $this->translator->trans('Tax excl.', [], 'Admin.Global') . ')';
                 }
             } else {
                 $impact = '--';
             }
             if ($specific_price['from'] == '0000-00-00 00:00:00' && $specific_price['to'] == '0000-00-00 00:00:00') {
                 $period = $this->translator->trans('Unlimited', [], 'Admin.Global');
             } else {
                 $period = $this->translator->trans('From', [], 'Admin.Global') . ' ' . ($specific_price['from'] != '0000-00-00 00:00:00' ? $specific_price['from'] : '0000-00-00 00:00:00') . '<br />' . $this->translator->trans('to', [], 'Admin.Global') . ' ' . ($specific_price['to'] != '0000-00-00 00:00:00' ? $specific_price['to'] : '0000-00-00 00:00:00');
             }
             if ($specific_price['id_product_attribute']) {
                 $combination = new \CombinationCore((int) $specific_price['id_product_attribute']);
                 $attributes = $combination->getAttributesName(1);
                 $attributes_name = '';
                 foreach ($attributes as $attribute) {
                     $attributes_name .= $attribute['name'] . ' - ';
                 }
                 $attributes_name = rtrim($attributes_name, ' - ');
             } else {
                 $attributes_name = $this->translator->trans('All combinations', [], 'Admin.Catalog.Feature');
             }
             $rule = new \SpecificPriceRuleCore((int) $specific_price['id_specific_price_rule']);
             $rule_name = $rule->id ? $rule->name : '--';
             if ($specific_price['id_customer']) {
                 $customer = new \CustomerCore((int) $specific_price['id_customer']);
                 if (\ValidateCore::isLoadedObject($customer)) {
                     $customer_full_name = $customer->firstname . ' ' . $customer->lastname;
                 }
                 unset($customer);
             }
             if (!$specific_price['id_shop'] || in_array($specific_price['id_shop'], \ShopCore::getContextListShopID())) {
                 $can_delete_specific_prices = true;
                 if (\ShopCore::isFeatureActive()) {
                     $can_delete_specific_prices = count($this->legacyContext->employee->getAssociatedShops()) > 1 && !$specific_price['id_shop'] || $specific_price['id_shop'];
                 }
                 $price = \ToolsCore::ps_round($specific_price['price'], 2);
                 $fixed_price = $price == \ToolsCore::ps_round($product->price, 2) || $specific_price['price'] == -1 ? '--' : \ToolsCore::displayPrice($price, $current_specific_currency);
                 $content[] = ['id_specific_price' => $specific_price['id_specific_price'], 'id_product' => $product->id, 'rule_name' => $rule_name, 'attributes_name' => $attributes_name, 'shop' => $specific_price['id_shop'] ? $shops[$specific_price['id_shop']]['name'] : $this->translator->trans('All shops', [], 'Admin.Global'), 'currency' => $specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->translator->trans('All currencies', [], 'Admin.Global'), 'country' => $specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->translator->trans('All countries', [], 'Admin.Global'), 'group' => $specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->translator->trans('All groups', [], 'Admin.Global'), 'customer' => isset($customer_full_name) ? $customer_full_name : $this->translator->trans('All customers', [], 'Admin.Global'), 'fixed_price' => $fixed_price, 'impact' => $impact, 'period' => $period, 'from_quantity' => $specific_price['from_quantity'], 'can_delete' => !$rule->id && $can_delete_specific_prices ? true : false];
                 unset($customer_full_name);
             }
         }
     }
     return $content;
 }