{"id":621,"date":"2023-02-26T11:03:10","date_gmt":"2023-02-26T03:03:10","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=621"},"modified":"2023-04-29T20:17:44","modified_gmt":"2023-04-29T12:17:44","slug":"opencart-development-summary","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/opencart-development-summary\/","title":{"rendered":"OpenCart\u5f00\u53d1\u603b\u7ed3"},"content":{"rendered":"<h3>\u83b7\u53d6\u8d2d\u7269\u8f66\u5546\u54c1<\/h3>\n<pre><code class=\"language-php\">$products = $this-&gt;cart-&gt;getProducts();<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u83b7\u53d6Customer\u767b\u5f55\u72b6\u6001<\/h3>\n<pre><code class=\"language-php\">$data[&#039;logged&#039;] = $this-&gt;customer-&gt;isLogged();<\/code><\/pre>\n<h3>\u83b7\u53d6\u767b\u5f55Customer\u4fe1\u606f<\/h3>\n<pre><code class=\"language-php\">$this-&gt;load-&gt;model(&#039;account\/customer&#039;);\n$customer_info = array();\nif ($this-&gt;customer-&gt;isLogged()) {\n    $customer_info = $this-&gt;model_account_customer-&gt;getCustomer($this-&gt;customer-&gt;getId());\n}<\/code><\/pre>\n<h3>\u83b7\u53d6\u8ba2\u5355\u5305\u542b\u5546\u54c1\u4fe1\u606f<\/h3>\n<pre><code class=\"language-php\">$order_products = $this-&gt;db-&gt;query(&quot;SELECT `name`,`quantity` as `qty`,`price` FROM `&quot; . DB_PREFIX . &quot;order_product` WHERE order_id = $order_id&quot;);\n$products = $order_products-&gt;rows;  \/\/\u591a\u6761\u8bb0\u5f55\n$product = $order_products-&gt;row;  \/\/\u5355\u6761\u8bb0\u5f55<\/code><\/pre>\n<p>catalog\/model\/checkout\/order.php<\/p>\n<pre><code class=\"language-php\">public function getOrder($order_id) {\n    $order_products = $this-&gt;db-&gt;query(&quot;SELECT `name`,`quantity`,`price` FROM `&quot; . DB_PREFIX . &quot;order_product` WHERE order_id = $order_id&quot;);\n    \/\/$products = json_encode($order_products-&gt;row, JSON_UNESCAPED_SLASHES);\n    return array(\n        &#039;order_id&#039;                =&gt; $order_query-&gt;row[&#039;order_id&#039;],\n        &#039;products&#039;                =&gt; $order_products-&gt;rows,\n        ...\n    )\n}<\/code><\/pre>\n<h3>Checkout\u652f\u4ed8\u65b9\u5f0f\u9875\u63d0\u4ea4\u53c2\u6570<\/h3>\n<p>catalog\/view\/theme\/default\/template\/checkout\/payment_method.twig<\/p>\n<pre><code class=\"language-html\">&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;* &lt;\/span&gt;Document Type&lt;\/strong&gt;&lt;\/p&gt;\n&lt;p&gt;\n  &lt;input name=&quot;doc_type&quot; class=&quot;form-control&quot; value=&quot;{{ doc_type }}&quot;\/&gt;\n&lt;\/p&gt;\n&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;* &lt;\/span&gt;Document No.&lt;\/strong&gt;&lt;\/p&gt;\n&lt;p&gt;\n  &lt;input name=&quot;doc_no&quot; class=&quot;form-control&quot; value=&quot;{{ doc_no }}&quot;\/&gt;\n&lt;\/p&gt;\n\n...\n\n$(document).delegate(&#039;#button-payment-method&#039;, &#039;click&#039;, function() {\n    $.ajax({\n        url: &#039;index.php?route=checkout\/payment_method\/save&#039;,\n        type: &#039;post&#039;,\n        data: $(&#039;#collapse-payment-method input[type=\\&#039;radio\\&#039;]:checked, #collapse-payment-method input[type=\\&#039;checkbox\\&#039;]:checked, #collapse-payment-method textarea, #collapse-payment-method input[name=\\&#039;doc_type\\&#039;], #collapse-payment-method input[name=\\&#039;doc_no\\&#039;]&#039;),<\/code><\/pre>\n<h3>Checkout\u5730\u5740\u8be6\u60c5\u9875\u6821\u9a8c\u5730\u5740<\/h3>\n<p>catalog\/controller\/checkout\/payment_address.php<\/p>\n<pre><code class=\"language-php\">$this-&gt;load-&gt;model(&#039;account\/address&#039;);\nif (isset($this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;address_id&#039;])) {\n    $address_id = $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;address_id&#039;];\n} else {\n    $address_id = $this-&gt;customer-&gt;getAddressId();\n}\n\n$address = $this-&gt;model_account_address-&gt;getAddress($address_id);\nif (empty($address[&#039;city&#039;]) || empty($address[&#039;district&#039;]) || empty($address[&#039;street_name&#039;]) || empty($address[&#039;house_number&#039;]) || empty($address[&#039;postcode&#039;])) {\n    $json[&#039;error&#039;][&#039;warning&#039;] = &#039;Warning: You must complete your address info to continue!&#039;;\n}<\/code><\/pre>\n<h3>Checkout\u652f\u4ed8\u65b9\u5f0f\u9875\u6821\u9a8c\u8bc1\u4ef6<\/h3>\n<p>catalog\/controller\/checkout\/payment_method.php<\/p>\n<pre><code class=\"language-php\">if ($information_info &amp;&amp; !isset($this-&gt;request-&gt;post[&#039;agree&#039;])) {\n    $json[&#039;error&#039;][&#039;warning&#039;] = sprintf($this-&gt;language-&gt;get(&#039;error_agree&#039;), $information_info[&#039;title&#039;]);\n} else {\n    \/\/\u8bc1\u4ef6\u53f7\n    \/*\n    $this-&gt;load-&gt;model(&#039;account\/customer&#039;);\n    $document = &#039;&#039;;\n    if ($this-&gt;customer-&gt;isLogged()) {\n        $customer_info = $this-&gt;model_account_customer-&gt;getCustomer($this-&gt;customer-&gt;getId());\n        $document = $customer_info[&#039;doc_no&#039;];\n    }\n\n    if (empty($document)) {\n        $json[&#039;error&#039;][&#039;warning&#039;] = &#039;Warning: You must set the identity document (like CPF) info for your account!&#039;;\n    }\n    *\/\n\n    if (!isset($this-&gt;request-&gt;post[&#039;doc_type&#039;]) || empty($this-&gt;request-&gt;post[&#039;doc_type&#039;])\n        || !isset($this-&gt;request-&gt;post[&#039;doc_no&#039;]) || empty($this-&gt;request-&gt;post[&#039;doc_no&#039;])) {\n        $json[&#039;error&#039;][&#039;warning&#039;] = &#039;Warning: You must set the identity document (like CPF) info for your account!&#039;;\n    }\n}<\/code><\/pre>\n<h3>Checkout\u652f\u4ed8\u65b9\u5f0f\u9875\u6dfb\u52a0\u8bc1\u4ef6\u4fe1\u606fSession<\/h3>\n<p>catalog\/controller\/checkout\/payment_method.php<\/p>\n<pre><code class=\"language-php\">if (!$json) {\n    $this-&gt;session-&gt;data[&#039;payment_method&#039;] = $this-&gt;session-&gt;data[&#039;payment_methods&#039;][$this-&gt;request-&gt;post[&#039;payment_method&#039;]];\n\n    $this-&gt;session-&gt;data[&#039;comment&#039;] = strip_tags($this-&gt;request-&gt;post[&#039;comment&#039;]);\n\n    \/\/\u8bc1\u4ef6\u4fe1\u606fSession\n    if ($this-&gt;customer-&gt;isLogged()) {\n        $this-&gt;session-&gt;data[&#039;doc_type&#039;] = $this-&gt;request-&gt;post[&#039;doc_type&#039;];\n        $this-&gt;session-&gt;data[&#039;doc_no&#039;] = $this-&gt;request-&gt;post[&#039;doc_no&#039;];\n    } else {\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;doc_type&#039;] = $this-&gt;request-&gt;post[&#039;doc_type&#039;];\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;doc_no&#039;] = $this-&gt;request-&gt;post[&#039;doc_no&#039;];\n    }\n}<\/code><\/pre>\n<h3>\u6539\u9020Checkout\u6ce8\u518c\u9875\u9762<\/h3>\n<p>catalog\/view\/theme\/default\/template\/checkout\/register.twig<\/p>\n<pre><code class=\"language-html\">&lt;div class=&quot;form-group required&quot;&gt;\n  &lt;label class=&quot;control-label&quot; for=&quot;input-payment-district&quot;&gt;{{ entry_district }}&lt;\/label&gt;\n  &lt;input type=&quot;text&quot; name=&quot;district&quot; value=&quot;&quot; placeholder=&quot;{{ entry_district }}&quot; id=&quot;input-payment-district&quot; class=&quot;form-control&quot; \/&gt;\n&lt;\/div&gt;\n&lt;div class=&quot;form-group required&quot;&gt;\n  &lt;label class=&quot;control-label&quot; for=&quot;input-payment-street_name&quot;&gt;{{ entry_street_name }}&lt;\/label&gt;\n  &lt;input type=&quot;text&quot; name=&quot;street_name&quot; value=&quot;&quot; placeholder=&quot;{{ entry_street_name }}&quot; id=&quot;input-payment-street_name&quot; class=&quot;form-control&quot; \/&gt;\n&lt;\/div&gt;\n&lt;div class=&quot;form-group required&quot;&gt;\n  &lt;label class=&quot;control-label&quot; for=&quot;input-payment-house_number&quot;&gt;{{ entry_house_number }}&lt;\/label&gt;\n  &lt;input type=&quot;text&quot; name=&quot;house_number&quot; value=&quot;&quot; placeholder=&quot;{{ entry_house_number }}&quot; id=&quot;input-payment-house_number&quot; class=&quot;form-control&quot; \/&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<h3>Guest\u7528\u6237Checkout\u6539\u9020<\/h3>\n<p>catalog\/controller\/checkout\/guest.php<\/p>\n<pre><code class=\"language-php\">public function index() {\n    ...\n\n    if (isset($this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;district&#039;])) {\n        $data[&#039;district&#039;] = $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;district&#039;];\n    } else {\n        $data[&#039;district&#039;] = &#039;&#039;;\n    }\n\n    if (isset($this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;street_name&#039;])) {\n        $data[&#039;street_name&#039;] = $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;street_name&#039;];\n    } else {\n        $data[&#039;street_name&#039;] = &#039;&#039;;\n    }\n\n    if (isset($this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;house_number&#039;])) {\n        $data[&#039;house_number&#039;] = $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;house_number&#039;];\n    } else {\n        $data[&#039;house_number&#039;] = &#039;&#039;;\n    }\n\n    ...\n}\n\npublic function save() {\n    ...\n\n    if (!$json) {\n        $this-&gt;session-&gt;data[&#039;account&#039;] = &#039;guest&#039;;\n\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;customer_group_id&#039;] = $customer_group_id;\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;firstname&#039;] = $this-&gt;request-&gt;post[&#039;firstname&#039;];\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;lastname&#039;] = $this-&gt;request-&gt;post[&#039;lastname&#039;];\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;email&#039;] = $this-&gt;request-&gt;post[&#039;email&#039;];\n        $this-&gt;session-&gt;data[&#039;guest&#039;][&#039;telephone&#039;] = $this-&gt;request-&gt;post[&#039;telephone&#039;];\n        ...\n\n        $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;district&#039;] = $this-&gt;request-&gt;post[&#039;district&#039;];\n        $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;street_name&#039;] = $this-&gt;request-&gt;post[&#039;street_name&#039;];\n        $this-&gt;session-&gt;data[&#039;payment_address&#039;][&#039;house_number&#039;] = $this-&gt;request-&gt;post[&#039;house_number&#039;];\n        ...\n\n        if ($this-&gt;session-&gt;data[&#039;guest&#039;][&#039;shipping_address&#039;]) {\n            ...\n\n            $this-&gt;session-&gt;data[&#039;shipping_address&#039;][&#039;district&#039;] = $this-&gt;request-&gt;post[&#039;district&#039;];\n            $this-&gt;session-&gt;data[&#039;shipping_address&#039;][&#039;street_name&#039;] = $this-&gt;request-&gt;post[&#039;street_name&#039;];\n            $this-&gt;session-&gt;data[&#039;shipping_address&#039;][&#039;house_number&#039;] = $this-&gt;request-&gt;post[&#039;house_number&#039;];\n            ...\n}<\/code><\/pre>\n<p>catalog\/view\/theme\/default\/template\/checkout\/guest.twig<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u83b7\u53d6\u8d2d\u7269\u8f66\u5546\u54c1 $products = $this-&gt;cart-&gt;getProducts(); \u83b7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[],"class_list":["post-621","post","type-post","status-publish","format-standard","hentry","category-opencart"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}