We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2611
    • 394 Posts
    If I do this:

    $categories = $product->getMany('ProductCategory');


    And loop through them and call a toArray on one of the objects it gives me:

    Fatal error: Call to undefined method modX::toArray()


    What am I missing here? It says it’s a modX object, that’s wrong right?

    Here’s the scheme part that matters:
        <object class="vcCategory" table="visioncart_categories" extends="xPDOSimpleObject">
        	<!-- This table holds all the categories for the products and its settings -->
        	<field key="shopid" dbtype="int" phptype="integer" precision="8" null="false" default="" />
    		<field key="name" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
    		<field key="alias" dbtype="varchar" precision="50" phptype="string" null="false" default="" index="index" />
    		<field key="description" dbtype="text" phptype="string" null="false" default="" />
    		<field key="parent" dbtype="int" phptype="integer" precision="8" null="false" default="" />
    		<field key="sort" dbtype="int" phptype="integer" precision="8" null="false" default="" />
    		<field key="config" dbtype="text" phptype="json" null="false" default="" />
    		<field key="customfields" dbtype="text" phptype="json" null="false" default="" />
    		<field key="pricechange" dbtype="decimal" precision="8,2" phptype="float" null="false" default="" />
    		<field key="pricepercent" dbtype="tinyint" precision="1" attributes="unsigned" phptype="boolean" null="false" default="0" />
    		<field key="active" dbtype="tinyint" precision="1" attributes="unsigned" phptype="boolean" null="false" default="0" />
    	
    		<composite alias="ProductCategory" class="vcProductCategory" local="id" foreign="categoryid" cardinality="many" owner="local" />
    		<aggregate alias="Shop" class="vcShop" local="shopid" foreign="id" cardinality="one" owner="foreign" />
        </object>
        
        <object class="vcProductCategory" table="visioncart_products_categories" extends="xPDOSimpleObject">
        	<!-- This table holds the links between categories and products -->
        	<field key="shopid" dbtype="int" phptype="integer" precision="8" null="false" default="" />
        	<field key="categoryid" dbtype="int" phptype="integer" precision="8" null="false" default="" index="index" />
        	<field key="productid" dbtype="int" phptype="integer" precision="8" null="false" default="" index="index" />
        	<field key="sort" dbtype="int" phptype="integer" precision="8" null="false" default="" />
        	
        	<aggregate alias="Product" class="vcProduct" local="productid" foreign="id" cardinality="one" owner="foreign" />
        	<aggregate alias="Shop" class="vcShop" local="shopid" foreign="id" cardinality="one" owner="foreign" />
        	<aggregate alias="Category" class="vcCategory" local="categoryid" foreign="id" cardinality="one" owner="foreign" />
        </object>
        
        <object class="vcProduct" table="visioncart_products" extends="xPDOSimpleObject">
        	<!-- This table holds all the products and its settings -->
        	<field key="shopid" dbtype="int" phptype="integer" precision="8" null="false" default="" />
        	<field key="parent" dbtype="int" phptype="integer" precision="8" null="false" default="" />
        	<field key="taxcategory" dbtype="int" phptype="integer" precision="8" null="false" default="" />
    		<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
    		<field key="alias" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
    		<field key="description" dbtype="text" phptype="string" null="false" default="" />
    		<field key="articlenumber" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
    		<field key="price" dbtype="decimal" precision="8,2" phptype="float" null="false" default="" index="index" />
    		<field key="weight" dbtype="decimal" precision="8,2" phptype="float" null="false" default="" />
    		<field key="shippingprice" dbtype="decimal" precision="8,2" phptype="float" null="false" default="" />
    		<field key="publishdate" dbtype="int" precision="20" phptype="timestamp" null="false" default="0" index="index" />
            <field key="unpublishdate" dbtype="int" precision="20" phptype="timestamp" null="false" default="0" index="index" />
    		<field key="customfields" dbtype="text" phptype="json" null="false" default="" />
    		<field key="pictures" dbtype="text" phptype="json" null="false" default="" />
    		<field key="stock" dbtype="int" phptype="integer" precision="8" null="false" default="" />
    		<field key="active" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
    
    		<composite alias="ProductOption" class="vcProductOption" local="id" foreign="productid" cardinality="many" owner="local" />
    		<composite alias="ProductCategory" class="vcProductCategory" local="id" foreign="productid" cardinality="many" owner="local" /> 
    		<aggregate alias="TaxCategory" class="vcCategory" local="taxcategory" foreign="id" cardinality="one" owner="foreign" />
    		<aggregate alias="Shop" class="vcShop" local="shopid" foreign="id" cardinality="one" owner="foreign" />
        </object>
      Follow me on twitter: @b03tz
      Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
      CodeMaster
      • 22303 MODX Staff
      • 10,725 Posts
      You are doing $category->toArray() right? Not $modx->toArray() huh
        • 2611
        • 394 Posts
        Yes...like so:

        <?php
            	// Get all links to categories for this product
            	$productCategories = $product->getMany('ProductCategory');
            	
            	foreach($productCategories as $category) {
            		print_r($category->toArray());
            		exit(); // dev code
            	}
          Follow me on twitter: @b03tz
          Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
          CodeMaster
          • 28215
          • 4,149 Posts
          Can you post your entire code for that part?
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 2611
            • 394 Posts
            I managed to circumvent the use of getMany with a query...but I still don’t get
            why this wouldn’t work.

            <?php
                public function calculateProductPrice($product, $asArray=false) {
                	if (is_array($product) && isset($product['id'])) {
                		$product = $this->modx->getObject('vcProduct', $product['id']);
                	}
                	
                	if ($product == null) {
                		return false;	
                	}
                	
                	if ($asArray) {
                		$productArray = array();
                	}	
            
                	$categoryPrices = array();
                	
                	$this->fireEvent('vcCalculateProductPrice', 'onBeforeCalculate', array(
                		'product' => &$product,
                		'asArray' => $asArray
                	));
            
                	// Get all links to categories for this product
                	$productCategories = $product->getMany('ProductCategory');
            
                	foreach($productCategories as $productCategory) {
                		$category = $productCategory->getOne('Category');
                		if ($category->active != 1) {
                			continue;	
                		}
                		
                		// Check if it's by amount
                		if ($category->get('pricechange') != '' && ($category->get('pricepercent') == '' || $category->get('pricepercent') == '0')) {
                			$priceChange = floatval($category->get('pricechange'));
                			
                			if ($priceChange > 0) {
                				$categoryPrices[$category->get('id')] = $product->get('price') + $category->get('pricechange');
                			} else {
                				$categoryPrices[$category->get('id')] = $product->get('price') - abs($category->get('pricechange'));
                			}
                		} else {
                			// Or by percentage	
                			$percentage = floatval($category->get('pricechange'));
                			
                			if ($percentage > 0) {
                				$percentagePrice = ($product->get('price') / 100) * $percentage;
                				$categoryPrices[$category->get('id')] = $product->get('price') + $percentagePrice;	
                			} else {
                				$percentagePrice = ($product->get('price') / 100) * abs($percentage);
                				$categoryPrices[$category->get('id')] = $product->get('price') - $percentagePrice;
                			}
                		}
                	}
                	    	
                	// Get the cheapest price
                	$cheapest = min($categoryPrices);
                	
                	$product->set('price', $cheapest);
                	
                	$this->fireEvent('vcCalculateProductPrice', 'onAfterCalculate', array(
                		'product' => &$product,
                		'asArray' => $asArray
                	));
            
                	// Get the tax category
                	$taxCategory = $product->getOne('TaxCategory');
                	$taxAmount = ($product->get('price') / 100) * abs($taxCategory->get('pricechange'));
                	$totalAmount = number_format(($product->get('price') + $taxAmount), 2, '.', '');
                	
                	if ($asArray) {
                		$productArray['ex'] = $product->get('price');
                		$productArray['in'] = $totalAmount;
                		return $productArray;
                	}
                	
                	return $totalAmount;
                }
              Follow me on twitter: @b03tz
              Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
              CodeMaster
              • 2611
              • 394 Posts
              I am still really curious as to what went wrong though laugh
                Follow me on twitter: @b03tz
                Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
                CodeMaster