Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3301016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:42:45+00:00 2026-05-17T20:42:45+00:00

In the script below I iterate through a bunch of products. For each of

  • 0

In the script below I iterate through a bunch of products. For each of these I want to add one custom option “Eskestørrelse”. Works like a charm on the first product, however for the following products the custom options from the previous products are somehow kept and the product will end up with multiple custom options.

For example – the 10th product would have 10 custom options which are the ones generated for product 1, 2, 3, … 10.

What am I doing wrong?

Heres the script (if you prefer pastie, see http://pastie.org/1243529), custom options and product save in bold :

$categoryId = 128;
$storeId = 4;
$cwd = getcwd();
chdir($launchdir);
echo "Entered $launchdir...\n";

require_once('includes/config.php');
require_once('app/Mage.php');

try {

  $mageObj = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);;

  $category = Mage::getModel('catalog/category')->load($categoryId);
  $products = $category->getProductCollection()->addStoreFilter($storeId)->addAttributeToSelect('*');

  echo "Found " . count($products) . " products in category #{$categoryId} + store #{$storeId}...\n";

  $nodesc = array();
  $n = 0;
  foreach($products as $product) {
    $sku = $product->getSku();
    $desc = $product->getDescription();
    $shortdesc = $product->getShortDescription();
    $price = $product->getPrice();

    $matches = array();
    if (preg_match('/[Ee]ske med ([0-9]+)/', $desc, $matches)==0) {

      $nodesc[] = $product;

    }
    else {
      $product = Mage::getModel('catalog/product')->load($product->getId());

      $halfqty = (int)($matches[1] / 2);
      $halfpriceExact = ($price / 2) * 1.1;
      $halfprice = ceil($halfpriceExact/10) * 10;

      $pricediff = round(($halfprice / ($price / 2)) * 100) - 100;
      $savepct = round(($halfprice*2 - $price)/($halfprice*2)*100);

      echo "{$sku}: quantity ({$matches[1]}, {$halfqty}), price ({$price}, {$halfprice} ({$halfpriceExact}), half +{$pricediff}%), savepct {$savepct}% \n";

      $newdesc = preg_replace('/([Ee])ske med [0-9]+/', "$1ske med {$halfqty} eller {$matches[1]} ", $desc);
      $newshortdesc = preg_replace('/([Ee])ske med [0-9]+/', "$1ske med {$halfqty} eller {$matches[1]} ", $shortdesc);
      $product->setPrice($halfprice);
      $product->setDescription($newdesc . "\n\n<b>PS! </b>Du sparer $savepct% ved å kjøpe den største esken ({$matches[1]} stk).");
      $product->setShortDescription($newshortdesc);

      // figure out options
      $newopts = array();
      $newopts[] = array(
        'title' => 'Eskestørrelse',
        'type' => 'drop_down',
        'previous_type' => null,
        'previous_group' => 'select',
        'is_require' => 1,
        'is_delete' => null,
        'sort_order' => 1,
        'values' => array(
          array(
            'option_type_id' => -1,
            'is_delete' => null,
            'title' => "Eske med {$matches[1]} stk.",
            'price' => ($price - $halfprice),
            'price_type' => 'fixed',
            'sku' => "-{$matches[1]}",
            'sort_order' => '1'
          ),
          array(
            'option_type_id' => -1,
            'is_delete' => null,
            'title' => "Eske med {$halfqty} stk.",
            'price' => 0.00,
            'price_type' => 'fixed',
            'sku' => "-{$halfqty}",
            'sort_order' => '2'

          )
        )
      );

      if ($product->getOptionsReadonly()) {
        echo "READONLY options, cant save...\n";
      }
      else {
        $product->setProductOptions($newopts);
        $product->setCanSaveCustomOptions(true);
        $product->save();

        $n++;
        if ($n==2) die('temp stop');
      }
    }
    $options = null;
    $product = null;
  }

  echo "SUMMARY: " . count($products) . " products. " . count($nodesc) . " descriptions with no match. \n";
  foreach ($nodesc as $product) {
    echo "UNMATCHED: " . $product->getSku() . "\n";
  }

}
catch (Exception $e) {
  echo "Failed with exception " . $e . "\n";
}

chdir($cwd);
echo "Returned to $cwd...\n";
return;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:42:45+00:00Added an answer on May 17, 2026 at 8:42 pm

    I figured this one out after a few more hours and after sticking my nose into Magentos model classes. Seems like product options was designed with a singleton pattern.

    There’s no need to reload the product in line 37:

    $product = Mage::getModel('catalog/product')->load($product->getId());
    

    …that was just a desperate try to deal with this problem. What I should’ve done is resetting the multiple options set in the Magentos singleton Product_Option instance. Replacing line 37 with the line below resolves the problem:

    Mage::getSingleton('catalog/product_option')->unsetOptions(); // forget me and Magento will hate you
    

    Hope this can save someone else some frustration. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The script below works fine if 3 and 3.2 are not links but i
I tested out the script below in jsfiddle and it works fine, can someone
I am using PHP and I need to script something like below: I have
i want to iterate through a group of radio buttons generated dynamically and get
This script is supposed to to get a multidimensional array and iterate through the
I write a shell script below to add a job to cron. #!/bin/sh touch
The following script below is not running in IE7 but works perfectly fine in
I am using this script below and it works well except that it is
In the script below, I need to add an item None with value of
I'm trying to get the script below to produce a shortcut like this: C:\Program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.